user-0 提问: fscanf问题！

回复:
user: user-1
content: http://blog.csdn.net/liu1164316159/article/details/10300343

user: user-0
content: 引用 5 楼 zhao4zhong1 的回复:为什么不使用ftell函数呢？  想了解下文件指针指向的问题！求赵4老师 科普下！文件指针不是对文件调用一次 文件指针才++的吗？

user: user-2
content: 为什么不使用ftell函数呢？

user: user-3
content: 建议楼主去了解一下 全缓冲 行缓冲 不带缓冲

user: user-0
content: 引用 2 楼 visp 的回复://  为什么多了这条fscanf程序没出错，但是上面的gets输入就白输了（输入时无错误回车正常结束），打开文件发现完全没变化， 加这条fscanf是会出错的，因为有缓冲的原因，数据没有写到文件里。但是数据指针已经到了最后，但是最后的数据又没有在文件里，所以fscanf就读不到数据，所以程序就出错了。所以你看到的文件就没有变量。 你在fprintf(fp, "%s", max);后加一个fflush(fp);就可以体会到了。 fprintf(fp, "%s", max)执行后，并没有把max的内容写到文件里。比如max有三个字符，这时文件指针却指到了这3个字符的位置。但是fscanf却读不到。 //好好体会下缓冲，和文件指针的位置。fprintf(fp, "%s", max)之后，加下面两句中的一条，你的程序都是对的。 fflush(fp); rewind(fp);   #include <stdio.h> #include <stdlib.h> int main() { int i = 0; FILE *fp; char max[50] = { 0 }; if((fp = fopen("kiss.txt", "a+"))==NULL) { printf("打开错误\n"); } printf("Re Input: "); gets(max); fprintf(fp, "%s", max); //好好体会下缓冲，和文件指针的位置。下面两句有一条，程序都是对的。不刷新的话，就把指针指到文件开头 fflush(fp); //rewind(fp); fscanf(fp,"%s",max); puts(max);   puts("**********"); fclose(fp);                      return 0; }    文件指针移动不是要对数据进行一次处理的时候文件指针才会指向下一个字节码？ fprintf(fp, "%s", max); 运行一次也会让文件指针++？那到底我运行时是++呢？还是+整个strlen（max）的字节？

user: user-2
content: ftell Gets the current position of a file pointer.  long ftell( FILE *stream );  Function Required Header Optional Headers Compatibility  ftell <stdio.h> <errno.h> ANSI, Win 95, Win NT    For additional compatibility information, see Compatibility in the Introduction.  Libraries  LIBC.LIB Single thread static library, retail version  LIBCMT.LIB Multithread static library, retail version  MSVCRT.LIB Import library for MSVCRT.DLL, retail version    Return Value  ftell returns the current file position. The value returned by ftell may not reflect the physical byte offset for streams opened in text mode, because text mode causes carriage return–linefeed translation. Use ftell with fseek to return to file locations correctly. On error, ftell returns –1L and errno is set to one of two constants, defined in ERRNO.H. The EBADF constant means the stream argument is not a valid file-handle value or does not refer to an open file. EINVAL means an invalid stream argument was passed to the function. On devices incapable of seeking (such as terminals and printers), or when stream does not refer to an open file, the return value is undefined.  Parameter  stream  Target FILE structure  Remarks  The ftell function gets the current position of the file pointer (if any) associated with stream. The position is expressed as an offset relative to the beginning of the stream.  Note that when a file is opened for appending data, the current file position is determined by the last I/O operation, not by where the next write would occur. For example, if a file is opened for an append and the last operation was a read, the file position is the point where the next read operation would start, not where the next write would start. (When a file is opened for appending, the file position is moved to end of file before any write operation.) If no I/O operation has yet occurred on a file opened for appending, the file position is the beginning of the file.  In text mode, CTRL+Z is interpreted as an end-of-file character on input. In files opened for reading/writing, fopen and all related routines check for a CTRL+Z at the end of the file and remove it if possible. This is done because using ftell and fseek to move within a file that ends with a CTRL+Z may cause ftell to behave improperly near the end of the file.   Example  /* FTELL.C: This program opens a file named FTELL.C  * for reading and tries to read 100 characters. It  * then uses ftell to determine the position of the  * file pointer and displays this position.  */  #include <stdio.h>  FILE *stream;  void main( void ) {    long position;    char list[100];    if( (stream = fopen( "ftell.c", "rb" )) != NULL )    {       /* Move the pointer by reading data: */       fread( list, sizeof( char ), 100, stream );       /* Get position after read: */       position = ftell( stream );       printf( "Position after trying to read 100 bytes: %ld\n",               position );       fclose( stream );    } }   Output  Position after trying to read 100 bytes: 100   Stream I/O Routines  See Also   fgetpos, fseek, _lseek, _tell

user: user-2
content: 不要把 fopen("...","...");fscanf,fprintf,fgets,fgetc,fclose //读时把\r\n替换成\n，写时把\n替换成\r\n;读到\x1a就设置EOF;读写的内容当字符看待 和 fopen("...","...b");fseek,ftell,fread,fwrite,fgetc,fclose //不作以上替换，遇到\x1a仍继续读;读写的内容当字节看待 弄混了

user: user-4
content: //  为什么多了这条fscanf程序没出错，但是上面的gets输入就白输了（输入时无错误回车正常结束），打开文件发现完全没变化， 加这条fscanf是会出错的，因为有缓冲的原因，数据没有写到文件里。但是数据指针已经到了最后，但是最后的数据又没有在文件里，所以fscanf就读不到数据，所以程序就出错了。所以你看到的文件就没有变量。 你在fprintf(fp, "%s", max);后加一个fflush(fp);就可以体会到了。 fprintf(fp, "%s", max)执行后，并没有把max的内容写到文件里。比如max有三个字符，这时文件指针却指到了这3个字符的位置。但是fscanf却读不到。 //好好体会下缓冲，和文件指针的位置。fprintf(fp, "%s", max)之后，加下面两句中的一条，你的程序都是对的。 fflush(fp); rewind(fp);   #include <stdio.h> #include <stdlib.h> int main() { int i = 0; FILE *fp; char max[50] = { 0 }; if((fp = fopen("kiss.txt", "a+"))==NULL) { printf("打开错误\n"); } printf("Re Input: "); gets(max); fprintf(fp, "%s", max); //好好体会下缓冲，和文件指针的位置。下面两句有一条，程序都是对的。不刷新的话，就把指针指到文件开头 fflush(fp); //rewind(fp); fscanf(fp,"%s",max); puts(max);   puts("**********"); fclose(fp);                      return 0; }

user: user-0
content: 引用 7 楼 paschen 的回复:http://blog.csdn.net/liu1164316159/article/details/10300343  感谢版主！

user: user-5
content: 在程序中给max一个初值试试~