民国学生帽子:C语言文件操作(二)
来源:百度文库 编辑:偶看新闻 时间:2024/10/02 20:33:39
示例程序一:模拟dos命令copy整个程序分为三部分:头文件copy.h,实现功能文件copy.cpp,主函数文件main.cpp,具体源程序如下:copy.h程序如下:#ifndef _COPY_H_
#define _COPY_H_ #define MAX_PATH 260 enum ERR_CODE
{
ERR_TRUE = 0,
ERR_NO_SRC_FILE = 1,
ERR_NO_DES_FILE = 2,
}; void help(void);
ERR_CODE copy(const char* file_src,const char* file_des);
#endif
copy.cpp程序如下:#include "copy.h"
#include using namespace std; void help(void)
{
cout<<"CopyFile Usage:\n\n\tCopyFile Source Destination";
cout<<"\n\tSource file\n\tDestination:Destination file\n";
} ERR_CODE copy(const char* file_src , const char* file_des)
{ FILE* fp_src;
FILE* fp_des; if((fp_src=fopen(file_src,"rb"))==NULL)
{
cout<<"Open Source File: "< return ERR_NO_SRC_FILE;
} if((fp_des=fopen(file_des,"wb"))==NULL)
{
cout<<" Open Destination File:"< return ERR_NO_DES_FILE;
} unsigned char buffer[MAX_PATH];
int nRet = 0; #ifndef FEOF
do{
nRet = fread(buffer,sizeof(unsigned char),MAX_PATH,fp_src);
fwrite(buffer,sizeof(unsigned char),nRet,fp_des);
}while(nRet==MAX_PATH);
#else
while(!feof(fp_src))
{
nRet=fread(buffer,sizeof(unsigned char),MAX_PATH,fp_src);
fwrite(buffer,sizeof(unsigned char),nRet,fp_des);
}
#endif
fclose(fp_des);
fclose(fp_src); return ERR_TRUE;
}
main.cpp程序如下:#include
#include "copy.h" using namespace std; int main(int argc,char* argv[])
{
if(argc!=3)
{
help();
return 1;
} if(ERR_TRUE!=copy(argv[1],argv[2]))
cout<<"Copy Error"< else
cout<<"Copied\t\t1 file"<示例程序二:读取如下格式的文本文件score.txt,得到每个人的分数的平均值,再写入新的文本文件aver_score.txt中读取的文本文件格式如下:Zhang=80,75,92
Wang=61,65,71
Li=59,63,70
Zhao=85,87,90
Zhou=76,77,85 整个程序分为三个文件,student.h,student.cpp,main.cpp,下面是这三个文件的源代码 student.h的源代码如下:#ifndef _STUDENT_H_
#define _STUDENT_H_ #include #define NAME_LEN 16
#define MAX_PATH 260
typedef struct tagStudentInfo
{
char name[NAME_LEN];
int c_score;
int math_score;
int db_score;
tagStudentInfo* next;
} Student_Info; typedef enum
{
FLAG_EQU = '=',
FLAG_COMMA = ','
} Flat_Sign; // declare Get Default StudentInfo function
void InputStudentInfo(Student_Info** person); // declare output function
void OutputStudentInfo(Student_Info* array); // free resource
void FreeResource(Student_Info* person); #endif
student.cpp的源代码如下: #include "student.h" using namespace std; static int curr_pos = 0; static int GetIndex(const char* str, Flat_Sign flag = FLAG_COMMA)
{
int count = 0;
char* tmp = (char*) (str + curr_pos);
while(*tmp)
{
if(*tmp++ != flag)
count++;
else
break;
} return count;
} static int GetScore(const char* buffer)
{
int nIndex = 0;
char tmp[MAX_PATH] = {0}; memcpy(tmp, buffer + curr_pos, nIndex = GetIndex(buffer));
curr_pos += (nIndex + 1);
return atoi(tmp);
} void InputStudentInfo(Student_Info** person)
{
FILE* fp;
if((fp = fopen("score.txt", "rt")) == NULL)
{
cout << "Open File Error" << endl;
return;
} Student_Info* info_list = new Student_Info;
Student_Info* pre_list; *person = info_list;
char buffer[MAX_PATH];
int nIndex = 0;
while(!feof(fp))
{
memset(info_list, 0, sizeof(Student_Info));
memset(buffer, 0, sizeof(buffer));
fgets(buffer, MAX_PATH, fp); memcpy(info_list->name, buffer, nIndex = GetIndex(buffer, FLAG_EQU));
curr_pos += (nIndex + 1);
info_list->c_score = GetScore(buffer);
info_list->math_score = GetScore(buffer);
info_list->db_score = GetScore(buffer); pre_list = info_list;
info_list->next = new Student_Info;
info_list = info_list->next; curr_pos = 0;
} fclose(fp); delete info_list;
pre_list->next = NULL;
} void OutputStudentInfo(Student_Info* person)
{
FILE* fp;
if((fp = fopen("aver_score.txt", "wt")) == NULL)
{
cout << "Open File Error" << endl;
return ;
} fprintf(fp, "Name = Aver_Score\n"); Student_Info* list = person;
do {
fprintf(fp, "%s = %d\n",
list->name,
(list->c_score + list->math_score + list->db_score) / 3);
list = list->next;
} while(list); fclose(fp);
} void FreeResource(Student_Info* person)
{
Student_Info* list = person;
Student_Info* temp_list;
do {
temp_list = list->next;
delete list;
list = temp_list;
} while (list);
}
main.cpp的源代码如下:#include "student.h" /*
* This is Main program for Score management
*/ int main(int argc, char** argv)
{
Student_Info* person = NULL; //Get all the student score from file
InputStudentInfo(&person); //Output student score into file
OutputStudentInfo(person); //free memory resource
FreeResource(person); // getchar();
return 0;
}
最后生成的结果文件如下图:Name = Aver_Score
Zhang=82
Wang=65
Li=64
Zhao=87
Zhou=79 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/liyebing/archive/2008/12/07/3466440.aspx
#define _COPY_H_ #define MAX_PATH 260 enum ERR_CODE
{
ERR_TRUE = 0,
ERR_NO_SRC_FILE = 1,
ERR_NO_DES_FILE = 2,
}; void help(void);
ERR_CODE copy(const char* file_src,const char* file_des);
#endif
copy.cpp程序如下:#include "copy.h"
#include
{
cout<<"CopyFile Usage:\n\n\tCopyFile Source Destination";
cout<<"\n\tSource file\n\tDestination:Destination file\n";
} ERR_CODE copy(const char* file_src , const char* file_des)
{ FILE* fp_src;
FILE* fp_des; if((fp_src=fopen(file_src,"rb"))==NULL)
{
cout<<"Open Source File: "<
} if((fp_des=fopen(file_des,"wb"))==NULL)
{
cout<<" Open Destination File:"<
} unsigned char buffer[MAX_PATH];
int nRet = 0; #ifndef FEOF
do{
nRet = fread(buffer,sizeof(unsigned char),MAX_PATH,fp_src);
fwrite(buffer,sizeof(unsigned char),nRet,fp_des);
}while(nRet==MAX_PATH);
#else
while(!feof(fp_src))
{
nRet=fread(buffer,sizeof(unsigned char),MAX_PATH,fp_src);
fwrite(buffer,sizeof(unsigned char),nRet,fp_des);
}
#endif
fclose(fp_des);
fclose(fp_src); return ERR_TRUE;
}
main.cpp程序如下:#include
#include "copy.h" using namespace std; int main(int argc,char* argv[])
{
if(argc!=3)
{
help();
return 1;
} if(ERR_TRUE!=copy(argv[1],argv[2]))
cout<<"Copy Error"<
cout<<"Copied\t\t1 file"<
Wang=61,65,71
Li=59,63,70
Zhao=85,87,90
Zhou=76,77,85 整个程序分为三个文件,student.h,student.cpp,main.cpp,下面是这三个文件的源代码 student.h的源代码如下:#ifndef _STUDENT_H_
#define _STUDENT_H_ #include
#define MAX_PATH 260
typedef struct tagStudentInfo
{
char name[NAME_LEN];
int c_score;
int math_score;
int db_score;
tagStudentInfo* next;
} Student_Info; typedef enum
{
FLAG_EQU = '=',
FLAG_COMMA = ','
} Flat_Sign; // declare Get Default StudentInfo function
void InputStudentInfo(Student_Info** person); // declare output function
void OutputStudentInfo(Student_Info* array); // free resource
void FreeResource(Student_Info* person); #endif
student.cpp的源代码如下: #include "student.h" using namespace std; static int curr_pos = 0; static int GetIndex(const char* str, Flat_Sign flag = FLAG_COMMA)
{
int count = 0;
char* tmp = (char*) (str + curr_pos);
while(*tmp)
{
if(*tmp++ != flag)
count++;
else
break;
} return count;
} static int GetScore(const char* buffer)
{
int nIndex = 0;
char tmp[MAX_PATH] = {0}; memcpy(tmp, buffer + curr_pos, nIndex = GetIndex(buffer));
curr_pos += (nIndex + 1);
return atoi(tmp);
} void InputStudentInfo(Student_Info** person)
{
FILE* fp;
if((fp = fopen("score.txt", "rt")) == NULL)
{
cout << "Open File Error" << endl;
return;
} Student_Info* info_list = new Student_Info;
Student_Info* pre_list; *person = info_list;
char buffer[MAX_PATH];
int nIndex = 0;
while(!feof(fp))
{
memset(info_list, 0, sizeof(Student_Info));
memset(buffer, 0, sizeof(buffer));
fgets(buffer, MAX_PATH, fp); memcpy(info_list->name, buffer, nIndex = GetIndex(buffer, FLAG_EQU));
curr_pos += (nIndex + 1);
info_list->c_score = GetScore(buffer);
info_list->math_score = GetScore(buffer);
info_list->db_score = GetScore(buffer); pre_list = info_list;
info_list->next = new Student_Info;
info_list = info_list->next; curr_pos = 0;
} fclose(fp); delete info_list;
pre_list->next = NULL;
} void OutputStudentInfo(Student_Info* person)
{
FILE* fp;
if((fp = fopen("aver_score.txt", "wt")) == NULL)
{
cout << "Open File Error" << endl;
return ;
} fprintf(fp, "Name = Aver_Score\n"); Student_Info* list = person;
do {
fprintf(fp, "%s = %d\n",
list->name,
(list->c_score + list->math_score + list->db_score) / 3);
list = list->next;
} while(list); fclose(fp);
} void FreeResource(Student_Info* person)
{
Student_Info* list = person;
Student_Info* temp_list;
do {
temp_list = list->next;
delete list;
list = temp_list;
} while (list);
}
main.cpp的源代码如下:#include "student.h" /*
* This is Main program for Score management
*/ int main(int argc, char** argv)
{
Student_Info* person = NULL; //Get all the student score from file
InputStudentInfo(&person); //Output student score into file
OutputStudentInfo(person); //free memory resource
FreeResource(person); // getchar();
return 0;
}
最后生成的结果文件如下图:Name = Aver_Score
Zhang=82
Wang=65
Li=64
Zhao=87
Zhou=79 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/liyebing/archive/2008/12/07/3466440.aspx
C语言文件操作
C语言文件位置指针的操作函数问题
请教:c语言文件操作 换行怎么实现?
C语言基本操作
C语言操作问题
c语言操作问题
c语言操作问题
c语言(操作问题)
C语言操作问题
C语言操作问题
c语言操作平台是什么
作业:c语言源代码文件(.c文件)
C语言标准文件
c语言文件题目
C语言文件问题。
C语言文件问题。
c语言,文件指针
C语言头文件
C语言编写的文件操作程序,如果编写错误,有可能损坏系统吗?
不我想用C语言操作word文件 不知道有谁有相关的接口函数
如何使用C语言进行文件的重命名及删除操作?
怎么对C语言中的文件进行内容添加,删除,查找的操作?
C语言中二进制文件操作代码
二级C语言上机的操作