2017-04-04 34 views
-4

シーザー暗号の暗号化では、このコードがあります。このプログラムは、ユーザーが書き込んだテキストを使用します。しかし、私はこれを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

このコードをコピーしてどこかから貼り付けましたか?

他の誰もがすでに指摘しているように、コードにかなりの大きな問題があるので、私が一度も言及していないものに触れてみましょう。しかし、開始するにはmainintと宣言する必要があります。戻り値は、プログラムが正しく終了したかどうかを示しています。

ここにオリジナルのコードが書式設定されています。私はconio.hを削除しました。何年も使用されなくなったため、ここでは必要ありません。 hereは、ここで何度も共有されているすばらしい記事ですが、使用することを控えるべき理由を説明していますが、System("CLS");で行うことができる画面をクリアしていました。

#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から再度実行したい –

関連する問題