1.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 256
char *s_gets(char arr[], int n);
int main(int argc, char *argv[])
{
int ch;
FILE *fp;
char arr[SIZE];
unsigned long count = 0;
puts("Please input filename: ");
s_gets(arr, SIZE);
if((fp = fopen(arr,"r")) == NULL)
{
printf("Can't open %s\n",argv[1]);
exit(EXIT_FAILURE);
}
while((ch = getc(fp)) != EOF)
{
putc(ch,stdout);
count++;
}
putchar('\n');
fclose(fp);
printf("File %s has %lu characters\n",arr,count);
system("pause");
return 0;
}
char *s_gets(char arr[], int n)
{
char *ret1;
ret1 = fgets(arr,n,stdin);
if(ret1)
{
char * ret2;
ret2 = strchr(arr, '\n');
if(ret2)
{
*ret2 = '\0';
}
else
{
while(getchar() != '\n')
continue;
}
}
return ret1;
}
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 512
int main(int argc, char *argv[])
{
size_t ch;
FILE *fp1;
FILE *fp2;
char buff[SIZE];
if((fp1 = fopen(argv[1],"rb")) == NULL)
{
printf("Can't open %s\n",argv[1]);
exit(EXIT_FAILURE);
}
if((fp2 = fopen(argv[2],"wb")) == NULL)
{
printf("Can't open %s\n",argv[2]);
exit(EXIT_FAILURE);
}
while((ch = fread(buff, sizeof(char), SIZE, fp1)) >0)
{
fwrite(buff, sizeof(char), ch, fp2);
}
if(fclose(fp1) != 0)
{
printf("Can't close %s it", argv[1]);
}
if(fclose(fp2) != 0)
{
printf("Can't close %s it", argv[2]);
}
return 0;
}
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define SIZE 512
char *s_gets(char arr[], int n);
int main(int argc, char *argv[])
{
char ch;
FILE *fp;
char buff[SIZE];
long last, count;
puts("Please input the file's name: ");
s_gets(buff, SIZE);
if((fp = fopen(buff,"r+")) == NULL)
{
printf("Can't open %s\n",buff);
exit(EXIT_FAILURE);
}
last = ftell(fp);
while((ch = getc(fp)) != EOF)
{
fseek(fp, -1L, SEEK_CUR);
ch = toupper(ch);
putc(ch, fp);
fseek(fp, 0, SEEK_CUR);
}
if(fclose(fp) != 0)
{
printf("Can't close %s it", argv[1]);
}
return 0;
}
char *s_gets(char arr[], int n)
{
char *ret1;
ret1 = fgets(arr,n,stdin);
if(ret1)
{
char * ret2;
ret2 = strchr(arr, '\n');
if(ret2)
{
*ret2 = '\0';
}
else
{
while(getchar() != '\n')
continue;
}
}
return ret1;
}
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define SIZE 512
int main(int argc, char *argv[])
{
char ch;
FILE *fp;
for(int i = 1; i < argc ; i++)
{
if((fp = fopen(argv[i],"r")) == NULL)
{
printf("Can't open %s\n",argv[i]);
exit(EXIT_FAILURE);
}
while((ch = getc(fp)) != EOF)
{
putc(ch, stdout);
}
if(fclose(fp) != 0)
{
printf("Can't close %s it", argv[i]);
}
}
return 0;
}
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#define BUFSIZE 4096
void append(FILE *source, FILE *dest);
int main(int argc, char **argv)
{
FILE *fa, *fs;
int files = 0;
int ch;
if((fa = fopen(argv[1],"a+")) == NULL)
{
fprintf(stderr,"Can't open %s\n",argv[1]);
exit(EXIT_FAILURE);
}
if(setvbuf(fa,NULL,_IOFBF,BUFSIZE) != 0)
{
fputs("Can't create output buffer\n",stderr);
exit(EXIT_FAILURE);
}
for( int i = 2; i < argc; i++)
{
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,"Cann't open %s\n",argv[i]);
else
{
if(setvbuf(fs,NULL,_IOFBF,BUFSIZE) != 0)
{
fputs("Can't create inupt buffer\n",stderr);
continue;
}
append(fs,fa);
if(ferror(fs) != 0)
fprintf(stderr,"Error in reading file %s.\n",argv[i]);
if(ferror(fa) != 0)
fprintf(stderr,"Error in writing file %s.\n",argv[1]);
fclose(fs);
files++;
printf("File %s appended.\n",argv[i]);
}
}
printf("Done appending. %d files appended.\n",files);
rewind(fa);
printf("%s contents:\n",argv[1]);
while((ch = getc(fa)) != EOF)
putchar(ch);
puts("\nDone displaying.");
fclose(fa);
system("pause");
return 0;
}
void append(FILE *source, FILE *dest)
{
size_t bytes;
static char temp[BUFSIZE];
while((bytes = fread(temp,sizeof(char),BUFSIZE,source)) > 0)
fwrite(temp,sizeof(char),bytes,dest);
}
char * s_gets(char *st,int n)
{
char * ret_val;
char * find;
ret_val = fgets(st,n,stdin);
if(ret_val)
{
find = strchr(st,'\n');
if(find)
*find = '\0';
else
{
while(getchar() != '\n')
continue;
}
}
return ret_val;
}
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#define LEN 40
char *s_gets(char arr[], int n);
int main(int argc, char *argv[])
{
FILE *in, *out;
int ch;
char name[LEN];
int count = 0;
printf("Please input file's name: ");
s_gets(name, LEN - 8);
if((in = fopen(name,"r")) == NULL)
{
fprintf(stderr,"I couldn,t open the file \"%s\"\n",name);
exit(EXIT_FAILURE);
}
strcat(name,"red.txt");
if((out = fopen(name,"w")) == NULL)
{
fprintf(stderr,"Can't creat output file.\n");
exit(3);
}
while((ch = getc(in)) != EOF)
if(count++ % 3 == 0)
putc(ch,out);
if(fclose(in) != 0 || fclose(out) != 0)
fprintf(stderr,"Error in closing files\n");
system("pause");
return 0;
}
char * s_gets(char *st,int n)
{
char * ret_val;
char * find;
ret_val = fgets(st,n,stdin);
if(ret_val)
{
find = strchr(st,'\n');
if(find)
*find = '\0';
else
{
while(getchar() != '\n')
continue;
}
}
return ret_val;
}
7.a
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#define LEN 40
char *s_gets(char arr[], int n);
int main(int argc, char *argv[])
{
FILE *fp1, *fp2;
int ch1;
int ch2;
char name1[LEN];
char name2[LEN];
int count = 0;
printf("Please input first file's name: ");
s_gets(name1, LEN);
if((fp1 = fopen(name1,"r")) == NULL)
{
fprintf(stderr,"I couldn,t open the file \"%s\"\n",name1);
exit(EXIT_FAILURE);
}
printf("Please input second file's name: ");
s_gets(name2, LEN);
if((fp2 = fopen(name2,"r")) == NULL)
{
fprintf(stderr,"I couldn,t open the file \"%s\"\n",name2);
exit(EXIT_FAILURE);
}
ch1 = getc(fp1);
ch2 = getc(fp2);
while(ch1 != EOF || ch2 != EOF)
{
while(ch1 != EOF && ch1 != '\n')
{
putchar(ch1);
ch1 = getc(fp1);
}
if(ch1 == '\n')
{
putchar(ch1);
ch1 = getc(fp1);
}
while(ch2 != EOF && ch2 != '\n')
{
putchar(ch2);
ch2 = getc(fp2);
}
if(ch2 == '\n')
{
putchar(ch2);
ch2 = getc(fp2);
}
}
if(fclose(fp1) != 0 || fclose(fp2) != 0)
fprintf(stderr,"Error in closing files\n");
return 0;
}
char * s_gets(char *st,int n)
{
char * ret_val;
char * find;
ret_val = fgets(st,n,stdin);
if(ret_val)
{
find = strchr(st,'\n');
if(find)
*find = '\0';
else
{
while(getchar() != '\n')
continue;
}
}
return ret_val;
}
7.b
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#define LEN 40
char *s_gets(char arr[], int n);
int main(int argc, char *argv[])
{
FILE *fp1, *fp2;
int ch1;
int ch2;
char name1[LEN];
char name2[LEN];
int count = 0;
printf("Please input first file's name: ");
s_gets(name1, LEN);
if((fp1 = fopen(name1,"r")) == NULL)
{
fprintf(stderr,"I couldn,t open the file \"%s\"\n",name1);
exit(EXIT_FAILURE);
}
printf("Please input second file's name: ");
s_gets(name2, LEN);
if((fp2 = fopen(name2,"r")) == NULL)
{
fprintf(stderr,"I couldn,t open the file \"%s\"\n",name2);
exit(EXIT_FAILURE);
}
ch1 = getc(fp1);
ch2 = getc(fp2);
while(ch1 != EOF || ch2 != EOF)
{
while(ch1 != EOF && ch1 != '\n')
{
putchar(ch1);
ch1 = getc(fp1);
}
if(ch1 == '\n')
{
if(ch2 == EOF)
{
putchar('\n');
}
else
{
putchar(' ');
}
ch1 = getc(fp1);
}
while(ch2 != EOF && ch2 != '\n')
{
putchar(ch2);
ch2 = getc(fp2);
}
if(ch2 == '\n')
{
putchar(ch2);
ch2 = getc(fp2);
}
}
if(fclose(fp1) != 0 || fclose(fp2) != 0)
fprintf(stderr,"Error in closing files\n");
return 0;
}
char * s_gets(char *st,int n)
{
char * ret_val;
char * find;
ret_val = fgets(st,n,stdin);
if(ret_val)
{
find = strchr(st,'\n');
if(find)
*find = '\0';
else
{
while(getchar() != '\n')
continue;
}
}
return ret_val;
}
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#define MAX 41
int main(int argc, char *argv[])
{
FILE *fp;
char words[MAX];
int count;
if((fp = fopen("wordy.txt","a+")) == NULL)
{
fprintf(stdout,"Can't open \"wordy\" file.\n");
exit(EXIT_FAILURE);
}
puts("Enter words to add to the file;press the #");
puts("key at the beginning of a line to terminate.");
while(fscanf(fp,"%d%40s", &count,words) == 2);
while((fscanf(stdin,"%40s",words) == 1) && (words[0] != '#'))
{
count++;
fprintf(fp,"%d%s\n", count,words);
}
puts("File contents:");
rewind(fp);
while(fscanf(fp,"%s",words) == 1)
puts(words);
puts("Done!");
if(fclose(fp) != 0)
fprintf(stderr,"Error closing file\n");
system("pause");
return 0;
}
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#define MAX 41
int main(int argc, char *argv[])
{
FILE *fp;
char words[MAX];
int count;
if((fp = fopen("wordy.txt","a+")) == NULL)
{
fprintf(stdout,"Can't open \"wordy\" file.\n");
exit(EXIT_FAILURE);
}
puts("Enter words to add to the file;press the #");
puts("key at the beginning of a line to terminate.");
while(fscanf(fp,"%d%40s", &count,words) == 2);
while((fscanf(stdin,"%40s",words) == 1) && (words[0] != '#'))
{
count++;
fprintf(fp,"%d%s\n", count,words);
}
puts("File contents:");
rewind(fp);
while(fscanf(fp,"%s",words) == 1)
puts(words);
puts("Done!");
if(fclose(fp) != 0)
fprintf(stderr,"Error closing file\n");
system("pause");
return 0;
}
在这里插入代码片
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define SLEN 81
int main(int argc, char **argv)
{
FILE * fp;
long pos = 0;
char name[SLEN];
int ch;
puts("Please input the file's name: ");
scanf("%80s", name);
if((fp = fopen(name,"r")) == NULL)
{
fprintf(stderr, "Can't open %s\n", name);
exit(EXIT_FAILURE);
}
printf("Please input the file's position: ");
while((scanf("%ld", &pos) == 1) && (pos >=0))
{
fseek(fp, pos, SEEK_SET);
while(((ch = fgetc(fp)) != EOF) && ch != '\n')
{
putc(ch, stdout);
}
putchar('\n');
printf("Please input the file's position: ");
}
if(fclose(fp) != 0)
{
fprintf(stderr, "Can't close file %s\n", name);
}
return 0;
}
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#define SLEN 81
int main(int argc, char **argv)
{
FILE * fp;
char str[SLEN];
if(argc != 3)
{
fprintf(stderr, "Error, The parameter isn't right.\n");
exit(EXIT_FAILURE);
}
if((fp = fopen(argv[2],"r")) == NULL)
{
fprintf(stderr, "Can't open %s\n", argv[2]);
exit(EXIT_FAILURE);
}
while(fgets(str, SLEN - 1, fp) != NULL)
{
if(strstr(str, argv[1]) != NULL)
{
fputs(str, stdout);
}
}
if(fclose(fp) != 0)
{
fprintf(stderr, "Can't close file %s\n", argv[2]);
}
return 0;
}
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#define SLEN 81
#define LEVELS 10
#define ROW 20
#define LINE 30
const char trans[LEVELS + 1] = " .':~*=&%#";
int main(int argc, char **argv)
{
FILE * fp1, *fp2;
char str[SLEN];
int num;
int arr1[ROW][LINE];
char arr2[ROW][LINE+1];
if(argc != 3)
{
fprintf(stderr, "Error, The parameter isn't right.\n");
exit(EXIT_FAILURE);
}
if((fp1 = fopen(argv[1],"r")) == NULL)
{
fprintf(stderr, "Can't open %s\n", argv[1]);
exit(EXIT_FAILURE);
}
if((fp2 = fopen(argv[2],"w")) == NULL)
{
fprintf(stderr, "Can't open %s\n", argv[2]);
exit(EXIT_FAILURE);
}
for(int i = 0; i < ROW; i++)
{
for(int j = 0; j < LINE; j++)
{
fscanf(fp1, "%d",&num);
arr1[i][j] = num;
}
}
for(int i = 0; i < ROW; i++)
{
for(int j = 0; j <= LINE; j++)
{
if(j == LINE)
{
arr2[i][j] = '\0';
}
else
{
arr2[i][j] = trans[arr1[i][j]];
}
}
}
for(int i = 0; i < ROW; i++)
{
puts(arr2[i]);
}
for(int i = 0; i < ROW; i++)
{
for(int j = 0; j <= LINE; j++)
{
if(j == LINE)
{
fprintf(fp2, "\n");
}
else
{
fprintf(fp2, "%c", arr2[i][j]);
}
}
}
if(fclose(fp1) != 0)
{
fprintf(stderr, "Can't close file %s\n", argv[1]);
}
if(fclose(fp2) != 0)
{
fprintf(stderr, "Can't close file %s\n", argv[2]);
}
return 0;
}
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#define SLEN 81
#define LEVELS 10
#define ROW 20
#define LINE 30
const char trans[LEVELS + 1] = " .':~*=&%#";
void getIntData(int row, int line, int arr1[row][line], FILE *fp)
{
for(int i = 0; i < row; i++)
{
for(int j = 0; j < line; j++)
{
fscanf(fp, "%d",&arr1[i][j]);
}
}
}
void tranData(int row, int line, int arr1[row][line], char arr2[row][line + 1])
{
for(int i = 0; i < row; i++)
{
for(int j = 0; j <= line; j++)
{
if(j == line)
{
arr2[i][j] = '\0';
}
else
{
arr2[i][j] = trans[arr1[i][j]];
}
}
}
}
void showPicture(int row, int line,char arr[row][line+1])
{
for(int i = 0; i < row; i++)
{
puts(arr[i]);
}
}
void showInFile(int row, int line, char arr1[row][line], FILE *fp)
{
for(int i = 0; i < row; i++)
{
for(int j = 0; j <= line; j++)
{
if(j == line)
{
fprintf(fp, "\n");
}
else
{
fprintf(fp, "%c", arr1[i][j]);
}
}
}
}
int main(int argc, char **argv)
{
FILE * fp1, *fp2;
char str[SLEN];
int num;
int row = 20, line = 30;
int arr1[row][line];
char arr2[row][line+1];
if(argc != 3)
{
fprintf(stderr, "Error, The parameter isn't right.\n");
exit(EXIT_FAILURE);
}
if((fp1 = fopen(argv[1],"r")) == NULL)
{
fprintf(stderr, "Can't open %s\n", argv[1]);
exit(EXIT_FAILURE);
}
if((fp2 = fopen(argv[2],"w")) == NULL)
{
fprintf(stderr, "Can't open %s\n", argv[2]);
exit(EXIT_FAILURE);
}
getIntData(row, line, arr1, fp1);
tranData(row, line, arr1, arr2);
showPicture(row, line, arr2);
showInFile(row, line, arr2, fp2);
if(fclose(fp1) != 0)
{
fprintf(stderr, "Can't close file %s\n", argv[1]);
}
if(fclose(fp2) != 0)
{
fprintf(stderr, "Can't close file %s\n", argv[2]);
}
return 0;
}
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#define SLEN 81
#define LEVELS 10
#define ROW 20
#define LINE 30
const char trans[LEVELS + 1] = " .':~*=&%#";
void getIntData(int row, int line, int arr1[row][line], FILE *fp)
{
for(int i = 0; i < row; i++)
{
for(int j = 0; j < line; j++)
{
fscanf(fp, "%d",&arr1[i][j]);
}
}
}
void tranData(int row, int line, int arr1[row][line], char arr2[row][line + 1])
{
for(int i = 0; i < row; i++)
{
for(int j = 0; j <= line; j++)
{
if(j == line)
{
arr2[i][j] = '\0';
}
else
{
arr2[i][j] = trans[arr1[i][j]];
}
}
}
}
void showPicture(int row, int line,char arr[row][line+1])
{
for(int i = 0; i < row; i++)
{
puts(arr[i]);
}
}
void showInFile(int row, int line, char arr1[row][line], FILE *fp)
{
for(int i = 0; i < row; i++)
{
for(int j = 0; j <= line; j++)
{
if(j == line)
{
fprintf(fp, "\n");
}
else
{
fprintf(fp, "%c", arr1[i][j]);
}
}
}
}
void dealData(int row, int line, int arr[row][line])
{
for(int i = 0; i < row; i++)
{
for(int k = 0; k < line; k++)
{
if(i == 0 && k == 0)
{
if(abs(arr[i][k] - arr[0][1]) > 1 && abs(arr[i][k] - arr[1][0]) > 1)
{
arr[i][k] = (arr[0][1] + arr[1][0]) / 2;
}
}
else if(i == 0 && k == 29)
{
if(abs(arr[i][k] - arr[0][28]) > 1 && abs(arr[i][k] - arr[1][29]) > 1)
{
arr[i][k] = (arr[0][28] + arr[1][29]) / 2;
}
}
else if(i == 19 && k == 0)
{
if(abs(arr[i][k] - arr[18][0]) > 1 && abs(arr[i][k] - arr[19][1]) > 1)
{
arr[i][k] = (arr[18][0] + arr[19][1]) / 2;
}
}
else if(i == 19 && k == 29)
{
if(abs(arr[i][k] - arr[18][29]) > 1 && abs(arr[i][k] - arr[19][28]) > 1)
{
arr[i][k] = (arr[18][29] + arr[19][28]) / 2;
}
}
else if(i == 0)
{
if(abs(arr[i][k] - arr[i][k-1]) > 1 && abs(arr[i][k] - arr[i][k+1]) > 1 && abs(arr[i][k] - arr[i+1][k]) > 1)
{
arr[i][k] = (arr[i][k-1] + arr[i][k+1] + arr[i+1][k]) / 3;
}
}
else if(i == 19)
{
if(abs(arr[i][k] - arr[i][k-1]) > 1 && abs(arr[i][k] - arr[i][k+1]) > 1 && abs(arr[i][k] - arr[i-1][k]) > 1)
{
arr[i][k] = (arr[i][k-1] + arr[i][k+1] + arr[i+1][k]) / 3;
}
}
else if(k == 0)
{
if(abs(arr[i][k] - arr[i-1][k]) > 1 && abs(arr[i][k] - arr[i+1][k]) > 1 && abs(arr[i][k] - arr[i][k+1]) > 1)
{
arr[i][k] = (arr[i-1][k] + arr[i+1][k] + arr[i][k+1]) / 3;
}
}
else if(k == 29)
{
if(abs(arr[i][k] - arr[i-1][k]) > 1 && abs(arr[i][k] - arr[i+1][k]) > 1 && abs(arr[i][k] - arr[i][k-1]) > 1)
{
arr[i][k] = (arr[i-1][k] + arr[i+1][k] + arr[i][k-1]) / 3;
}
}
else
{
if(abs(arr[i][k] - arr[i-1][k]) > 1 && abs(arr[i][k] - arr[i+1][k]) > 1 && abs(arr[i][k] - arr[i][k-1]) > 1 && abs(arr[i][k] - arr[i][k+1]) > 1)
{
arr[i][k] = (arr[i-1][k] + arr[i+1][k] + arr[i][k-1] + arr[i][k+1]) / 4;
}
}
}
}
}
int main(int argc, char **argv)
{
FILE * fp1, *fp2;
char str[SLEN];
int num;
int row = 20, line = 30;
int arr1[row][line];
char arr2[row][line+1];
if(argc != 3)
{
fprintf(stderr, "Error, The parameter isn't right.\n");
exit(EXIT_FAILURE);
}
if((fp1 = fopen(argv[1],"r")) == NULL)
{
fprintf(stderr, "Can't open %s\n", argv[1]);
exit(EXIT_FAILURE);
}
if((fp2 = fopen(argv[2],"w")) == NULL)
{
fprintf(stderr, "Can't open %s\n", argv[2]);
exit(EXIT_FAILURE);
}
getIntData(row, line, arr1, fp1);
dealData(row, line, arr1);
tranData(row, line, arr1, arr2);
showPicture(row, line, arr2);
showInFile(row, line, arr2, fp2);
if(fclose(fp1) != 0)
{
fprintf(stderr, "Can't close file %s\n", argv[1]);
}
if(fclose(fp2) != 0)
{
fprintf(stderr, "Can't close file %s\n", argv[2]);
}
return 0;
}
|