質問
したがって、教師はこの課題を提起しました:
合衆国法執行機関のネットワーク司令部に雇われ、解読する必要のあるヌル暗号を含むファイルが与えられました。
つまり、最初のファイル(例として)については、他のすべての文字が正しい(つまり、「hielqlpo」はこんにちは(最初の文字で始まると仮定))。最初の質問は、どのようにファイルを読むか?ドキュメントはデスクトップのフォルダーにあり、ファイルの名前はdocument01.cryです。このファイルをプログラムに入れるために必要なコマンドがわかりません。
私はまた、手紙をつかんで手紙を飛ばす方法をあまりよく知りませんが、正直なところ、私はその質問を投稿する前にそれをいじりたいです!だから今のところ...私の質問はタイトルに述べられているとおりです:C ++で読むためにどのようにファイルを取得しますか?
違いがある場合は(確かにそうだと思いますが)、Visual C ++ 2008 Express Editionを使用しています(無料で気に入っているためです!これまでに添付したこともありますので、続けてください)非常に基本的なことを心に留めて...そして最後にgetchar();
を追加したので、適切に実行されると、ウィンドウが開いたままになるのでそれを見ることができます(Visual Expressは実行が終了するとすぐにウィンドウを閉じる傾向があるため。)
これまでのコード:
#include<iostream>
using namespace std;
int main()
{
while (! cin.eof())
{
int c = cin.get() ;
cout.put(c) ;
}
getchar();
}
PS:このコードは、すべての文字を取得して出力することを理解しています。今のところは問題ありません。一度ファイルを読み取れたら、そこからいじることができると思います。また、C ++で1冊か2冊の本を読んで、何かがポップアップして叫ぶのを見るために<!> quot; Pick me!<!> quot;どうもありがとう!
EDIT ::また、好奇心、盛です。必要なファイルを入力する方法はありますか? (つまり:
char filename;
cout << "Please make sure the document is in the same file as the program, thank you!" << endl << "Please input document name: " ;
cin >> filename;
cout << endl;
ifstream infile(filename, ios::in);
このコードは機能しません。 charをconst char *に変換できないというエラーを返します。この問題はどのように修正できますか?
編集2: パート2については気にしないでください!助けてくれてありがとう!
解決 5
わかりました!正直なところ、誰も助けてくれませんでした。それは、3つの組み合わせとそれらからのコメントです。どうもありがとうございました!使用したコードとドキュメントのコピーを添付しました。プログラムはすべての文字を読み取り、解読されたテキストを吐き出します。 (IE:1h.e0l / lqo is hello)次のステップ(追加クレジット)は、ユーザーが入力する次の文字を読み込む前にスキップする文字数を入力するように設定することです。
このステップは自分で行うつもりですが、再び、皆さんの支援に感謝します!このサイトの素晴らしさを、一度に1行のコードでもう一度証明してください!
EDIT ::ユーザー入力を受け入れ、再コンパイルせずに複数の使用を許可するように調整されたコード(私はそれが巨大なずさんな混乱のように見えますが、それは非常にコメントされている理由です...きちんとした)
#include<iostream>
#include<fstream> //used for reading/writing to files, ifstream could have been used, but used fstream for that 'just in case' feeling.
#include<string> //needed for the filename.
#include<stdio.h> //for goto statement
using namespace std;
int main()
{
program:
char choice; //lets user choose whether to do another document or not.
char letter; //used to track each character in the document.
int x = 1; //first counter for tracking correct letter.
int y = 1; //second counter (1 is used instead of 0 for ease of reading, 1 being the "first character").
int z; //third counter (used as a check to see if the first two counters are equal).
string filename; //allows for user to input the filename they wish to use.
cout << "Please make sure the document is in the same file as the program, thank you!" << endl << "Please input document name: " ;
cin >> filename; //getline(cin, filename);
cout << endl;
cout << "'Every nth character is good', what number is n?: ";
cin >> z; //user inputs the number at which the character is good. IE: every 5th character is good, they would input 5.
cout << endl;
z = z - 1; //by subtracting 1, you now have the number of characters you will be skipping, the one after those is the letter you want.
ifstream infile(filename.c_str()); //gets the filename provided, see below for incorrect input.
if(infile.is_open()) //checks to see if the file is opened.
{
while(!infile.eof()) //continues looping until the end of the file.
{
infile.get(letter); //gets the letters in the order that that they are in the file.
if (x == y) //checks to see if the counters match...
{
x++; //...if they do, adds 1 to the x counter.
}
else
{
if((x - y) == z) //for every nth character that is good, x - y = nth - 1.
{
cout << letter; //...if they don't, that means that character is one you want, so it prints that character.
y = x; //sets both counters equal to restart the process of counting.
}
else //only used when more than every other letter is garbage, continues adding 1 to the first
{ //counter until the first and second counters are equal.
x++;
}
}
}
cout << endl << "Decryption complete...if unreadable, please check to see if your input key was correct then try again." << endl;
infile.close();
cout << "Do you wish to try again? Please press y then enter if yes (case senstive).";
cin >> choice;
if(choice == 'y')
{
goto program;
}
}
else //this prints out and program is skipped in case an incorrect file name is used.
{
cout << "Unable to open file, please make sure the filename is correct and that you typed in the extension" << endl;
cout << "IE:" << " filename.txt" << endl;
cout << "You input: " << filename << endl;
cout << "Do you wish to try again? Please press y then enter if yes (case senstive)." ;
cin >> choice;
if(choice == 'y')
{
goto program;
}
}
getchar(); //because I use visual C++ express.
}
編集:::
テキストを挿入しようとしましたが、正しく出力することができませんでした。コーディングのように文字の一部を扱い続けました(つまり、アポストロフィはボールドコマンドと同等です)。 <!> quot; 0h1e.l9lao <!> quot; .txtへの括弧なしで、同じ結果が得られます。
助けてくれてありがとう!
他のヒント
ファイル操作を行うには、正しいインクルードが必要です:
#include <fstream>
次に、メイン関数でファイルストリームを開くことができます:
ifstream inFile( "filename.txt", ios::in );
または、出力用:
ofstream outFile( "filename.txt", ios::out );
その後、cinを使用する場合はinFileを使用し、coutを使用する場合はoutFileを使用できます。完了したらファイルを閉じるには:
inFile.close();
outFile.close();
[EDIT]コマンドライン引数のサポートを含む [編集]メモリリークの可能性を修正 [編集]欠落している参照を修正しました
#include <fstream>
#include <iostream>
using namespace std;
int main(int argc, char *argv){
ifstream infh; // our file stream
char *buffer;
for(int c = 1; c < argc; c++){
infh.open(argv[c]);
//Error out if the file is not open
if(!infh){
cerr << "Could not open file: "<< argv[c] << endl;
continue;
}
//Get the length of the file
infh.seekg(0, ios::end);
int length = infh.tellg();
//reset the file pointer to the beginning
is.seekg(0, ios::beg);
//Create our buffer
buffer = new char[length];
// Read the entire file into the buffer
infd.read(buffer, length);
//Cycle through the buffer, outputting every other char
for(int i=0; i < length; i+= 2){
cout << buffer[i];
}
infh.close();
}
//Clean up
delete[] buffer;
return 0;
}
トリックを行う必要があります。ファイルが非常に大きい場合、おそらくファイル全体をバッファにロードすべきではありません。
あなたの質問には答えられましたが、2つの小さなヒント: 1)xとyをカウントして、奇数または偶数のキャラクターであるかどうかを確認する代わりに、次のことができます。
int count=0;
while(!infile.eof())
{
infile.get(letter);
count++;
if(count%2==0)
{
cout<<letter;
}
}
%は本質的に「割ったときの余り」を意味するため、11%3は2です。上記では、奇数または偶数になります。
2)Windowsでのみ実行する必要がある場合:
system("pause");
実行が終了してもウィンドウは開いたままになるため、最後のgetchar()呼び出しは不要です(動作させるには#include<Windows.h>
が必要な場合があります)。