Q
編程C
-1
A
回答
0
從你的問題1,即菜單, 1)我想要爲例菜單:[1] 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);
}
如果要打印到文件的Q2,
file = fopen(filename, "a");
fprintf(file, "%s", line);
fclose(file);
+0
我想我明白了,我試圖修復它,並感謝你;) – NoWorries 2013-02-14 11:07:35
0
我居然做了一個 「文件EN /解密」 日前。它不僅有一個相當不錯的選項菜單,它還處理filehandeling。不是最好的評論,也許有點混亂,但我很肯定,如果你閱讀它,你會發現它很有用,或者至少得到一些檢查。
我推薦首先閱讀「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^)[email protected];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");
}
對不起,我的英語不好。
+0
謝謝你,我會嘗試它:) – NoWorries 2013-02-14 10:51:24
相關問題
- 1. C彙編編程
- 2. C#線程編程
- 3. c編程scanf
- 4. Socket編程C
- 5. C++元編程
- 6. c#winform編程
- 7. C編程strtok
- 8. C編程:+ = vs = +
- 9. c socket編程
- 10. C++編程
- 11. C編程MPI
- 12. C++編程板
- 13. C++,Windows編程
- 14. C++編程
- 15. HTML&C編程
- 16. VOIP C++編程
- 17. C編程printf
- 18. FWRITE C編程
- 19. C++ OOP編程
- 20. C++編程GUI
- 21. 在C++編程
- 22. C編程XML
- 23. C編程 - getopt
- 24. 用C編寫outlook編程#
- 25. C編程編譯問題
- 26. 用C編寫Socket編程#
- 27. 底層編程的C/C++
- 28. C編程按比例組合編程
- 29. C編程中的2D遊戲編程
- 30. 在c編程的套接字編程
這是'return(printf);'你的程序的一部分還是錯字? – Ganesh 2013-02-13 16:27:48
可能你應該詳細說明'scanf(「%s」,fileName);'因爲在fileName'char fileName [30];'中只有30個字符的空間。設想一個用戶進入。 「ThisFilenameIsittleittleBitTooLongToFitInOurThirtyCharacterArray」或類似的東西。 – mikyra 2013-02-13 17:00:50
好吧,我會嘗試;)thnx – NoWorries 2013-02-14 10:51:59