所以这是我正在尝试制作的程序:创建密码生成器,它使用字典和匹配文件通过用它们的匹配替换字典单词中的字母来生成密码。示例:字典文件:apple loop 匹配文件:a 4 e 3 o 0 输出:4pple appl3 4ppl3 l0op lo0p l00p
这是我的解决方案:
#include <stdio.h>
#include <string.h>
/* function prototypes */
int read_source(char* ,char* ,char* );
char* shift_string(char* ,char* );
int write_shifted(char* ,char* );
int main(int argc, char *argv[])
{
/* argv[1]: the source file with the original strings
* argv[2]: the shift file which indicates which characters are to be
* replaced with what
* argv[3]: the file in which the new strings are to be stored*/
if(!argv[1])
{
printf("No source file, can do nothing. Exiting.\n");
return 1;
}
else if (!argv[2])
{
printf("No shift file given, can do nothing. Exiting.\n");
return 2;
}
else if (!argv[3])
{
/* If no output file has been specified, print the
* output directly to the console. */
read_source(argv[1],argv[2],NULL);
return 0;
}
if (read_source(argv[1],argv[2],argv[3])!=0)
{
printf("There has been an error. Exiting.\n");
return 3;
}
if (argv[3])
{
printf("Everything seems to have gone according to plan.\n");
printf("Your output has been stored in \"%s\"\n",argv[3]);
}
return 0;
}
int read_source(char* source_file,char* shift_file,char* out_file)
{
FILE *file_pointer;
file_pointer=fopen(source_file, "r");
/* Exit gracefully if source_file cannot be found. */
if (file_pointer == NULL)
{
printf("Couldn't open \"%s\" for reading.\n",source_file);
return 1;
}
char* new_line;
/* maximum line length, can be changed if needed */
char line[256];
/* Go through the source file. */
while (fgets(line,sizeof line,file_pointer) != NULL)
{
new_line = shift_string(shift_file,line);
if (new_line==NULL)
{
printf("There has been an error with replacing the characters.\n");
return 2;
}
write_shifted(new_line,out_file);
}
int fclose(FILE *file_pointer);
return 0;
}
char* shift_string(char* shift_file,char* source_string)
{
/* This function replaces certain characters in a given source_string as
* specified by shift_file */
/* Open shift file. */
FILE *file_pointer;
char i,j;
file_pointer=fopen(shift_file, "r");
/* Exit gracefully if shift_file cannot be found. */
if (file_pointer == NULL)
{
printf("Couldn't open \"%s\" for reading.\n",shift_file);
return NULL;
}
int k;
/* Determine how long the source_string is for the loop below. */
int length = strlen(source_string);
while (fscanf(file_pointer, "%c %c\n", &i, &j)==2)
{
/* This loop actually does the replacing. */
for (k=0;k<length;k++)
{
if (source_string[k]==i)
{
source_string[k]=j;
}
}
}
int fclose(FILE *file_pointer);
return source_string;
}
int write_shifted(char* new_line,char* out_file)
{
/* This function writes the new strings to out_file
* If no out_file has been given, it will write the
* output to the console.*/
if (out_file==NULL)
{
printf("%s",new_line);
return 0;
}
FILE *file_pointer;
/* Open in a+ mode. If out_file does not yet exist, it will be created.
* If it does exist, it will be appended to instead of overwritten. */
file_pointer = fopen(out_file,"a+");
/* Write the new line */
fprintf(file_pointer,"%s",new_line);
fclose(file_pointer);
return 0;
}
所以问题是现在它只打印最终状态(4ppl3 l00p),但我还需要中间状态(4pple appl3 l0op lo0p)。有人可以给我一些如何制作的线索。提前致谢。:)