c语言如何读取文件并输出
1、C语言标准库提供了一系列文件操作函数。
文件操作函数一般以f+单词的形式来命名(f是file的简写),其声明位于stdio.h头文件当中。例如:fopen、fclose函数用于文件打开与关闭;fscanf、fgets函数用于文件读取;fprintf、fputs函数用于文件写入;ftell、fseek函数用于文件操作位置的获取与设置。
2、例程: #include
//我认为你的应该为指针异常,没有数据。 下面有个例子可以参考:#include #include #include using namespace std;void main(){_mkdir("D:\example");//只能创建一级目录char* path="D:\example\test。 txt";/*写文件*/ofstream file;char ch;int total=0;file。 open(path);if(!file){cerrexit(1); } const char buf[] = "ABC";file。 write(buf, sizeof(buf));/*读文件*/ifstream ifile;ifile。 open(path,ofstream::in|ofstream::out);if(!ifile){cerrexit(1); }while(ifile。 get(ch)){ couttotal ;}coutfile。 close();}。 如果要求就这么简单的输出的话。不知道如下结果如何,希望对你有帮助 #include <stdio.h> void main() { FILE *fp; fp = fopen("xiongshuai.txt","r");//此处第一个参数,可以修给成你需要的文本文档绝对路径 //并且需要用双反斜杠如 C:\\document and settings\\xiongshuai.txt char ch; while((ch = fgetc(fp)) != EOF) { putchar(ch); } } 1、C语言标准库提供了一系列文件操作函数。 文件操作函数一般以f+单词的形式来命名(f是file的简写),其声明位于stdio.h头文件当中。例如:fopen、fclose函数用于文件打开与关闭;fscanf、fgets函数用于文件读取;fprintf、fputs函数用于文件写入;ftell、fseek函数用于文件操作位置的获取与设置。 2、例程: #include
你好,很高兴为您解答。 我来回答吧: 首先是两个输入文件: 一个文本文件:scoret。txt 一个二进制文件:scoreb。 txt 文本文件scoret。txt我们可以直接编辑(按要求编辑五个学生的姓名和成绩),而二进制文件不易编辑。 我们先写一个代码,实再将scoret。txt文件转成二进制文件scoreb。 txt #include #include #include struct stu { char name[30]; int grade; }; int main() { int i,n; struct stu s[10]; FILE *fpIt,*fpIb; /*输入*/ FILE *fpOt,*fpOb; /*输出*/ fpIt=fopen("scoret。 txt","r"); fpIb=fopen("scoreb。 txt","wb"); if (fpIt==NULL || fpIb==NULL) { printf("File Open Error:\npress any key to exit:\n"); getch(); return -1; } i=0; while (!feof(fpIt)) { fgets(s[i]。 name,30,fpIt); /*有空格只能这样输入*/ fscanf(fpIt,"%d\n",&s[i]。 grade); /*文本输入*/ printf("i=%d,name=%s\t grade=%d\n",i,s[i]。 name,s[i]。 grade); getch(); fwrite(&s[i],sizeof(struct stu),1,fpIb); i++; } n=i; printf("n=%d\n",n); fclose(fpIt); fclose(fpIb); getch(); return 0; } =============== 上面的代码在TC或VC下编辑运行,就可以生成scoreb。 txt文件。 这时scoreb。txt的学生信息与scoret。 txt是一样的。 接着就要修改一下scoret。 txt文件的内容,重新编辑5个学生的信息给scoreb。txt 这样使得scoret。 txt与scoreb。txt分别各有五个学生的信息。 当然你如果有原始文件的话,上面都是浮云。 下面是你这个问题的重点: #include #include #include #include struct stu { char name[30]; int grade; }; int main() { int i,j,n; struct stu s[10],temp; FILE *fpIt,*fpIb; /*输入*/ FILE *fpOt,*fpOb; /*输出*/ fpIt=fopen("scoret。 txt","r"); fpIb=fopen("scoreb。txt","rb"); fpOt=fopen("scoreOt。 txt","w"); fpOb=fopen("scoreOb。txt","wb"); if (fpIt==NULL || fpIb==NULL || fpOt==NULL || fpOb==NULL) { printf("File Open Error:\npress any key to exit:\n"); getch(); return -1; } n=0; while (!feof(fpIt)) { fgets(s[n]。 name,30,fpIt); /*有空格只能这样输入*/ fscanf(fpIt,"%d\n",&s[n]。grade); /*文本输入*/ printf("n=%d,name=%s\t grade=%d\n",n,s[n]。 name,s[n]。grade); getch(); n++; } fclose(fpIt); printf("===========\n"); while (!feof(fpIb) && n name,s[n]。 grade); getch(); n++; } printf("n=%d\n",n); fclose(fpIb); /*排序*/ for (i=0;i grade grade,s[i]。name); /*写文件*/ for (i=0;i 当然二进制文件你如果用记事本打开的话,一般是乱码。 你可以弄一个UltraEdit之类的文本编辑器,查看其二进制代码。 转载请注明出处51数据库 » c语言读写word文档 怎么正确地用C语言读写文本文件?把"ABC"写到文件中我刚学C 爱
c语言读取文本文件
c语言如何读取文件并输出
c文件的写入和读取-C语言中怎样读取文件数据并对数据排序再重新写