這兩天學習文件訪問,產生了一個做個小程序的想法:打開一個文件,並把它的內容顯示在屏幕上,如果不能打開,則記錄當前時間和錯誤信息到錯誤文件中。
運行時,把文件放入在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");
}