1、第9章 文件 习题答案1. 下面的程序有什么问题?int main (void) int *fp; int k; fp = fopen (gelatin); for (k = 0; k 30; k+) fputs (fp, Nanette eats gelatin ); fclose (gelatin); return 0; 答:因为程序有文件定义,所以应该有#include 。应该把fp声明为文件指针FILE *fp;函数fopen()需要一种模式:fopen(“gelatin”,”w”)或者a模式。Fputs函数中的参数的次序应该反过来,为了清楚,输出字符串应该具有一个换行符,因为fput
2、s并不会自动添加它。Fclose函数需要一个文件指针而不是文件名:fclose(fp)。以下为正确的版本#include int main (void) FILE *fp;int k; fp = fopen (gelatin, w); /* 用亍创建可冐文件 gelatin 但赋值给 一个文件指针 */ for (k = 0; k 30; k+) /* 循环输出 */ fputs (Nanette eats gelatin n, fp); fclose(fp); /* 关闭文件 */ return 0; 2. 编写一个程序,将任意数目的字符串写入文件。字符串由键盘输入。#include #in
3、clude #include #include #define BUFFER_SIZE 50char *read_string(char *buffer, int *pbuffer_size);int main(void) FILE *pFile = NULL; char *filename = d:myfile.txt; char answer = n; size_t str_length = 0; int buffer_size = BUFFER_SIZE; char *buffer = malloc(buffer_size); pFile = fopen(filename, w); if
4、(!pFile) printf(Error opening %s for writing. Program terminated., filename); exit(1); do /* 从键盘输入字符串 */ printf(Enter a string:n); read_string(buffer, &buffer_size); str_length = strlen(buffer); fwrite(&str_length, sizeof(size_t), 1, pFile); fwrite(buffer, str_length, 1, pFile); printf(Do you want t
5、o enter another (y or n)? ); scanf( %c, &answer); fflush(stdin); while(tolower(answer) = y); fclose(pFile); printf(nFile write completen); if(buffer) free(buffer); return 0; /* 从键盘读取字符串 */* 如果字符串超过buffer文件大小 */* buffer文件自动增加 */char *read_string(char *buffer, int *pbuffer_size) char *temp = NULL; int
6、 position = 0; while(bufferposition+ = getchar() != n) if(position = *pbuffer_size-1) /* Increase the size of the buffer */ *pbuffer_size += BUFFER_SIZE; temp = (char*)malloc(*pbuffer_size); bufferposition = 0; strcpy(temp, buffer); free(buffer); buffer = temp; temp = NULL; bufferposition-1 = 0; ret
7、urn buffer;3. 编写一个程序,获取用户输入文件名,如存在,则写入从键盘输入的字符串;如不存在,先创建,再获取键盘输入后写入。#include#include main()FILE *fp;char filename30,str30;/*定义两个字符型数组*/printf(please input filename:n);scanf(%s,filename);/*输入文件名*/fp=fopen(filename,w+);printf(please input string:n);/*提示输入字符串*/getchar();gets(str);fputs(str,fp);/*将字符串写入
8、fp所指向的文件中*/fclose(fp);4. 编写一个文件查看器,它可以将文件显示为十六进制和字符方式。#include #include #include const int MAXLEN = 256; const int DISPLAY = 80; const int PAGE_LENGTH = 20; int main(int argc, char *argv) char filenameMAXLEN; FILE *pfile; unsigned char bufferDISPLAY/4 - 1; int count = 0; int lines = 0; int i; if(arg
9、c = 1) printf(Please enter a filename: ); fgets(filename, MAXLEN, stdin); int len = strlen(filename); if(filenamelen-1 = n) filenamelen-1 = 0; else strcpy(filename, argv1); if(!(pfile = fopen(filename, rb) printf(Sorry, cant open %s, filename); return -1; while(!feof(pfile) if(count sizeof buffer) b
10、uffercount+ = (unsigned char)fgetc(pfile); else for(count = 0; count sizeof buffer; count+) printf(%02X , buffercount); printf(| ); for(count = 0; count sizeof buffer; count+) printf(%c, isprint(buffercount) ? buffercount:.); printf(n); count = 0; if(!(+lines%PAGE_LENGTH) if(getchar()=E) return 0; for( i = 0; i sizeof buffer; i+) if(i count) printf(%02X , bufferi); else printf( ); printf(| ); for( i = 0; i count; i+) printf(%c,isprint(bufferi) ? bufferi:.); printf(n); fclose(pfile); return 0;