私はこのコードについて助けが必要です:
plzは私の質問を削除します!
あなたの質問1、すなわちメニューから、1)私は例のためのメニューが欲しいです:[l] oad-[s] ave、私が「L」と書くとき、私はに行きます:
printf( "船の情報を含むファイルの名前を入力してください:");
この部分では、char inpChar;
do {
printf("enter menu: [l]oad - [s]ave\n");
scanf("%c", &inpChar);
} while((inpChar != 'l') && (inpChar != 'L') && (inpChar != 's') && (inpChar != 'S'));
if((inpChar == '1') || (inpChar == 'L'))
{
printf("Enter the name of the file containing ship information: ");
scanf("%s", filename);
}
ファイルへの印刷の第2四半期については、
file = fopen(filename, "a");
fprintf(file, "%s", line);
fclose(file);
私は実際に最近「ファイルen/decrypter」を実行しました。それはかなりまともなオプションメニューを持っているだけでなく、ファイル処理も扱います。最高のコメントではなく、少し厄介かもしれませんが、それを読んだら、役に立つか、少なくともある程度のインスピレーションを得ることができると確信しています。
最初に「man()」関数を読むことをお勧めします。または、コードの概要をわかりやすくするために、「。/a.out-h」を使用してコンパイルして実行することをお勧めします。
#include <stdio.h>
#include <stdlib.h>
/* ENCRYPTING FILES
15.Dec.2012
By Morten Lund */
#define ALPHA "\\~!?=#)%(&[{]}|@,.;:-_*^+<>1234567890abcdefghijklmnopqrstuvwxyzABCDEFG HIJKLMNOPQRSTUVWXYZ/$"
#define CRYPT_ALPHA "/#UwVM{>12E+j,4amN&stin]Ao_6}Q|7q[!f(FJKcLBz^)XYdhZkl9e?Wv@u;35yHrg< P*pSGTx=Ib~%R0$O.8:-DC\\"
#define ALPHA_SIZE sizeof(ALPHA)
void man();
void crypt(FILE *fpIN, FILE *fpOUT, char mode);
void replaceOriginal(char *inFileName, char *outFileName);
int main(int argc, char* argv[]) {
// Check if ALPHA and CRYPT_ALPHA is same size
if (sizeof(ALPHA) != sizeof(CRYPT_ALPHA)) {
printf("ALPHA and CRYPT_ALPHA is not the same size!!");
return EXIT_FAILURE;
}
/*------START-------OPTIONS MENU:-------START-----*/
char *inFileName; // Input file name/dir
char *outFileName = "out.txt"; // Output file name/dir
char mode = 'N'; // E=encrypt, D=decrypt mode. N=Not set
int replaceMode = 0; // Replace original file :default is OFF
int x;
for (x = 1; x < argc; x++) {
if (strcmp(argv[x], "-e") == 0) { // Encryption mode
mode = 'E';
}
if (strcmp(argv[x], "-d") == 0) { // Decryption mode
mode = 'D';
}
if (strcmp(argv[x], "-r") == 0) { // Replace original file
replaceMode = 1;
}
if (strcmp(argv[x], "-f") == 0) { // Input file option
x++;
inFileName = argv[x];
}
if (strcmp(argv[x], "-o") == 0) { // Output file option
x++;
outFileName = argv[x];
}
}
/*------END-------OPTIONS MENU:--------END-------*/
FILE *fpIN, *fpOUT; // Creating file pointer to in and output file
// Check if mode is set to E OR D. Check if input and output file is set.
if (mode == 'E' || mode == 'D' && inFileName != NULL && outFileName != NULL) {
// If inFileName exists and is readable, it is opened, else return EXIT_FAILURE
if ((fpIN = fopen(inFileName, "r")) == NULL) {
printf("\nERROR! Input file: %s\nFile does not exist or is not readable!\n\n", inFileName);
fclose(fpIN);
return EXIT_FAILURE;
}
// Check if Output file allready exist.
if ((fopen(outFileName, "r")) != NULL) {
printf("\nOutput file: %s\nFile allready exist, do you want to overwrite it? [Y/N]: ", outFileName);
char choice;
scanf("%c", &choice);
while (choice == 'N' || choice == 'n')
return EXIT_FAILURE;
}
// If outFileName is writeble, it is opened, else return EXIT_FAILURE
if ((fpOUT = fopen(outFileName, "w")) == NULL) {
printf("\nERROR! Output file: %s\n File or location is not writeble!\n\n", outFileName);
fclose(fpIN); // Closing input pointer
fclose(fpOUT); // and output pointer
return EXIT_FAILURE;
}
printf("\nInput file: %s\n", inFileName);
printf("Output file: %s\n", outFileName);
printf("mode (E)ncryption (D)ecrypting: %c\n", mode);
crypt(fpIN, fpOUT, mode);
fclose(fpIN); // Closing input pointer
fclose(fpOUT); // and output pointer
} else
man();
// Check if set, and if, then replace original file
if (replaceMode)
replaceOriginal(inFileName, outFileName);
return EXIT_SUCCESS;
}
void crypt(FILE *fpIN, FILE *fpOUT, char mode) {
char *alphaFrom, *alphaTo; // Alphabet to copy from, to
if (mode == 'E') { // If mode=Encrypt, copy from ALPHA to CRYPT_ALPHA
alphaFrom = ALPHA;
alphaTo = CRYPT_ALPHA;
}
else if (mode == 'D') { // If mode=Decrypt, copy from CRYPT_ALPHA to ALPHA
alphaFrom = CRYPT_ALPHA;
alphaTo = ALPHA;
}
char curChar; int i; // Current character, and counter (i)
while ((curChar = fgetc(fpIN)) != EOF) { // Assign curChar from fpIN as long it's not EOF
for (i = 0; i < ALPHA_SIZE; i++) { // For every character in alphabet size
if (curChar == alphaFrom[i]) // If character = alphaFrom, replace with alphaTO
fputc(alphaTo[i], fpOUT);
}
if (curChar == '\n') // Finding \n and inserting with \n
fputc('\n', fpOUT);
if (curChar == '\t') // Finding \t and inserting with \t
fputc('\t', fpOUT);
}
}
void replaceOriginal(char *inFileName, char *outFileName) {
if (remove(inFileName) == 0) { // Remove inFileName
if (rename(outFileName, inFileName) == 0) // Rename outFileName
printf("File %s successfully replaced\n", inFileName);
} else
printf("Couldn't delete %s.. Replace failed", inFileName);
}
void man() {
printf("\n.....Simple Crypter.....\n\nUsage: sCrypter [Arguments] -f [input file]\n\tOr: sCrypter -e -f file.txt\n\tOr: sCrypter -d -r -f file.txt\n\tOr: sCrypter -e -f file.txt -o file2.txt\n\n");
printf(" [-h] This page.\n");
printf(" [-f] Set input file.\n");
printf(" [-o] Set output file [OPTIONAL] default: out.txt.\n");
printf(" [-e] Encrypting input file.\n");
printf(" [-d] Decrypting input file.\n");
printf(" [-r] Replace the input file with the new output file.\n\n");
printf("Warning! If [-r] is used, out.txt will be used as tmp file.\n");
printf("[-o] can be used in combination with [-r] to insure that another tmp file is used.\n\n");
}
英語が下手でごめんなさい。