C PRIMER PLUS第六版 第十三章程式設計練習
阿新 • • 發佈:2019-01-23
複習題第五題程式碼
#include <stdio.h> #include <stdlib.h> #define BUF 256 int has_ch(char, char *); int main(int argc, char * argv []) { char ch; FILE * fp; char line[BUF]; if(argc != 3) { fprintf(stderr,"Could not open %s file", argv[0]); exit(EXIT_FAILURE); } if((fp = fopen(argv[2], "r")) == NULL) { fprintf(stderr,"Could not open %s file", argv[2]); exit(EXIT_FAILURE); } else { ch = argv[1][0]; while(fgets(line, BUF, fp) != NULL) { if(has_ch(ch, line)) fputs(line, stdout); } } fclose(fp); return 0; } int has_ch(char ch, char * ar) { while(*ar) { if(ch == *ar++) return 1; } return 0; }
程式設計練習:
1.
#include <stdio.h> #include <stdlib.h> #define SIZE 64 int main(void) { int ch; FILE *fp; unsigned long count = 0; char name[SIZE]; printf("Enter the name of the file : "); scanf("%s",name); if((fp = fopen(name, "r")) == NULL) { printf("Can't open %s\n",name); exit(EXIT_FAILURE); } while((ch = getc(fp)) != EOF) { putc(ch, stdout); count++; } fclose(fp); printf("\n"); printf("File %s has %lu characters.\n",name,count); return 0; }
2.
#include <stdio.h> #include <stdlib.h> #define COPY_SIZE 1024 void copy(FILE * , FILE *); int main(int argc, char * argv []) { FILE * sou, * tar; if(argc != 3) { fprintf(stderr, "Missing file or file out of boundary"); exit(EXIT_FAILURE); } else { if((sou = fopen(argv[1], "rb")) == NULL || (tar = fopen(argv[2], "wb")) == NULL) { fprintf(stderr, "File open failed."); exit(EXIT_FAILURE); } else { copy(sou, tar); } } fclose(sou); fclose(tar); puts("Done.\n"); return 0; } void copy(FILE * sou, FILE * tar) { int temp[COPY_SIZE]; fread(temp, sizeof(int), COPY_SIZE, sou); fwrite(temp, sizeof(int), COPY_SIZE, tar); }
3.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define SIZE 1024
#define NAME_SIZE 64
int main(void)
{
char temp[SIZE];
char name[NAME_SIZE];
char ch;
int i = 0;
FILE * sou;
printf("Enter the name of the file : ");
scanf("%s",name);
if((sou = fopen(name, "r")) == NULL)
{
fprintf(stderr, "Could not open %s file.\n",name);
exit(EXIT_FAILURE);
}
while((ch = getc(sou)) != EOF) //在讀到檔案結尾前把所有字母改為大寫
{
ch = toupper(ch);
temp[i] = ch;
i++;
}
fclose(sou); //先以讀的方式把檔案改為大寫並拷貝到字串陣列中臨時儲存,關閉檔案
if((sou = fopen(name, "w")) == NULL) //再以寫的方式開啟檔案,檔案原本內容被清空,但內容副本被儲存在字串陣列中
{
fprintf(stderr, "Could not open %s file.\n",name);
exit(EXIT_FAILURE);
}
fprintf(sou, "%s", temp); //再把字串陣列中的內容拷貝到原檔案中
fclose(sou);
puts("Done, your string has been updated.");
return 0;
}
4.
#include <stdio.h>
#include <stdlib.h>
#define SIZE 256
int main(int argc, char * argv [])
{
FILE *fp;
int i = 1;
char temp[SIZE];
while(i < argc)
{
if((fp = fopen(argv[i], "r")) == NULL)
{
fprintf(stderr, "Could not open the file %s", argv[i]);
exit(EXIT_FAILURE);
}
printf("%s : \n", argv[i]);
while(fgets(temp, SIZE, fp) != NULL); //fgets在遇到EOF時返回NULL,否則返回第一個引數地址
{
fputs(temp, stdout);
}
fclose(fp);
i++;
printf("\n");
}
printf("***Done!This is the end!***\n");
return 0;
}
5.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFSIZE 4096
void append(FILE *source, FILE *dest);
int main(int argc, char * argv [])
{
FILE *fa, *fs;
int files = 0;
int ch;
int i = 2;
if((fa = fopen(argv[1], "a+")) == NULL)
{
fprintf(stderr, "Can't open %s\n", argv[1]);
exit(EXIT_FAILURE);
}
if(setvbuf(fa, NULL, _IOFBF, BUFFSIZE) != 0)
{
fputs("Can't create output buffer\n",stderr);
exit(EXIT_FAILURE);
}
while(i < argc)
{
if(strcmp(argv[1], argv[i]) == 0)
fputs("Can't append file to itself.\n",stderr);
else if((fs = fopen(argv[i], "r")) == NULL)
fprintf(stderr, "Can't open %s\n", argv[i]);
else
{
if(setvbuf(fa, NULL, _IOFBF, BUFFSIZE) != 0)
{
fputs("Can't create input buffer\n", stderr);
i++;
continue;
}
append(fs, fa);
if(ferror(fs) != 0)
fprintf(stderr,"Error in reading file %s", argv[i]);
if(ferror(fa) != 0)
fprintf(stderr,"Error in writing file %s", argv[1]);
fclose(fs);
files++;
printf("File %s appended.\n", argv[i]);
}
i++;
}
printf("Done appending. %d files appended.\n", files);
rewind(fa);
printf("%s contents:\n", argv[1]);
while ((ch = getc(fa)) != EOF)
putchar(ch);
putchar('\n');
puts("Done displaying!\n");
fclose(fa);
return 0;
}
void append(FILE *source, FILE *dest)
{
static char temp[BUFFSIZE];
while (fgets(temp, BUFFSIZE, source) != NULL)
{
fputs(temp, dest);
}
}
6.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 40
int main(void)
{
FILE *in, *out;
int ch;
char sou_name[LEN] = {0};
char tar_name[LEN] = {0};
int count = 0;
printf("Enter the name of the source file : ");
scanf("%s", sou_name);
if((in = fopen(sou_name, "r")) == NULL)
{
fprintf(stderr, "I couldn't open the file \"%s\"\n");
exit(EXIT_FAILURE);
}
//設定輸出
strncpy(tar_name, sou_name, LEN-5);
tar_name[LEN-5] = '\0';
strcat(tar_name, ".red");
if((out = fopen(tar_name, "w")) == NULL)
{
fprintf(stderr, "Can't create output file.\n");
exit(3);
}
//拷貝資料
while((ch = getc(in)) != EOF)
{
if(count % 3 == 0)
putc(ch, out);
count++;
}
if(fclose(in) != 0 || fclose(out) != 0)
fprintf(stderr, "Error in closing files.\n");
return 0;
}
7.
a.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 256
int main(int argc, char * argv [])
{
FILE * file1, *file2;
int i = 1;
if(argc != 3)
{
fprintf(stderr,"Wrong number of files.\n");
exit(EXIT_FAILURE);
}
if((file1 = fopen(argv[1], "r")) == NULL)
{
fprintf(stderr,"Can't open file1.\n");
exit(EXIT_FAILURE);
}
if((file2 = fopen(argv[2], "r")) == NULL)
{
fprintf(stderr,"Can't open file2.\n");
exit(EXIT_FAILURE);
}
while(feof(file1) == 0 || feof(file2) == 0)
{
printf("This is the %d circle\n",i);
char line_temp1[LEN];
char line_temp2[LEN]; //定義兩個自動變數,在while迴圈一次後,自動釋放,然後再重新分配記憶體
if((fgets(line_temp1, LEN, file1)) != NULL)
fputs(line_temp1, stdout);
if((fgets(line_temp2, LEN, file2)) != NULL)
fputs(line_temp2, stdout);
i++;
}
fclose(file1);
fclose(file2);
return 0;
}
b.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 256
int main(int argc, char * argv [])
{
FILE * file1, *file2;
int i = 1;
if(argc != 3)
{
fprintf(stderr,"Wrong number of files.\n");
exit(EXIT_FAILURE);
}
if((file1 = fopen(argv[1], "r")) == NULL)
{
fprintf(stderr,"Can't open file1.\n");
exit(EXIT_FAILURE);
}
if((file2 = fopen(argv[2], "r")) == NULL)
{
fprintf(stderr,"Can't open file2.\n");
exit(EXIT_FAILURE);
}
while(feof(file1) == 0 || feof(file2) == 0)
{
int len;
printf("This is the %d circle\n",i);
char line_temp1[LEN];
char line_temp2[LEN]; //定義兩個自動變數,在while迴圈一次後,自動釋放,然後再重新分配記憶體
if((fgets(line_temp1, LEN, file1)) != NULL)
{
len = strlen(line_temp1);
line_temp1[len - 1] = '\0';
fputs(line_temp1, stdout);
}
if((fgets(line_temp2, LEN, file2)) != NULL)
fputs(line_temp2, stdout);
i++;
}
fclose(file1);
fclose(file2);
return 0;
}
8.
#include <stdio.h>
#include <stdlib.h>
int find_ch(char, FILE *);
int main(int argc, char * argv [])
{
char ch = argv[1][0];
int count = 0;
int i = 2;
FILE *fp;
if(argc < 2)
{
fprintf(stderr, "Missing arguments");
exit(EXIT_FAILURE);
}
else if(argc == 2)
{
count = find_ch(ch, stdin); //這種情況下,需要在stdin輸入後手動Control + Z來結束輸入,也可以用字串陣列來接手外部輸入
printf("In your input, there are %d %c%c.\n", count, ch, (count > 1)?'s':'\0');
}
else
{
while(i < argc)
{
if((fp = fopen(argv[i], "r")) == NULL)
{
fprintf(stderr, "Can't open the file.\n");
exit(EXIT_FAILURE);
}
count += find_ch(ch, fp);
i++;
}
printf("In the %d file%c, there are totally %d %c%c.\n", argc-2, (argc-2 > 1)?'s':'\0', count, ch,(count > 1)?'s':'\0' );
fclose(fp);
}
printf("Done.\n");
return 0;
}
int find_ch(char ch, FILE * fp)
{
char file_ch;
int ct = 0;
while((file_ch = getc(fp)) != EOF)
{
if(file_ch == ch)
{
ct++;
}
}
return ct;
}
9.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 41
int main(void)
{
FILE *fp;
char words[MAX];
int num = 1;
if((fp = fopen("wordy", "a+")) == NULL) //以在末尾新增內容模式開啟wordy
{
fprintf(stdout, "Can't open \"wordy\" file.\n");
exit(EXIT_FAILURE);
}
rewind(fp);
while(fgets(words, MAX, fp) != NULL)
num++;
puts("Enter words to add to the file; press the #");
puts("Key at the beginning of a line to terminate.");
while((fscanf(stdin, "%40s", words)) == 1 && (words[0] != '#'))
fprintf(fp, "%d : %s\n", num++, words);
puts("File contents:");
rewind(fp);
while(fgets(words, MAX, fp) != NULL)
fputs(words, stdout);
puts("Done!");
if(fclose(fp) != 0)
fprintf(stderr,"Error closing file\n");
return 0;
}
10.
#include <stdio.h>
#include <stdlib.h>
#define SIZE 64
int main(void)
{
long int dis;
char ch;
char name[SIZE];
FILE *fp;
printf("Enter the name of the file you want to open : ");
scanf("%s",name);
if((fp = fopen(name, "r")) == NULL)
{
fprintf(stderr, "Can't open this file.\n");
exit(EXIT_FAILURE);
}
printf("Enter the location of file, we will print from that location :");
scanf("%ld",&dis);
fseek(fp, dis, SEEK_SET);
while((ch = getc(fp)) != '\n')
{
putchar(ch);
}
return 0;
}
11.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 256
int main(int argc, char * argv [])
{
char compare[SIZE];
FILE *fp;
if(argc != 3)
{
fprintf(stderr, "Wrong cmd parameters.\n");
exit(EXIT_FAILURE);
}
if((fp = fopen(argv[2], "r")) == NULL)
{
fprintf(stderr, "Can't open the file.\n");
exit(EXIT_FAILURE);
}
while(fgets(compare, SIZE, fp) != NULL)
{
if(strstr(compare, argv[1])) //strstr(str1,str2)是在str1中查詢str2出現的位置,如果存在,返回第一次出現的位置,否則返回NULL
{
fputs(compare, stdout);
}
}
fclose(fp);
return 0;
}
12.
文件不好輸入,太多了,先寫一個程式生成隨機數的20*30的檔案,如下:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
FILE *fp;
int sou_ar[60];
int i;
int j = 0;
srand((unsigned int)time(0));
fp = fopen("wordy", "w");
while(j < 20)
{
for(i = 0; i < 59; i = i+2)
{
sou_ar[i] = rand() % 10;
sou_ar[i + 1] = ' ';
}
sou_ar[59] = '\0';
for(i = 0; i < 59; i += 2)
{
fprintf(fp, "%d ", sou_ar[i]);
}
fprintf(fp,"\n");
j++;
}
fclose(fp);
return 0;
}