2017-04-04 30 views
-4

代碼中使用文件愷撒密碼加密Caeser密碼加密,我有這樣的代碼。該程序使用用戶編寫的文本。但我想從txt文件中讀取並運行它。如何在C

#include<stdio.h> 
#include <conio.h> 
#include<string.h> 

void main() 
{ 
    int key,i; 
    char [30]; 
    clrscr(); 
    printf("\n enter plain text : "); 
    gets(data); 
    printf("\ enter key value : "); 
    scanf("%d",&key); 
    { 
    for (i=o;i<strlen(data);i++) { 
     if (data[i]==' ') {} 
     else 
     { 
     if (data[i]>='x') 
     { 
      data[i]=data[i]-26; 
     } 
     data[i]=data[i]+key; 
     } 
    } 
    } 
    printf("your cipher text is : %s",data); 
    getch(); 
} 
+1

請請你幫個忙,並格式化你的代碼。它站在這裏是不可讀的。 –

+3

只需從文件中讀取它? – Fredrik

+1

請勿使用'gets'。這是危險的,自C99標準以來已被棄用,並從最新的C11標準中徹底取消。根據平臺使用標準C ['fgets'](http://en.cppreference.com/w/c/io/fgets)函數或POSIX ['getline'](http://pubs.opengroup.org /onlinepubs/9699919799/functions/getline.html)函數。 –

回答

2

你剛剛從某處複製並粘貼了這段代碼嗎?

正如其他人已經指出的那樣,也有你的代碼一些相當大的問題,所以我會嘗試和觸摸他們並沒有在整個提到的那些。首先,您應該將main聲明爲int。返回值告訴你程序是否正確退出,這不僅僅是約定。

這是您的原代碼,格式化。我刪除了conio.h,因爲它已被棄用多年,此外您不需要它。所有它所做的是清除您的屏幕,你可以用System("CLS");做,雖然here,是指已共享多次在這裏,這樣解釋了爲什麼你應該避免使用它一個偉大的文章。

#include<stdio.h> 
#include<string.h> 

int main() 
{ 
    int key,i; 

    // original: char [30]; 
    // I'm guess you copied and pasted wrong? 
    char plainText[30]; 

    printf("\n enter plain text : "); 
    gets(data); 
    printf("\nenter key value : "); 
    scanf("%d",&key); 

    // You had brackets here, essentially creating an unnecessary 
    // block of code. 
    for(i=o;i<strlen(data);i++) 
    { 
    // Where did the "data" variable come from? You haven't declared it 
    // and you're trying to get its length in this for loop 
    if (data[i]==' ') 
    { 
     // ? 
    } 
    else 
    { 
     if (data[i]>='x') 
     { 
      // Instead of this approach I would go with a modulus operation 
      // If you're not familiar with modular arithmetic I really recommend you look 
      // it up. It's absolutely essential in cryptography. It's central to both 
      // symmetric and asymmetric key cryptography. 
      data[i]=data[i]-26; 
     } 

    // This is the heart of the Caesar cipher right here. This is where you're actually 
    // shifting the characters, so it's a literal Caesar cipher 
    data[i]=data[i]+key; 
    } 
    } 

    // Again, you haven't declared "data" so you can't call it. I'm guessing you didn't 
    // try to compile this program because it is teeming with errors GCC, Clang, and VC++ 
    // would have caught immediately 
    printf("your cipher text is : %s",data); 
    getch(); 

    // Remember to make your "main" function return an <code>int</code>. This value is 
    // very important, especially when your program gets called by another program because 
    // it's how your program communicates that it ran successfully, or something went 
    // wrong. In that case you could set a specific error code to know exactly 
    // what went wrong and were 

    // Example: 
    int x = 1; 
    if (x == 1) 
     exit(4); 

    // This program would now exit with a status of 4, which you would then know was due 
    // to this function. This was a contrived example, but hopefully you get the gist 
    return 0; 
} 
+0

謝謝,,,,但我想執行這個程序,,,,從文件,thnx再次 –