在同一位置复制文件指针

时间:2016-04-08 10:53:32

标签: c replace duplicates dup fdopen

Hy大家, 我有这段代码:

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()

我的想法是复制文件指针fp(在原型中传递),如果我找到标记<item ...>,因为,如果此标记链接到我的服务名称&# 39;我想删除,我已经替换了整个<item ...>字符串。

但是,我有一个问题......当我执行snprintf()fputs()时,文件会在文件开头被覆盖,因为,imho,我认为文件指针不是很复制。

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

提前谢谢!

1 个答案:

答案 0 :(得分: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()