저는 C++을 처음 접했습니다. 나는 구조와 물체에 대한 소개의 수업을 지금 takking하고있다. 우리는 finnaly 파일을 넘었고 텍스트 파일을 암호화하는 프로그램을 작성했습니다. 단지 내 자신의 즐거움과 지식 (이것은 숙제가 아닙니다). 나는 암호화를 작성하지 않았지만, 그것이 가장 쉽다고 말했기 때문에 개인 키가 될 것입니다. 그리고 이것은 이것과 같은 것을 처음 시도하는 것입니다. 어쨌든 저는 지금 코드를 함수로 작성하여 작동하는지 확인한 다음 나중에 클래스로 만들고 프로그램을 확장합니다. 그래서, 지금 내 코드가 열리고 파일을 씁니다. 암호화하기 전에 cmd 창에서 방금 쓴 파일을보고 싶기 때문에 이전과 이후를 볼 수 있습니다. Heres the code :cmd 프롬프트 창에 파일이 표시되지 않습니다.
//This program will create, store and encrypt a file for sending over the inernet
#include<iostream>
#include<cstdlib>
#include<iomanip>
#include<string>
#include<fstream>
#include"fileSelect.h"
using namespace std;
void openFile(fstream &);
void readFile(fstream &);
int main() {
//output file stream
fstream outputStream;
fstream inputStream;
string fileName, line;
openFile(outputStream);
readFile(inputStream);
system("pause");
return 0;
}
//open file Def
void openFile(fstream &fout){
string fileName;
char ch;
cout<<"Enter the name of the file to open: ";
getline(cin, fileName);
//try to open the file for writing
fout.open(fileName.c_str(),ios::out);
if(fout.fail()){
cout<<"File, "<<fileName<<" failed to open.\n";
exit(1);
}
cout<<"Enter your message to encrypt. End message with '&':\n";
cin.get(ch);
while(ch!='.'){
fout.put(ch);
cin.get(ch);
}
fout.close();
return;
}
void readFile(fstream &fin){
string fileName, line;
cout<<endl;
cout<<"Enter the name of the file to open: ";
getline(cin, fileName);
//check file is good
if(fin.fail()){
cout<<"File "<<fileName<<" failed to open.\n";
exit(1);
}
cout<<"Opening "<<"'"<<fileName<<"'" <<endl;
//cout<<"Enter the file to open: ";
//cin>>fileName;
fin.open(fileName.c_str(),ios::in);
//readFile(inputStream);
if(fin){
//read in data
getline(fin,line);
while(fin){
cout<<line<<endl;
getline(fin,line);
}
fin.close();
}
else{
cout<<"Error displaying file.\n";
}
return ;
}
이것은 컴파일되고 실행됩니다. openFile()이 주석 처리되고 readFile() 함수가 단독으로 호출되면 파일은 openFile()에서 작성된 내용을 읽습니다. 그것은 내가 원하는 것처럼 그것을 한 번 해보지 않을 것이다. 그것은 아마도 내가 놓치고있는 간단한 수정 일 뿐이며, 지금은 약간의 두통이되고 있습니다. 어떤 도움이 절실히 받아 들여질 것입니다. 감사합니다.
하나의 파일을 열고 싶지 않습니다. 그것을 읽고 다른 파일에 씁니 까? 또는 사용자 입력을 읽고 파일에 기록 하시겠습니까? 질문을 요약 할 수 있습니까? – stardust
나는 wasnt가 명확하게 만들었다 고 생각한다.나는 파일을 열고, 그것에 쓰고, 그 같은 파일을 열고, 방금 cmd로 쓰여진 것을 읽고 싶다. 나는 기록 된 것을보기 위해 메모장으로 가고 싶지 않다. –
그러면'openFile'에서 열어야 할 것이 무엇인지 물을 때 같은 파일 이름을주고 싶다는 뜻입니까? 그게 효과가 없을거야? – stardust