【问题标题】:Duplicate File Pointer at the same position相同位置的重复文件指针
【发布时间】:2016-04-08 10:53:31
【问题描述】:

大家好, 我有这个代码:

int lenInput;
char input[64], buffer[512], temp[512], *ret, tagName[] = "<name>", tagItem[] = "<item ";
bool deleted = false;
FILE *fp, *fpTemp = NULL;

if(! (fp = fopen(nameFile, "r+")) ) {
    perror("Error Opening File");
    exit(-1);
}

printf("Insert the Name of the service you want to erase... ");
fgets(input, sizeof(input), stdin);

lenInput = (int) strlen(input);
input[lenInput-1] = '\0';
lenInput = (int) strlen(input);

do {
    fgets(buffer, sizeof(buffer), fp);
    buffer[strlen(buffer)-1] = '\0';

    if( (ret = strstr(buffer, tagName)) != NULL ) {
        if( strncmp(ret, tagName, strlen(tagName)) == 0 ) {
            if( (ret = strstr(ret, input)) != NULL ) {
                 if( strncmp(ret, input, lenInput) == 0 ) {
                     snprintf(temp, sizeof(temp), "<item present=\"false\">\n");
                     fputs(temp, fpTemp);

                     deleted = true;
                  }
             }
        }
    }
    else if( (ret = strstr(buffer, tagItem)) != NULL ) {
        if( strncmp(ret, tagItem, strlen(tagItem)) == 0 ) {
             fpTemp = fdopen( dup ( fileno(fp) ), "r+");     /* associates a stream with the existing file descriptor, fd */
        }
    }
} while( (deleted != true) && (!(feof(fp))) );

if(deleted == false)
    printf("Error: Service Not Found!\n");
else
    printf("Success: Service Erased!\n");

它适用于这样的文件:

<serviceNumber>2</serviceNumber>
<item present="true">
    <id>1</id>
    <name>name1</name>
    <description>descr1</description>
    <x>1</x>
    <y>1</y>
</item>
<item present="true">
    <id>2</id>
    <name>name2</name>
    <description>descr2</description>
    <x>2</x>
    <y>2</y>
</item>

主文件指针(FILE *fp)在main()中。

如果我找到标签&lt;item ...&gt;,我的想法是复制文件指针fp(在原型中传递),因为如果这个标签链接到我想要删除的服务的名称,我要替换整个&lt;item ...&gt; 字符串。

但是,我有一个问题......当我将执行snprintf() 和fputs() 时,文件在文件开头被覆盖,因为,恕我直言,我认为文件指针没有很好地重复。

有解决/解决此问题的方法或解决方法吗?

提前感谢您!

【问题讨论】:

  • 什么“重复”?我们在您的代码中唯一能看到的是fpTemp,这显然是一个局部变量。你不会在任何地方打电话给fopen。没有人可以仅使用给出的代码来回答这个问题。另请参阅why is while(!feof(fp)) always wrong。
  • 不要将 POSIX dup() 与 ANSI FILE * 一起使用。
  • @Lundin 我已经编辑了帖子...我已经添加了fopen。
  • @jdarthenay 如何备份/保存/存储在哪一行/位置我找到了标签&lt;item ...&gt;?

标签: c replace duplicates dup fdopen


【解决方案1】:

你不需要复制文件指针,你需要使用ftell()/fseek()。没有错误处理的小代码。 (所以请不要在没有通过检查返回添加错误处理的情况下复制它)。

FILE *f = fopen(f, "r");

// do various things with file
long where_am_i = ftell(f); // if it fails, -1 is returned and errno is set to indicate the error.

// Do stuff requiring moving cursor in f stream

fseek(f, SEEK_SET, where_am_i); // same returns convention as ftell()
// You moved cursor back, you can start reading again

另外,您似乎可以使用fgetpos() 和fsetpos()。

【讨论】:

  • 我已经使用fgetpos() 和fsetpos() 函数和fpos_t position 变量,但我不得不在fsetpos() 之后放置一个fseek(),因为fgets() 将文件指针放在下一行的开始。谢谢你的朋友!
  • @Federic Cuozzo 另外,我在您的问题中看到您可以在文件中读取和写入。如果不调用移动游标语句(例如 fseek(); 例如 fseek(f, SEEK_CUR, 0); 用于将游标移动到其当前位置(大声笑)就足够了),这不应该工作。
猜你喜欢
  • 2012-02-15
  • 2012-09-15
  • 1970-01-01
  • 2017-02-02
  • 1970-01-01
  • 2015-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多