#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;
}
C文件读写
最新推荐文章于 2022-11-14 23:27:40 发布