这两天学习文件访问,产生了一个做个小程序的想法:打开一个文件,并把它的内容显示在屏幕上,如果不能打开,则记录当前时间和错误信息到错误文件中。
运行时,把文件放入在cmd默认的目录(我这是C:\Users\Ralap)下,通过命令窗口(cmd)来控制。这把文件名命名为“OpenFile.exe”,在输入命令的时候,
输入格式为:程序文件名 【需打开的文件名1】 【需打开的文件名2】……
运行效果如下:
总体思路为:
1、通过命令后面的参数,打开文件。能打开便显示,无法打开便把错误信息输入到err.txt中
2、处理里命令后面的参数后,再次询问是否需要打开其他文件。打开成功与否的处理与上面一样
代码如下:
#include <stdio.h> #include <conio.h> #include <string.h> #include <stdlib.h> #include <time.h> #include <ctype.h> #include <direct.h> //get the current working directroy: getcwd() #define FILE_NAME_SIZE 50 #define ERR_INFO_SIZE 100 #define READ "r" #define WRITE "w" #define ADD "a" #define ERR_HANDLE_FILE "err.txt" char printFile(FILE *ifp, FILE *ofp) { char c; while((c=getc(ifp)) != EOF) putc(c,ofp); return ferror(ofp) ? EOF : 1; } void errorHandle(char *errfname,char *s) { time_t dtime; FILE *errp; if((errp=fopen(errfname,ADD)) == NULL) { fprintf(stderr,"Error_handle file \"%s\" can't operated!",errfname); exit(1); //exit the program directly } else{ dtime=time(NULL); //get the time of the now fprintf(errp,"%s%s\n\n",ctime(&dtime),s); fclose(errp); } } /*stradd: stack the strings*/ char stradd(char *destination, unsigned size, char *first, char *second, char *third) { /*若不判断长度,总长度过长会导致fname指针无效,在释放内存时将出现异常,中断在此函数CheckBytes()*/ if(size <= strlen(first)+strlen(second)+strlen(third)) return EOF; strcpy(destination,first); strcat(destination,second); strcat(destination,third); return 0; } int main(int argc, char **argv) { char fpath[80]; //the path of this file FILE *fp; char *exe; char slct; char *fname; char errinfo[ERR_INFO_SIZE]; exe=strrchr(*argv, '\\') == NULL ? *argv : (strrchr(*argv, '\\')+1); puts("********************************************************************"); puts("***The function of this file is open a file which you want, ***"); puts("***and the error information will output to a error_handle file, ***"); puts("***like err.txt ***"); puts("********************************************************************"); if(getcwd(fpath,sizeof(fpath)) == NULL) //print current working directory puts("Error: Can't get current working directory\n"); else printf("Current working directory is:\n%s\n",fpath); puts("____________________________________________________________________"); while(--argc >0){ if((fp = fopen(*++argv, "r")) == NULL){ stradd(errinfo, ERR_INFO_SIZE, exe," can't open the file: ",*argv); errorHandle(ERR_HANDLE_FILE,errinfo); fprintf(stderr,"%s\n",errinfo); continue; } printf("The file \"%s\" opened successfully, and the following is its contents:\n",*argv); printFile(fp,stdout); puts("\n"); fclose(fp); } while(1){ //ask users whether they want to open other files until they select "N". printf("Do you wanta open other files?(Y or N):"); scanf("%c",&slct); while(getchar() != '\n'); //empty the stdin slct=toupper(slct); if(slct == 'N') break; if(slct == 'Y'){ printf("Please input the file name:"); fname =(char *)malloc(sizeof(*fname)*FILE_NAME_SIZE); scanf("%s",fname); while(getchar() != '\n'); //empty the stdin if((fp = fopen(fname,"r")) == NULL){ if(stradd(errinfo, ERR_INFO_SIZE, exe, " can't open the file: ", fname) == EOF){ puts("your path is too deep, or the length of the path is too long");break; } errorHandle(ERR_HANDLE_FILE,errinfo); fprintf(stderr,"%s\n",errinfo); } else{ printf("The file \"%s\" opened successfully, and the following is its contents:\n",fname); printFile(fp,stdout); puts(""); //auto change line fclose(fp); } free(fname); } } puts(""); system("pause"); }