本来以为什么I\o,还不是手到擒来,结果就被人擒了。整整两天时间,都填这上头了。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#define BUFSIZE 1024
#define NAMELEN 80
//int argc, char *argv[]
int main(int argc, char *argv[]) {
//1
/*
int ch;
FILE *fp;
long cout = 0;
char redName[NAMELEN];
printf("Please enter a file name to be read:");
gets(redName);
printf("the filename is : %s\n",redName);
if (fp = fopen(redName, "r") == NULL)
{
printf("cannot open %s",redName);
exit(1);
}
fp = fopen(redName, "r");
if (fp==NULL)
{
printf("huge error!");
}
while ((ch=getc(fp))!=EOF)
{
putc(ch, stdout);
cout++;
}
fclose(fp);
printf("File %s has %d characters\n", redName, cout);
*/
//2
/*
//cmd下输入两个参数,一个为被复制文件名称,另一个为生成文件名称(需加扩展名!)
FILE *fromFile;
FILE *toFile;
void append(FILE *source, FILE *dest);
if (argc < 3)
{
printf("don't have enough arguments!");
exit(1);
}
if ((fromFile = fopen(argv[1], "r")) == NULL)
{
printf("cannot open file:%s!", argv[1]);
exit(1);
}
if ((toFile = fopen(argv[2], "a")) == NULL)
{
printf("cannot copy file:%s", argv[2]);
exit(1);
}
if (setvbuf(toFile,NULL,_IOFBF,BUFSIZE)!=0)
{
fputs("Cannot create output buffer\n", stderr);
}
if (setvbuf(fromFile, NULL, _IOFBF, BUFSIZE) != 0)
{
fputs("Cannot create input buffer\n", stderr);
}
append(fromFile, toFile);
if (ferror(fromFile)!=0)
{
fprintf(stderr,"Error in reading files%s",argv[1]);
}
if (ferror(toFile)!=0)
{
fprintf(stderr,"Error in writing file:%s",argv[2]);
}
fclose(fromFile);
fclose(toFile);
*/
//3
/*
FILE *fromFile;
FILE *toFile;
char ch;
char fromName[NAMELEN];
char toName[NAMELEN];
printf("Please enter the filename you want to process:\n");
gets(fromName);
printf("Please enter the filename you want to create:\n");
gets(toName);
if ((fromFile = fopen(fromName, "r")) == NULL)
{
printf("cannot open file:%s!", fromName);
exit(1);
}
if ((toFile = fopen(toName, "a")) == NULL)
{
printf("cannot copy file:%s", toName);
exit(1);
}
while ((ch = getc(fromFile))!=EOF)
{
ch = toupper(ch);
putc(ch, toFile);
}
if (ferror(fromFile) != 0)
{
fprintf(stderr, "Error in reading files%s", argv[1]);
}
if (ferror(toFile) != 0)
{
fprintf(stderr, "Error in writing file:%s", argv[2]);
}
fclose(fromFile);
fclose(toFile);
*/
//4
/*
int m;
FILE *fp;
char Path[] = "D:\\CWorkSpace\\Chapter13_02\\Debug\\";//相对路径,为了省事!~~~
char absPath[NAMELEN];//绝对路径存放处。运行文件时需绝对路径
char data[BUFSIZE];
char *pos;
if (argc<2)
{
printf("Done!");
exit(1);
}
for (int i = 1; i < argc; i++)
{
sprintf(absPath, "%s%s", Path, argv[i]);//组合绝对路径
if ((fp = fopen(absPath, "r")) == NULL)
{
printf("cannot open file:%s", argv[i]);//找不到文件则报错!
}
else
{
int sum = 0;
pos = fgets(data, BUFSIZE, fp);
while (pos != NULL)
{
fputs(data, stdout);
pos = fgets(data, BUFSIZE, fp);
}
printf("\nfile %s has done!\n", argv[i]);
if (fclose(fp) != 0) {
fprintf(stderr, "ERROR in closing!");
}
}
}
if (fclose(fp) != 0) {
fprintf(stderr, "ERROR in closing!");
}
*/
//5
/*
FILE *fa;
FILE *fs;
int count = 2;//文件计数器,1为程序名,2为被添加文件,以后为资源文件
char Path[] = "D:\\CWorkSpace\\Chapter13_02\\Debug\\";//相对路径,为了省事!~~~
char absPath1[NAMELEN];//绝对路径存放处。运行文件时需绝对路径
char absPath2[NAMELEN];//绝对路径存放处。运行文件时需绝对路径
void append(FILE *source,FILE *dest);
sprintf(absPath1, "%s%s", Path, argv[1]);//组合绝对路径
if ((fa = fopen(absPath1, "a")) == NULL)//第一个为被追加的文件,以后的都为来源文件
{
printf("cannot open file:%s", argv[1]);//找不到文件则报错!
exit(1);
}
while (count < argc)
{
sprintf(absPath2, "%s%s", Path, argv[count]);//组合绝对路径
if (strcmp(argv[1], argv[count]) == 0)
{
printf("cannot append itself!!!");
}
else if ((fs = fopen(absPath2, "r")) == NULL)
{
printf("cannot open file:%s", argv[count]);//找不到文件则报错!
exit(2);
}
else {
append(fs, fa);
fclose(fs);
printf("\n%s is appended!",argv[count]);
}
count++;
}
printf("DONE!");
fclose(fa);
*/
//6
/*
FILE *in;
FILE *out;
int ch;
char fromName[NAMELEN];
char toName[NAMELEN];
int count = 0;
printf("please enter the file name you want to process:");
gets(fromName);
if ((in = fopen(fromName, "r")) == NULL) {
fprintf(stderr, "couldn't open file:%s", fromName);
exit(2);
}
strcpy(toName, fromName);
strcat(toName, ".red");
if ((out = fopen(toName, "a")) == NULL) {
fprintf(stderr, "couldn't open file:%s", fromName);
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!");
}
*/
//7
//a
/*
FILE *name1;
FILE *name2;
char *pos1;//两个结束的位置
char *pos2;
char Path[] = "D:\\CWorkSpace\\Chapter13_02\\Debug\\";//相对路径,为了省事!~~~
char ch1[BUFSIZE];
char ch2[BUFSIZE];
char fileName1[NAMELEN];//文件名
char fileName2[NAMELEN];
char absPath1[NAMELEN];//绝对路径存放处。运行文件时需绝对路径
char absPath2[NAMELEN];
printf("please enter the first filename you want to process:");
gets(fileName1);
//fgets(fileName1, NAMELEN, stdin);
sprintf(absPath1, "%s%s", Path, fileName1);//组合绝对路径
if ((name1 = fopen(absPath1, "r")) == NULL) {
fprintf(stderr, "couldn't open file:%s", fileName1);
exit(2);
}
printf("please enter the second filename you want to process:");
gets(fileName2);
//fgets(fileName2, NAMELEN, stdin);
sprintf(absPath2, "%s%s", Path, fileName2);
if ((name2 = fopen(absPath2, "r")) == NULL) {
fprintf(stderr, "couldn't open file:%s", fileName2);
exit(3);
}
pos1 = fgets(ch1, BUFSIZE, name1);//先行试探
pos2 = fgets(ch2, BUFSIZE, name2);
while (pos1 != NULL || pos2 != NULL)
{//每次各获取一行,判断是否为空
if (pos1 != NULL)//哪个不为空就打印哪个
{
fprintf(stdout, "%s", ch1);
pos1 = fgets(ch1, BUFSIZE, name1);
}
if (pos2 != NULL)
{
fprintf(stdout, "%s", ch2);
pos2 = fgets(ch2, BUFSIZE, name2);
}
}
if (fclose(name1) != 0 || fclose(name2) != 0) {
fprintf(stderr, "ERROR in closing!");
}
*/
//7
//b
/*
FILE *name1;
FILE *name2;
char *pos1;//两个结束的位置
char *pos2;
char Path[] = "D:\\CWorkSpace\\Chapter13_02\\Debug\\";//相对路径,为了省事!~~~
char ch1[BUFSIZE];
char ch2[BUFSIZE];
char fileName1[NAMELEN];//文件名
char fileName2[NAMELEN];
char absPath1[NAMELEN];//绝对路径存放处。运行文件时需绝对路径
char absPath2[NAMELEN];
printf("please enter the first filename you want to process:");
gets(fileName1);
//fgets(fileName1, NAMELEN, stdin);
sprintf(absPath1, "%s%s", Path, fileName1);//组合绝对路径
if ((name1 = fopen(absPath1, "r")) == NULL) {
fprintf(stderr, "couldn't open file:%s", fileName1);
exit(2);
}
printf("please enter the second filename you want to process:");
gets(fileName2);
//fgets(fileName2, NAMELEN, stdin);
sprintf(absPath2, "%s%s", Path, fileName2);
if ((name2 = fopen(absPath2, "r")) == NULL) {
fprintf(stderr, "couldn't open file:%s", fileName2);
exit(3);
}
pos1 = fgets(ch1, BUFSIZE, name1);//先行试探
pos2 = fgets(ch2, BUFSIZE, name2);
while (pos1 != NULL || pos2 != NULL)
{//每次各获取一行,判断是否为空
if (pos1 != NULL)//哪个不为空就打印哪个
{
if (pos2!=NULL)//在pos2不为空,也就是第二个没完的情况下,将第一个出现时的\n都去掉
{
int count = 0;
while (ch1[count]!='\0')//此处循环找出\n
{
if (ch1[count]=='\n')
{
ch1[count] = '\0';
}
count++;
}
}
fprintf(stdout, "%s", ch1);
pos1 = fgets(ch1, BUFSIZE, name1);
}
if (pos2 != NULL)
{
fprintf(stdout, "%s", ch2);
pos2 = fgets(ch2, BUFSIZE, name2);
}
}
if (fclose(name1) != 0|| fclose(name2) != 0 ) {
fprintf(stderr, "ERROR in closing!");
}
*/
//8
/*
int strLength;
FILE *fp;
char *pos;
char data[BUFSIZE];
char Path[] = "D:\\CWorkSpace\\Chapter13_02\\Debug\\";//相对路径,为了省事!~~~
char ch1[BUFSIZE];
char absPath[NAMELEN];//绝对路径存放处。运行文件时需绝对路径
if (argc < 2 || strlen(argv[1]) < 2)//如果第二个参数的长度小于2,即是一个字符//stdin
{
int temp;
if (argc<2)
{
temp = 0;
}
else
{
temp = 1;
}
printf("Enter:\n");
fgets(data, BUFSIZE, stdin);
strLength = strlen(data);
printf("the total number of letters plus the order is %d", strLength-1+temp);
}
else {
for (int i = 1; i < argc; i++)
{
sprintf(absPath, "%s%s", Path, argv[i]);//组合绝对路径
if ((fp = fopen(absPath, "r")) == NULL)
{
printf("cannot open file:%s", argv[i]);//找不到文件则报错!
}
else
{
int sum = 0;
pos = fgets(data, BUFSIZE, fp);
while (pos != NULL)
{
sum += strlen(data);
pos = fgets(data, BUFSIZE, fp);
}
sum += strlen(argv[i]);
printf("file %s has :%d characters!\n", argv[i],sum);
if (fclose(fp) != 0 ) {
fprintf(stderr, "ERROR in closing!");
}
}
}
}
*/
//9
/*
FILE *fp;
char words[NAMELEN];
if ((fp = fopen("D:\\CWorkSpace\\Chapter13_02\\Debug\\words.txt", "a+")) == NULL)
{
printf("cannot open file:%s", "words.txt");//找不到文件则报错!
}
puts("Enter words to add to the file:");
puts("key at the begining of a line to terminate:");
while (gets(words) != NULL&&*words != '\0')
{
fprintf(fp, "%s", words);
}
puts("File contents:");
rewind(fp);
int counter = 1;
while (fscanf(fp,"%s",words)==1)
{
printf("%d ",counter);
counter++;
puts(words);
}
if (fclose(fp)!=0)
{
fprintf(stderr,"Error closing file!\n");
}
*/
//10
/*
FILE *name1;
char Path[] = "D:\\CWorkSpace\\Chapter13_02\\Debug\\";//相对路径,为了省事!~~~
char fileName1[NAMELEN];//文件名
char absPath1[NAMELEN];//绝对路径存放处。运行文件时需绝对路径
char ch1[BUFSIZE];
long posNum = 0;
long last;
printf("please enter the first filename you want to process:");
gets(fileName1);
sprintf(absPath1, "%s%s", Path, fileName1);//组合绝对路径
if ((name1 = fopen(absPath1, "rb")) == NULL) {
fprintf(stderr, "couldn't open file:%s", fileName1);
exit(1);
}
fseek(name1, 0L, SEEK_END);
last = ftell(name1);
printf("please enter the position:(less than %d)",last);
scanf("%d", &posNum);
while (posNum != 0 && posNum < last)
{
fseek(name1, posNum, SEEK_SET);
fgets(ch1, BUFSIZE, name1);
fputs(ch1, stdout);
printf("please enter the position:");
scanf("%d", &posNum);
}
if (fclose(name1) != 0)
{
fprintf(stderr, "Error closing file!\n");
}
*/
//11
/*
char desCh[BUFSIZE];
char ch[BUFSIZE];
char name[NAMELEN]= "D:\\CWorkSpace\\Chapter13_02\\Debug\\four.txt";
FILE *fp;
int count = 0;
int have = 0;
if ((fp = fopen(name,"r")) == NULL) {
printf("cannot open file %s",name);
exit(1);
}
printf("enter desch:");
gets_s(desCh, BUFSIZE);//这里用了VS2015的函数,它在字符串后面补个结尾。之所以不用Fgets来填充desCh是因为它在后面补回车,加了回车后strstr就识别不出来了
while (fgets(ch, BUFSIZE, fp) != NULL)
{
printf("%s", ch);
if (strstr(ch, desCh) != NULL)
{
fputs(ch, stdout);
have++;
}
count++;
}
printf("Done! Search %d lines, %d have %s", count, have, desCh);
if (fclose(fp) != 0)
{
printf("something wrong with fclose!");
exit(2);
}
*/
//12
/*
int m=20;
int n=30;
int temp;
int flag;
FILE *fp;
int Mat[20][30];
char chMat[20][30];
char name[NAMELEN] = "D:\\CWorkSpace\\Chapter13_02\\Debug\\eight.txt";
void SymbolMatrix(int Mat[][30], char chMat[][30], int m, int n);
//生成文件部分
srand(time(NULL));
if ((fp = fopen(name, "w")) == NULL) {
printf("cannot open file %s", name);
exit(1);
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
temp = rand()%10;
printf("%d ", temp);
fprintf(fp, "%d ", temp);
}
printf("\n");
fprintf(fp, "\n");
}
if (fclose(fp) != 0)
{
printf("something wrong with fclose!");
exit(2);
}
//下面是重新读取文件部分
if ((fp = fopen(name, "r")) == NULL) {
printf("cannot open file %s", name);
exit(1);
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
fscanf(fp, "%d", &Mat[i][j]);
}
}
SymbolMatrix(Mat, chMat, m, n);
if (fclose(fp) != 0)
{
printf("something wrong with fclose!");
exit(2);
}
*/
//13
/*
int m=20;
int n=30;
int temp;
int flag;
FILE *fp;
int Mat[20][30];
char chMat[20][30];
char name[NAMELEN] = "D:\\CWorkSpace\\Chapter13_02\\Debug\\six.txt";
void ProcessMatrix(int Mat[][30],int m);
void SymbolMatrix(int Mat[][30], char chMat[][30],int m,int n);
//void ProcessMatrixNumber(int Mat[][30], int m, int n, int i, int j);
if ((fp = fopen(name, "r")) == NULL) {
printf("cannot open file %s", name);
exit(1);
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
fscanf(fp, "%d", &Mat[i][j]);
}
}
ProcessMatrix(Mat, m, n);
//ProcessMatrixNumber(Mat, m, n, 1, 1);
SymbolMatrix(Mat, chMat,m,n);
if (fclose(fp) != 0)
{
printf("something wrong with fclose!");
exit(2);
}
*/
return 0;
}
void SymbolMatrix(int Mat[][30],char chMat[][30],int m,int n) {
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
switch (Mat[i][j])
{
case 0:
chMat[i][j] = '!';
break;
case 1:
chMat[i][j] = '@';
break;
case 2:
chMat[i][j] = '#';
break;
case 3:
chMat[i][j] = '$';
break;
case 4:
chMat[i][j] = '%';
break;
case 5:
chMat[i][j] = '^';
break;
case 6:
chMat[i][j] = '&';
break;
case 7:
chMat[i][j] = '&';
break;
case 8:
chMat[i][j] = '*';
break;
case 9:
chMat[i][j] = '+';
break;
default:
break;
};
printf("%c", chMat[i][j]);
}
printf("\n");
}
}
void ProcessMatrix(int Mat[][30],int m,int n) {
void ProcessMatrixNumber(int Mat[][30], int m, int n,int i,int j);
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
//printf("%d ", Mat[i][j]);
ProcessMatrixNumber(Mat, m, n, i, j);
}
//printf("\n");
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
printf("%d ", Mat[i][j]);
}
printf("\n");
}
}
void ProcessMatrixNumber(int Mat[][30], int m, int n, int x, int y) {
int upx;//标记包含该数的位置,左上为up,右下为down
int upy;
int downx;
int downy;
int limPt;
int flag = 0;
int sum = 0;
upx = (x - 1) > 0 ? x - 1 : 0;
upy = (y - 1) > 0 ? y - 1 : 0;
downx = (x + 1) > (m - 1) ? (m - 1) : x + 1;
downy = (y + 1) > (m - 1) ? (m - 1) : y + 1;
limPt = (downx - upx + 1)*(downy - upy + 1) - 1;//计算出周围点的个数
for (int i = upx; i <= downx; i++)
{
for (int j = upy; j <= downy; j++)
{
flag += (abs(Mat[i][j] - Mat[x][y]) > 1) ? 1 : 0;//如果差1,则记录
}
}
if (flag==limPt)//如果每个都差1,则取值处理
{
//printf("x = %d,y = %d\n",x,y);
for (int i = upx; i <= downx; i++)
{
for (int j = upy; j <= downy; j++)
{
sum += Mat[i][j];
}
}
Mat[x][y] = ((sum - Mat[x][y]) / limPt);//除了本点外的平均值
}
}
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);
}
}