关于文件指针的问题
我有点搞不懂为什么操作文件指针的时候不用写个 fp++ 呢?
while(ch!=EOF)
{
ch=fgetc(fp);
if(ch== '{ ')count1++;
if(ch== '} ')count2++;
}
为什么不是这样
while(ch!=EOF)
{
ch=fgetc(fp);
if(ch== '{ ')count1++;
if(ch== '} ')count2++;
fp++;
}
[解决办法]
fgetc(fp)会自己移动文件指针
[解决办法]
fp是一个结构体状态的文件指针, 定义为:
struct _iobuf {
char *_ptr;
int _cnt;
char *_base;
int _flag;
int _file;
int _charbuf;
int _bufsiz;
char *_tmpfname;
};
你读一个字符, 文件指针自动会下移一个
[解决办法]
在函数fgetc中已经帮你自动调整文件指针的位置了。
另外FILE结构如下:
struct _iobuf {
char *_ptr;
int _cnt;
char *_base;
int _flag;
int _file;
int _charbuf;
int _bufsiz;
char *_tmpfname;
};
typedef struct _iobuf FILE;
FILE *fp;
fp++; //能达到你原想的目的吗
要手动改变文件指针 我觉得也应该这样
fseek(fp,1L,SEEK_CUR);
[解决办法]
首先你對指針的理解有寫問題。其次文件的概念也需要再學習鞏固一下。
[解决办法]
msdn
Each of these functions reads a single character from the current position of the file associated with stream. The function then increments the associated file pointer (if defined) to point to the next character. If the stream is at end of file, the end-of-file indicator for the stream is set.