iostream正确读取了几行,然后在随后的几行中失败

iostream reads a couple of lines correctly then fails on subsequent lines

本文关键字:几行 失败 然后 读取 iostream      更新时间:2023-10-16

我正在为类创建一个程序,该程序可以对简单的文本文件进行加密和解密。在设置iostreams之前,我能够让程序使用简单的用户输入(cin/cout)。我努力让iostream正常工作,最终还是弄明白了——大部分都是这样。当我的测试文件有多行时,我会遇到问题。它能很好地阅读和翻译前两行,但在第二行之后,我得到的只是jibberish。我想这可能与马车返程的某种怪癖有关;Linefeed在线路的尽头,但我很困惑为什么它在第一到第二线路上有效,而在第二到第三线路及以上线路上无效。我的代码在下面。我现在最好的猜测是,问题存在于从第123行开始的循环中。但我还没能隔离。

如果有人有几分钟的时间来看看这个,我非常感谢你的时间!不过,请尽量只给我指明正确的方向,不要对我的代码的其余部分提出太多更改。这是家庭作业,需要尽可能成为我的工作。我将表明我得到了帮助,使之发挥作用。

如果你想测试这个,在这个解决方案运行的目录中创建一个名为C++test.txt的文件,并在其中放入一些测试文本以供阅读。同样,它似乎在第1行和第2行运行得很好,但在第2行之后失败了。

#include <iostream>
#include<string>
#include<iomanip>
#include<cstring>
#include<fstream>
using namespace std;
inline void keep_window_open() { char ch; cin>>ch; }

char cipher(char, int, char, int);
char calculation(int);
void encrypt();
void decrypt();
void displayMenu(void);
void getMenuSelection(int);
int main (void)
{
bool running=true;
char answer=' ';
cout<<
"Use this program to encrypt/decrypt text.nPlease select an optionn";
while (running==true)                   //allows program to loop until user
{                                       //user selects to exit
displayMenu();                      //function call
cout<<
"nWould you like to start over?n(enter Y for Yes or N for No)n";
cin>>answer;
if (answer=='Y'||answer=='y')
{
running=true;
}
else
{
running=false;
}
}
cout<<"nGoodbye.";

//The next two lines stop the Command Prompt window from closing
//until the user presses a key, including Enter.
cout    << "nPress enter twice to exit." << endl;
cin.ignore(2);
}
///////////////////////////////Width Guage//////////////////////////////////////
///////////////////////////////displayMenu//////////////////////////////////////
//This function simply displays the menu and calls getMenuSelection()
void displayMenu(void)
{
int userInput=0;
cout<< "nPlease select an option:n(Enter 1, 2, or 3)n" ;
cout<< "1. Encryptn" ;
cout<< "2. Decryptn" ;
cout<< "3. Exitn";
cin>>userInput;
if (userInput==1||userInput==2)
{
getMenuSelection(userInput);            //function call
}
else
{
}

}
///////////////////////////////Width Guage//////////////////////////////////////
///////////////////////////////getMenuSelection/////////////////////////////////
void getMenuSelection(int userInput)
{
switch (userInput)
{
case 1:
{
encrypt();
break;
}
case 2:
{
decrypt();
break;
}
}
}
///////////////////////////////Width Guage//////////////////////////////////////
///////////////////////////////encrypt//////////////////////////////////////////
void encrypt()
{
char key[128]={' '}, mess[256]={' '};
string newMess;
int keyLength=0, messLength=0, switchChar=0, i=0;
cout<<"What is the encryption key?n(128 characters or less only)";
cin>>key;
keyLength=strlen(key);
cin.ignore(300,'n');
cout<<"Getting message file C++Test.txtn";
ifstream message;                   //Opens input stream to get C++Test.txt
message.open("C++Test.txt", ifstream::in);
int j=0;                            //loop counter
do                                  //loop to input from file
{
mess[j]=message.get();
j++;
}
while (message.good());
message.close();                    //closes input stream

messLength=strlen(mess);
int k=0;
for (int counter=0;counter<messLength;counter++)
{
if (isalpha(mess[counter]))
{
if (i>keyLength)
{
i=0;
}
else if (i==keyLength-1)
{
newMess.push_back(cipher(mess[counter],keyLength,key[i],
switchChar));
i=0;
}
else
{
newMess.push_back(cipher(mess[counter],keyLength,key[i],
switchChar));
i++;
}
}
else
{
newMess.push_back(mess[counter]);
i++;
}

}
filebuf newMessagefb;
newMessagefb.open("C++TestDecrypt.txt", ios::out);
ostream newMessage (&newMessagefb);
newMessage<<newMess;
newMessagefb.close();
k++;
cout<<"The encrypted message is:nn"<<newMess;
}
///////////////////////////////Width Guage//////////////////////////////////////
///////////////////////////////cipher///////////////////////////////////////////
char cipher (char origMessChar, int keyLength, char origKeyChar, int switchChar)
{
origMessChar=tolower(origMessChar);
origKeyChar=tolower(origKeyChar);
switch (switchChar)
{
case 0://for encryption
{
int messNum=origMessChar-'a';
int keyNum=origKeyChar-'a';
char newMessChar=((messNum+keyNum)%26)+'a';
return (newMessChar);
}
case 1://for decryption
{
int messNum=origMessChar-'a';
int keyNum=26-(origKeyChar-'a');
char newMessChar=((messNum+keyNum)%26)+'a';
return (newMessChar);
}
}
}
///////////////////////////////Width Guage//////////////////////////////////////
///////////////////////////////decrypt//////////////////////////////////////////
void decrypt()
{
char key[128]={' '}, mess[256]={' '};
string newMess;
int keyLength=0, messLength=0, switchChar=1, i=0;
cout<<"What is the encryption key?n(128 characters or less only)";
cin>>key;
keyLength=strlen(key);
cin.ignore(300,'n');
cout<<"Getting message file C++TestDecrypt.txtnn";
ifstream message;                   //Opens input stream to get C++Test.txt
message.open("C++TestDecrypt.txt", ifstream::in);
int j=0;                            //loop counter
do                                  //loop to input from file
{
mess[j]=message.get();
j++;
}
while (message.good());
message.close();                    //closes input stream
messLength=strlen(mess);
for (int counter=0;counter<messLength;counter++)
{
if (isalpha(mess[counter]))
{
if (i>keyLength)
{
i=0;
}
else if (i==keyLength-1)
{
newMess.push_back(cipher(mess[counter],keyLength,key[i],
switchChar));
i=0;
}
else
{
newMess.push_back(cipher(mess[counter],keyLength,key[i],
switchChar));
i++;
}
}
else
{
newMess.push_back(mess[counter]);
i++;
}
}
cout<<"The encrypted message is:nn"<<newMess;
}     

问题出在读取char/string的方法上。

如果您更换:

do
{
mess[j]=message.get();
j++;
}
while (message.good());

这个:

do                                  //loop to input from file
{
message >> mess[j];
j++;
}
while (message.good());
message.ignore();

您的程序现在将正确读取每个字符。

但是,请注意,如果输入包含多行,那么处理字符/字符串的方式会有一个小缺陷。我不知道你想要的行为是什么,但你应该相应地改变。

我上面提到的修改后的代码将不会读取结束行n字符。

因此,您必须再次调整为可能有2D字符数组,或者字符串数组而不是字符。

我建议您使用字符串而不是char(您也可以遍历类似字符串的char数组,例如string[0]string[1]等),然后使用getline读取每一行。例如:

string mess[256];
do                                  //loop to input from file
{
getline(message,mess[j]);
j++;
}
while (message.good());

希望能有所帮助。