怀疑c中的文件... [英] Doubt on files in c...

查看:80
本文介绍了怀疑c中的文件...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
int main()
{
    FILE *fptr;char ch;
    char fname[15];
    puts("Enter file name with directory");
    gets(fname);
    fptr=fopen(fname,"r+");
    if(fptr==NULL)
    {
        puts("Not successful");
    }
    else{
            puts("Successfully opened");getch();
    while(1)
    {
        ch=fgetc(fptr);
        if(ch==EOF)
        {
            break;
        }
        if(ch==';')
        {
           fseek(fptr,-1,SEEK_CUR);
           fputc('$',fptr);
           fseek(fptr,0,SEEK_CUR);---------->//////[HERE]//////
        }
    }


    }
    puts("Replacement Completed successfully");
    fclose(fptr);
    return 0;
}



这个ia proagram用于替换文件中的字符与其他....如果我删除行[这里]这个proagram永远不会停止。但是这并没有让人产生影响......如fseek(fptr,0,SEEK_CUR);什么都不做..请帮帮我



我尝试过:



i trien in codeblocks和devcpp


This i a proagram in c to replace a character in file with other....if i remove the line [HERE] this proagram never stops..But this doesnt make anu sence..as fseek(fptr,0,SEEK_CUR); doesnot do anything..please help me out

What I have tried:

i trien in codeblocks and devcpp

推荐答案

',fptr);
fseek(fptr,0,SEEK_CUR); ----------> ////// [HERE] //////
}
}


}
puts(替换成功完成);
fclose(fptr);
返回0;
}
',fptr); fseek(fptr,0,SEEK_CUR);---------->//////[HERE]////// } } } puts("Replacement Completed successfully"); fclose(fptr); return 0; }



这个ia proagram用于替换文件中的字符与其他....如果我删除行[这里]这个proagram永远不会停止。但是这并没有让人产生影响......如fseek(fptr,0,SEEK_CUR);什么都不做..请帮帮我



我尝试过:



i trien in codeblocks and devcpp


This i a proagram in c to replace a character in file with other....if i remove the line [HERE] this proagram never stops..But this doesnt make anu sence..as fseek(fptr,0,SEEK_CUR); doesnot do anything..please help me out

What I have tried:

i trien in codeblocks and devcpp


这很有道理。

fseek 设置档案位置到选定的地方。

如果您阅读文档: fseek - C ++ Reference [ ^ ]非常清楚:

It makes perfect sense.
fseek sets the file position to the selected place.
And if you read the documentation: fseek - C++ Reference[^] it is very clear:
Quote:

成功调用此函数后,流的文件结束内部指示符将被清除,并且全部此流上以前调用ungetc的效果将被删除。

The end-of-file internal indicator of the stream is cleared after a successful call to this function, and all effects from previous calls to ungetc on this stream are dropped.



因此,如果使用该行,则下一次调用 fgetc 将不会返回EOF。


请参阅 fseek - C ++参考 [ ^ ]:

See fseek - C++ Reference[^]:
引用:

在打开以进行更新(读取+写入)的流上,对fseek的调用允许在读取和写入之间切换。

On streams open for update (read+write), a call to fseek allows to switch between reading and writing.

这意味着每当您更改IO的方向时您必须调用的流 fseek 。原因是有两个内部文件位置:一个用于读取,一个用于写入,但只有相应的一个用读或写更新。 fseek 将更新到上一次操作的位置。



在您的情况下 fputc('

That means whenever you change the IO direction of a stream you have to call fseek. The reason is that there are two internal file positions: One for reading and one for writing but only the corresponding one is updated with a read or write. fseek will update both to the position of the last operation.

In your case the fputc('


这篇关于怀疑c中的文件...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆