-1

我需要有关此代码的帮助:

请删除我的问题!

4

2 回答 2

0

我最近实际上做了一个“文件加密/解密”。它不仅有一个相当不错的选项菜单,它还处理文件处理。不是最好的评论,也许有点乱,但我很确定,如果你读了它,你会发现它很有用,或者至少得到一些启发。

我建议先阅读“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");
}

对不起,我的英语不好。

于 2013-02-13T18:44:18.310 回答
0

从您的问题 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);
}

对于第二季度的打印归档,

file = fopen(filename, "a");
fprintf(file, "%s", line);
fclose(file);
于 2013-02-13T16:34:08.840 回答