您的位置:首页 > 其它

C文件读写

2016-05-12 15:04 176 查看
#include <stdio.h>
#include <stdlib.h>

/*#include<iostream>
using namespace std;*/

#define MAXLEN 1024

void safeOpenFile(FILE* &file, char const *name, char const *mode)
{
if ((file = fopen(name, mode)) == NULL)
{
printf("open error\t");
}
printf(name);
}

void safeCloseFile(FILE *&file)
{
if (fclose(file) != 0)
{
printf("close error\n");
}
}

void copyFile(FILE *infile, FILE *outfile)
{
char buf[MAXLEN];
/*	 int n_vals;
while ((n_vals = fread(buf, sizeof(char), MAXLEN, infile)) != 0)	//fread按块读取
{
fwrite(buf, sizeof(char), n_vals, outfile);
}*/

/* while (fgets(buf, MAXLEN, infile) != NULL)		//fgets按行读取
{
fputs(buf, outfile);
}*/

/* char cc;
while (EOF != (cc = fgetc(infile)))			//fgetc单个字符读取
{
fputc(cc, outfile);
}
*/
}

void doFile(char const *fromfilename, char const *read_mode, char const *tofilename, char const *write_mode, void (*dosomething)(FILE *, FILE*))
{
FILE * outfile, *infile;

safeOpenFile(infile, fromfilename, read_mode);
safeOpenFile(outfile, tofilename, write_mode);

dosomething(infile, outfile);

safeCloseFile(infile);
safeCloseFile(outfile);
}

void encodeProcess(FILE *infile, FILE *outfile)
{
char buf[MAXLEN];

int n_vals;
while ((n_vals = fread(buf, sizeof(char), MAXLEN, infile)) != 0)		//fread按块读取
{
for (int i = 0; i < n_vals; ++i)
{
buf[i] ^= 'A';
}
fwrite(buf, sizeof(char), n_vals, outfile);
}

/* while (fgets(buf, MAXLEN, infile) != NULL)			//fgets按行读取
{
fputs(buf, outfile);
}*/

/* char cc;
while (EOF != (cc = fgetc(infile)))			//fgetc单个字符读取
{
fputc(cc, outfile);
}
*/
}

int main(int argc, char *argv[])
{
//doFile("Slicing.gcode", "rb", "result.txt", "wb", ©File);

doFile("Slicing.gcode", "rb", "res.gcode", "wb", &encodeProcess);
doFile("res.gcode", "rb", "Slicing_after.gcode", "wb", ©File);

//	system("PAUSE");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: