日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

《C语言及程序设计》程序阅读——文件操作

發布時間:2025/4/5 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 《C语言及程序设计》程序阅读——文件操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

返回:賀老師課程教學鏈接

1、閱讀下面的程序,寫出輸出結果,并上機運行程序進行對照

#include "stdio.h" int main() {FILE *outfile,*infile;outfile=fopen("data.dat","w");fprintf(outfile, "1111111111\n");fprintf(outfile, "aaaaaaaaaa\n");fprintf(outfile,"AAAAAAAAAA\n");fprintf(outfile,"**********\n");fclose(outfile);infile=fopen("data.dat","r");char line[80];int i=0;while(!feof(infile)){i++;fgets(line,sizeof(line), infile);printf("%d: %s", i, line);}fclose(infile);return 0; }

2、區分ASCII文件和二進制文件:閱讀并運行下面的兩個程序,分別用記事本和二進制文件閱讀器(請自行下載Binary Viewer等程序,或者用DOS中的Debug程序,并百度其用法)。查看其內容,并理解文件存儲的原理。
(1)

#include <stdio.h> #include <stdlib.h> int main() {FILE *outfile;int a;outfile=fopen("f1.dat","w");if(!outfile){printf("open error!\n");exit(1);}scanf("%d", &a);fprintf(outfile, "%d", a);fclose(outfile);return 0; }

(2)

#include <stdio.h> #include <stdlib.h> int main() {FILE *outfile;int a;outfile=fopen("f2.dat","wb");if(!outfile){printf("open error!\n");exit(1);}scanf("%d", &a);fwrite(&a, sizeof(int), 1, outfile);fclose(outfile);return 0; }

3、閱讀下面的程序,指出其功能,體會fseek()等與文件指針相關的函數的功能及其用法
(1)請說出程序的輸出

#include <stdio.h> #include <stdlib.h> char *filename = "a.txt"; int main() {FILE *file;long l,m;file=fopen(filename,"rb");if(!file){printf("open error!\n");exit(1);}l = ftell(file); //ftell函數返回文件位置指針相對于文件首的偏移量fseek(file, 0, SEEK_END);m = ftell(file);fclose(file);printf("size of %s is %ld bytes.\n", filename, (m-l));return 0; }

(2)請說出運行程序后test.txt中的內容

#include <stdio.h> #include <stdlib.h> int main() {FILE *outfile;long pos;outfile=fopen("test.txt","w");if(!outfile){printf("open error!\n");exit(1);}fwrite ("This is an apple",16, 1, outfile);pos=ftell(outfile);fseek(outfile, pos-7, SEEK_SET);fwrite (" sam", 4, 1, outfile);fclose(outfile);return 0; }

總結

以上是生活随笔為你收集整理的《C语言及程序设计》程序阅读——文件操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。