C学习笔记——打开文件,并保存错误信息
来源:互联网 发布:微盘交易软件 编辑:程序博客网 时间:2024/06/03 03:31
这两天学习文件访问,产生了一个做个小程序的想法:打开一个文件,并把它的内容显示在屏幕上,如果不能打开,则记录当前时间和错误信息到错误文件中。
运行时,把文件放入在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 nowfprintf(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 fileFILE *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 directoryputs("Error: Can't get current working directory\n");elseprintf("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 stdinslct=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 stdinif((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 linefclose(fp);}free(fname);}}puts("");system("pause");}
0 0
- C学习笔记——打开文件,并保存错误信息
- 雅阁学习笔记之windows编程——保存文件和打开浏览文件夹窗口
- C#学习笔记(十六):使用打开保存文件对话框
- C#学习笔记(十六):使用打开保存文件对话框
- C#学习笔记(十六):使用打开保存文件对话框
- PyQt5学习笔记09----标准文件打开保存框QFileDialog
- c#-文件打开与保存,打开文件夹
- Qt学习笔记——打开并显示图片
- QT笔记(6)——Qt打开文件与保存文件
- VC++项目开发笔记——打开或保存指定类型的文件CFileDialog类
- Practical Common Lisp学习笔记——之保存文件
- Android学习笔记——保存文件(Saving Files)
- Object-c学习笔记十六-----文件加载与保存
- Objective-C学习笔记第十五章文件加载与保存
- 打开文件对话框写入并保存文件操作
- MFC学习(20)MFC打开/保存文件对话框——CFileDialog之一
- MFC学习(21)MFC打开/保存文件对话框——CFileDialog之二
- Java学习笔记3 —— Java常见错误信息
- add binary
- Girl_iOS100天学iOS的第八天(nil)
- hdu1234开门人和关门人(结构体排序)
- Java&&RTTI(运行时类型识别)
- Android - Error:Calling startActivity() from outside of an activity context
- C学习笔记——打开文件,并保存错误信息
- 使用left join造成的数据重复问
- 函数指针实现对象传递消息(回调)
- 九度 题目1449:确定比赛名次
- Unity3D学习之第四天(脚本入门)
- 关于struts2加载xml失败的处理
- Android项目之HomeHealth基础学习1:Baseadapter
- .net中XML的详细解析
- Android_3 事件响应