fscanf函数使用问题
- C/C++ code
/* addaword.c -- uses fprintf(), fscanf(), and rewind() *//* 文件测试程序 */#include <stdio.h>#include <stdlib.h>#define MAX 100int main(void){ FILE *fp; char words[MAX]; if ((fp = fopen("words", "a+")) == NULL) //不能打开文件就返回NULL { fprintf(stdout, "Can't open \"words\" file\n"); exit(1); } printf("Input words:\n"); //如果在一行开头键入回车,即word[0]='\0',终止循环 while (gets(words)!=NULL && words[0]!='\0') { fprintf(fp, "%s ", words); } puts("File contents:"); //文件内容 rewind(fp); //rewind()命令使程序回到文件开始处,这样 //最后的while循环就可以打印文件的内容 while (fscanf(fp, "%s", words) == 1) { puts(words); } if (fclose(fp) != 0) { fprintf(stderr, "Error closing file\n"); } return 0;}其中这一行,while (fscanf(fp, "%s", words) == 1)
我把fp换成stdin程序就出现问题了,请问用文件指针和标准输入指针有何不同
[解决办法]
[解决办法]
fscanf, fwscanf
Read formatted data from a stream.
int fscanf( FILE *stream, const char *format [, argument ]... );
int fwscanf( FILE *stream, const wchar_t *format [, argument ]... );
Function Required Header Compatibility
fscanf <stdio.h> ANSI, Win 95, Win NT
fwscanf <stdio.h> or <wchar.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
Each of these functions returns the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. If an error occurs, or if the end of the file stream is reached before the first conversion, the return value is EOF for fscanf or WEOF for fwscanf.
Parameters
stream
Pointer to FILE structure
format
Format-control string
argument
Optional arguments
Remarks
The fscanf function reads data from the current position of stream into the locations given by argument (if any). Each argument must be a pointer to a variable of a type that corresponds to a type specifier in format. format controls the interpretation of the input fields and has the same form and function as the format argument for scanf; see scanf for a description of format. If copying takes place between strings that overlap, the behavior is undefined.
fwscanf is a wide-character version of fscanf; the format argument to fwscanf is a wide-character string. These functions behave identically otherwise.
Generic-Text Routine Mappings
TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_ftscanf fscanf fscanf fwscanf
For more information, see Format Specification Fields scanf functions and wscanf Functions.
Example
/* FSCANF.C: This program writes formatted
* data to a file. It then uses fscanf to
* read the various data back from the file.
*/
#include <stdio.h>
FILE *stream;
void main( void )
{
long l;
float fp;
char s[81];
char c;
stream = fopen( "fscanf.out", "w+" );
if( stream == NULL )
printf( "The file fscanf.out was not opened\n" );
else
{
fprintf( stream, "%s %ld %f%c", "a-string",
65000, 3.14159, 'x' );
/* Set pointer to beginning of file: */
fseek( stream, 0L, SEEK_SET );
/* Read data back from file: */
fscanf( stream, "%s", s );
fscanf( stream, "%ld", &l );
fscanf( stream, "%f", &fp );
fscanf( stream, "%c", &c );
/* Output data read: */
printf( "%s\n", s );
printf( "%ld\n", l );
printf( "%f\n", fp );
printf( "%c\n", c );
fclose( stream );
}
}
Output
a-string
65000
3.141590
x
Stream I/O Routines
See Also _cscanf, fprintf, scanf, sscanf
[解决办法]
可能是的,我用这个比较多:
freopen("in.txt", "r", stdin);把内容读到输入缓冲区,然后用 读取函数去缓冲区读数据,
如:scanf(),getchar()这些
其它的应该也是的。。。
[解决办法]
[解决办法]
fp指针指向文件的吧!标准指针不是指向文件的吧!!