C语言fscanf的一个问题
#include <stdio.h>
#include <stdlib.h>
#define MAX 40
int main(void)
{
FILE *fp;
char words[MAX];
char ch ;
int len ;
if ((fp = fopen("wordy", "a+")) == NULL)
{
fprintf(stdout,"Can't open \"words\" file.\n");
exit(1);
}
puts("Enter words to add to the file; press the Enter");
puts("key at the beginning of a line to terminate.");
while (gets(words) != NULL && words[0] != '\0')
{
fprintf(fp, "%s ", words);
}
puts("File contents:");
rewind(fp);
while (fscanf(fp,"%s",words) == 1)
{
puts(words);
}
if (fclose(fp) != 0)
fprintf(stderr,"Error closing file\n");
return 0;
}
相关推荐
FSCANF.C
C标准库源码
C语言中的fscanf()函数与vfscanf()函数使用
主要介绍了C语言中的fscanf()函数与vfscanf()函数使用,是C语言入门学习中的基础知识,需要的朋友可以参考下
fscanf_s()使用解析[项目代码]
本文详细介绍了fscanf_s()函数的使用方法,通过示例代码展示了如何正确读取文件中的数据。文章指出,fscanf_s()函数在设置长度参数时存在一些限制,不指定长度参数或仅设置一个长度参数均可正常使用,而为每个数据都指定长度参数反而会导致错误。相比之下,fscanf()函数更为直接。文章还提供了具体的代码示例,帮助读者更好地理解和使用fscanf_s()函数。
使用C语言实现,随机读写文件,资源为源代码
c语言,输入若干学生信息,保存到指定磁盘文件中,要求将奇数条学生信息从磁盘读入并显示到屏幕上。
当fscanf()遇到空格怎么办?
当fscanf()遇到空格怎么办? 以下就是一个例子 #include #include int main() { FILE *fpw=fopen("data1.txt","w"); if(fpw==NULL) { return 0; } char s[20]; gets(s); fprintf(fpw,"%s",s); fclose(fpw); FILE *fpr=fopen("data1.txt","r"); if(fpr==NULL) { return 0; } fscanf(fpr,"%s",s); puts(s); getchar(); fclose(fpr); return 0; } 输入: big baby 回车 输出 :big


