C++ C++;尝试读取文件并输出到其他文本文件

C++ C++;尝试读取文件并输出到其他文本文件,c++,C++,我有一个我想修改的学校项目。我希望能够从一个文件中读取数据,并在解码原始文件后输出到一个不同的文件。一切都很好,但当我打开新文件时,只有最后一个字被解码,而不是整个句子。我认为这与while(!inFS.eof())循环有关,但我不知道如何修复它。任何帮助都将不胜感激。非常感谢。这是代码 // Secret Message App #include <iostream> #include <fstream> #include <vector> #include

我有一个我想修改的学校项目。我希望能够从一个文件中读取数据,并在解码原始文件后输出到一个不同的文件。一切都很好,但当我打开新文件时,只有最后一个字被解码,而不是整个句子。我认为这与while(!inFS.eof())循环有关,但我不知道如何修复它。任何帮助都将不胜感激。非常感谢。这是代码

// Secret Message App
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;

int main() {
    vector<char> normalV(26);
    vector<char> cipherV(26);
    string toDec = "";
    string beenDec = "";
    string usrInptOptn = "default";
    ifstream inFS;
    ofstream outFS;    
    string filename;

    // Vector for Message letters and Cipher Symbols
    normalV.at(0) = 'a'; cipherV.at(0) = '!';
    normalV.at(1) = 'b'; cipherV.at(1) = '^';
    normalV.at(2) = 'c'; cipherV.at(2) = '&';
    normalV.at(3) = 'd'; cipherV.at(3) = '*';
    normalV.at(4) = 'e'; cipherV.at(4) = '@';
    normalV.at(5) = 'f'; cipherV.at(5) = '(';
    normalV.at(6) = 'g'; cipherV.at(6) = ')';
    normalV.at(7) = 'h'; cipherV.at(7) = '-';
    normalV.at(8) = 'i'; cipherV.at(8) = '#';
    normalV.at(9) = 'j'; cipherV.at(9) = '_';
    normalV.at(10) = 'k'; cipherV.at(10) = '=';
    normalV.at(11) = 'l'; cipherV.at(11) = '+';
    normalV.at(12) = 'm'; cipherV.at(12) = '[';
    normalV.at(13) = 'n'; cipherV.at(13) = '{';
    normalV.at(14) = 'o'; cipherV.at(14) = '$';
    normalV.at(15) = 'p'; cipherV.at(15) = ']';
    normalV.at(16) = 'q'; cipherV.at(16) = '}';
    normalV.at(17) = 'r'; cipherV.at(17) = ';';
    normalV.at(18) = 's'; cipherV.at(18) = ':';
    normalV.at(19) = 't'; cipherV.at(19) = ',';
    normalV.at(20) = 'u'; cipherV.at(20) = '%';
    normalV.at(21) = 'v'; cipherV.at(21) = '<';
    normalV.at(22) = 'w'; cipherV.at(22) = '.';
    normalV.at(23) = 'x'; cipherV.at(23) = '>';
    normalV.at(24) = 'y'; cipherV.at(24) = '/';
    normalV.at(25) = 'z'; cipherV.at(25) = '?';

    // Get secret message

        cout << "\nSpecify file to Open: example secret.txt." << endl;
        cin >> filename;
        cout << "Opening secret message file... " << filename << endl;
        inFS.open(filename.c_str());

        if (!inFS.is_open()) {
        cout << "Could not open file secret.txt.\n";
        return 1;
        }

    while (!inFS.eof()) {
        inFS >> toDec;
    } 

    while (toDec.length() == 0); 

    beenDec = toDec;

    // Loop Count Variables
    int i = 0;
    int n = 0;

    // Decrypt secret message
    for (i = 0; i < toDec.size(); ++i) { // This Loop cycles for each character in the input message
        for (n = 0; n < 26; ++n) {      // This Loops the if statement 
            if (toDec.at(i) == cipherV.at(n)) { //This converts the cipher into normal letters from the "normalV" vector
                beenDec.at(i) = normalV.at(n);
            }
        }
    }

    inFS.close(); // Done with file, so close it

    outFS.open("decrypted.txt");    
        if (!outFS.is_open()) {
        cout << "Could not open file secret.txt.\n";
        return 1;
        }
    outFS << beenDec << endl;
    outFS.close(); // Done with file, so close it       

    cout << "Decrypted message: " << beenDec << "  Has been saved to: decrypted.txt" << endl;

    system("pause");
    return 0;
}
//秘密消息应用程序
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
向量法向量v(26);
载体密码病毒(26);
字符串toDec=“”;
字符串beenDec=“”;
字符串usrInptOptn=“default”;
IFS;
流出流的流量;
字符串文件名;
//信息字母和密码符号的矢量
在(0)处的normalV.at='a';在(0)处的cipherV.at='!';
在(1)处的normalV.at='b';在(1)处的cipherV.at='^';
在(2)处的normalV.at='c';在(2)处的cipherV.at='&';
在(3)处的normalV.at='d';在(3)处的cipherV.at='*';
在(4)处的normalV.at='e';在(4)处的cipherV.at='@';
在(5)处的正规v.at='f';在(5)处的密码v.at='(';
正态v.at(6)='g';密码v.at(6)=');
正态v.at(7)='h';密码v.at(7)='-';
正规v.at(8)='i';密码v.at(8)='#';
正规v.at(9)='j';密码v.at(9)='u';
(10)处的normalV.at='k';在(10)处的cipherV.at='k';
正态v.at(11)='l';密码v.at(11)='+';
(12)处的normalV.at='m';在(12)处的cipherV.at='[';
正规v.at(13)='n';密码v.at(13)='{';
在(14)处的normalV.at='o';在(14)处的cipherV.at='$';
(15)处的normalV.at='p';在(15)处的cipherV.at=']';
正规v.at(16)='q';密码v.at(16)='}';
(17)处的normalV.at='r';在(17)处的cipherV.at=';';
在(18)处的normalV.at='s';在(18)处的cipherV.at=':';
正态v.at(19)='t';密码v.at(19)=',';
(20)处的normalV.at='u';在(20)处的cipherV.at='%';
在(21)处的normalV.at='v';在(21)处的cipherV.at='';
正态v.at(24)=“y”;密码v.at(24)=“/”;
正态v.at(25)='z';密码v.at(25)='?';
//收到密信
cout文件名;

是的,你是对的,这是错误

while (!inFS.eof()) {
    inFS >> toDec;
} 
请使用以下内容代替上述LOC

string temp = ""; // Declaring a temp string variable wherever you want

while (inFS >> temp) {
    toDec += temp;
}
现在它可以工作了

谢谢和问候, Ankur Gupta

cout文件名;
    cout << "\nSpecify file to Open: example secret.txt." << endl;
    cin >> filename;
    cout << "Opening secret message file... " << filename << endl;
    inFS.open(filename.c_str());

    if (!inFS.is_open()) {
         cout << "Could not open file secret.txt.\n";
         return 1;
    }
    outFS.open("decrypted.txt");    
    if (!outFS.is_open()) {
         cout << "Could not open file secret.txt.\n";
         return 1;
    }

    while (inFS >> toDec) {
    beenDec = toDec;
    int i = 0;
    int n = 0;

    // Decrypt secret message
    for (i = 0; i < toDec.size(); ++i) { // This Loop cycles for each character in the input message
        for (n = 0; n < 26; ++n) {      // This Loops the if statement 
            if (toDec.at(i) == cipherV.at(n)) { //This converts the cipher into normal letters from the "normalV" vector
                beenDec.at(i) = normalV.at(n);
            }
        }
    }
     outFS << beenDec << endl;
     cout << "Decrypted message: " << beenDec << "  Has been saved to: decrypted.txt" << endl;
} 

inFS.close(); // Done with file, so close it
outFS.close(); // Done with file, so close it       
system("pause");
return 0;
}

请阅读关于创建的部分。您可以从这段代码中删去很多内容,但仍然保留错误,这有助于我们(和您)缩小问题的范围。突击测验:如您所知,
inFS>>toDec;
替换了
toDec
(a
std::string
)下一个单词从代码< INFS>代码>,因为这就是<>代码> >代码>的工作方式。你知道吗?如果没有,请查看你的C++书籍以获取更多信息。但是如果你确实知道,你的流行测试将是:在<代码>的结尾,而循环,其唯一内容是这个语句,将会是<代码>内容> DEC> <代码>。甚至让它成为一个多项选择突击测验:1)文件的全部内容,2)从中读取的最后一个单词。您正在逐字阅读并使用读取的最后一个单词。如果在获得单词后将函数的其余部分放入白色循环中,它可能会工作。因为我不知道您的文件结构。而(!inFS.eof()){inFS>>toDec;//在此处插入其余代码}机密文件存储了一些需要解码为解密文件的句子或短语。是逐行解码,还是仅仅一个单词或整个内容。与其使用while循环,不如使用std::istreambuf_迭代器。这样做很有效,但是secret.txt文件中的句子占用了所有空格。感谢您的支持帮助。因为,您没有写入向量中不存在的任何其他字符,即(a-z)之外的字符。