日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

發布時間:2025/4/5 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 《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语言及程序设计》程序阅读——文件操作的全部內容,希望文章能夠幫你解決所遇到的問題。

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