全局变量:
FILE* gfptrIn = NULL;
FILE* gfptrOut = NULL;
FILE* gfptrKey = NULL;
char gInFileName[256];
char gOutFileName[256];
char gKeyFileName[256];
以下是打开输入文件和输出文件的函数:
FILE* openInputFile(char* filename)
{
FILE* fptr;
long sz;
fptr = fopen(filename, "rb");
if (fptr == NULL)
{
fprintf(stderr, "\n\nError - Could not open input file %s!\n\n", filename);
exit(-1);
}
fseek(fptr, 0, SEEK_END);
sz = ftell(fptr);
fseek(fptr, 0, SEEK_SET);
if (sz == 0)
{
fprintf(stderr, "Error - File size is zero for \"%s\".\n\n", filename);
fprintf(stderr, "Aborting operation - try again!\n\n");
fclose(fptr);
exit(0);
}
return fptr;
} // openInputFile
FILE* openOutputFile(char* filename)
{
FILE* fptr;
fptr = fopen(filename, "wb+");
if (fptr == NULL)
{
fprintf(stderr, "\n\nError - Could not open output file %s!\n\n", filename);
exit(-1);
}
return fptr;
} // openOutputFile
编辑:解析命令行参数
void parseCommandLine(int argc, char* argv[])
{
int cnt;
char ch;
bool i_flag, o_flag, k_flag, p_flag, err_flag;
i_flag = k_flag = false; // these must be true in order to exit this function
err_flag = p_flag = o_flag = false; // these will generate different actions
cnt = 1; // skip program name
while (cnt < argc)
{
ch = *argv[cnt];
if (ch != '-')
{
fprintf(stderr, "All options must be preceeded by a dash '-'\n\n");
usage(argv);
}
ch = *(argv[cnt] + 1);
if (0)
{
}
else if (ch == 'e' || ch == 'E')
{
if (i_flag == true || gOp != 0)
{
fprintf(stderr, "Error! Already specifed an input file - can't encrypt/decrypt multiple files.\n\n");
usage(argv);
}
i_flag = true;
cnt++;
if (cnt >= argc)
{
fprintf(stderr, "Error! Must specify a filename after '-e'\n\n");
usage(argv);
}
strncpy(gInFileName, argv[cnt], 256);
gOp = 1; // encrypt
}
else if (ch == 'd' || ch == 'D')
{
if (i_flag == true || gOp != 0)
{
fprintf(stderr, "Error! Already specifed an input file - can't decrypt/encrypt multiple files.\n\n");
usage(argv);
}
i_flag = true;
cnt++;
if (cnt >= argc)
{
fprintf(stderr, "Error! Must specify a filename after '-d'\n\n");
usage(argv);
}
strncpy(gInFileName, argv[cnt], 256);
gOp = 2; // decrypt
}
else if (ch == 'o' || ch == 'O')
{
if (o_flag == true)
{
fprintf(stderr, "Error! Already specifed an output file.\n\n");
usage(argv);
}
o_flag = true;
cnt++;
if (cnt >= argc)
{
fprintf(stderr, "Error! Must specify a filename after '-o'\n\n");
usage(argv);
}
strncpy(gOutFileName, argv[cnt], 256);
}
else if (ch == 'k' || ch == 'K')
{
if (k_flag == true)
{
fprintf(stderr, "Error! Already specifed a key file.\n\n");
usage(argv);
}
k_flag = true;
cnt++;
if (cnt >= argc)
{
fprintf(stderr, "Error! Must specify a filename after '-k'\n\n");
usage(argv);
}
strncpy(gKeyFileName, argv[cnt], 256);
}
else if (ch == 'p' || ch == 'P')
{
if (p_flag == true)
{
fprintf(stderr, "Error! Already specifed a password.\n\n");
usage(argv);
}
p_flag = true;
cnt++;
if (cnt >= argc)
{
fprintf(stderr, "Error! Must enter a password after '-p'\n\n");
usage(argv);
}
strncpy(gPassword, argv[cnt], 256);
}
else if (ch == 'r' || ch == 'R')
{
int x;
cnt++;
if (cnt >= argc)
{
fprintf(stderr, "Error! Must enter number between 1 and 3 after '-r'\n\n");
usage(argv);
}
x = atoi(argv[cnt]);
if (x < 1 || x > 3)
{
fprintf(stderr, "Warning! Entered bad value for number of rounds. Setting it to one.\n\n");
x = 1;
}
gNumRounds = x;
}
else
{
fprintf(stderr, "Error! Illegal option in argument. %s\n\n", argv[cnt]);
usage(argv);
}
cnt++;
} // end while
if (gOp == 0)
{
fprintf(stderr, "Error! Encrypt or Decrypt must be specified.\n\n)");
err_flag = true;
}
if (i_flag == false)
{
fprintf(stderr, "Error! No input file specified.\n\n");
err_flag = true;
}
if (k_flag == false)
{
fprintf(stderr, "Error! No key file specified.\n\n");
err_flag = true;
}
if (p_flag == false)
{
fprintf(stderr, "Warning! Using default 'password'.\n\n");
}
if (o_flag == false && err_flag == false) // no need to do this if we have errors
{
strcpy(gOutFileName, gInFileName);
if (gOp == 1) // encrypt
{
strcat(gOutFileName, ".enc");
}
if (gOp == 2) // decrypt
{
strcat(gOutFileName, ".dec");
}
}
if (err_flag)
{
usage(argv);
}
return;
} // parseCommandLine
这是我一直在研究的主要功能:
int main(int argc, char* argv[])
{
// open the input and output files
gfptrIn = openInputFile(gInFileName);
gfptrOut = openOutputFile(gOutFileName);
fclose(gfptrIn);
fclose(gfptrOut);
return 0;
}
试图弄清楚如何通过使用 main 函数中的openInputFile(char* filename)
和等函数打开我想要的文件,这是一个文本文件openOutputFile(char* filename)
,但我不知道如何。