我正在尝试编写一个从.txt文件中读取信息的程序, 删除单词/部分之间不需要的空格,并将结果保存到新的输出.txt文件。
在查看了网站上的大量问题后,我已经成功完成了大部分工作。目前我从.txt文件中读取代码并写入新文件,我还设法让它删除不需要的空格。但是现在我已经设法让这个部分运行它只会读取原始.txt文件中的一行并停在那里。它现在也正在写入输出文件的每个版本,它抓取每个空格。
这是我到目前为止编写的代码,任何有关任何部分的建议都会感激不尽,因为我还在学习。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// Declarations
string Inputfile, Outputfile;
ifstream OriginalInputFile("Sample of input file.txt");
ofstream NewOutputFile("Output File.txt");
ifstream OutputFileRead("Output File.txt");
int main()
{
if (!OriginalInputFile.is_open()) {
cout << "Input file could not be opened! Terminating!" << endl;
return 1;
}
if (OriginalInputFile.is_open())
{
NewOutputFile << " EXCEPTIONS REPORT " << endl;
NewOutputFile << " PP TO FS OO INTERFACE " << endl;
NewOutputFile << " =========================================================" << endl;
while ( getline (OriginalInputFile,Inputfile) )
while(true)
{
unsigned int pos = Inputfile.find(" ");
if(pos == string::npos)
{
break;
}
else
{
Inputfile.erase(pos, 1);
}
{
Outputfile = Inputfile;
NewOutputFile << Outputfile << endl;
OriginalInputFile.close();
}
}
}
if (!NewOutputFile.is_open()) {
cout << "Output file could not be opened! Terminating!" << endl;
return 1;
}
if (NewOutputFile.is_open())
{
while ( getline (OutputFileRead, Outputfile))
{
cout << Outputfile << endl;
}
{
NewOutputFile.close();
}
}
return 0;
}
以下是输入数据的示例:
BABROUB00008 PERSON1 MARTIN M 0610196129081978D B09 PM Brough B010 B00008 sfsf.testmailbox@mysite.com 54289
BABROUB00012 PERSON2 TIMOTHY T 1708196407091981D B08 PP Brough B306 B00012 sfsf.testmailbox@mysite.com
53899
以下是输出的一小部分示例,以显示现在发生的情况:
BABROUB00008 PERSON1 MARTIN M 0610196129081978D B09 PM Brough B010 B00008 sfsf.testmailbox@mysite.com 54289
BABROUB00008 PERSON1 MARTIN M 0610196129081978D B09 PM Brough B010 B00008 sfsf.testmailbox@mysite.com 54289
BABROUB00008 PERSON1 MARTIN M 0610196129081978D B09 PM Brough B010 B00008 sfsf.testmailbox@mysite.com 54289
BABROUB00008 PERSON1 MARTIN M 0610196129081978D B09 PM Brough B010 B00008 sfsf.testmailbox@mysite.com 54289
BABROUB00008 PERSON1 MARTIN M 0610196129081978D B09 PM Brough B010 B00008 sfsf.testmailbox@mysite.com 54289
BABROUB00008 PERSON1 MARTIN M 0610196129081978D B09 PM Brough B010 B00008 sfsf.testmailbox@mysite.com 54289
BABROUB00008 PERSON1 MARTIN M 0610196129081978D B09 PM Brough B010 B00008 sfsf.testmailbox@mysite.com 54289
BABROUB00008 PERSON1 MARTIN M 0610196129081978D B09 PM Brough B010 B00008 sfsf.testmailbox@mysite.com 54289
BABROUB00008 PERSON1 MARTIN M 0610196129081978D B09 PM Brough B010 B00008 sfsf.testmailbox@mysite.com 54289
BABROUB00008 PERSON1 MARTIN M 0610196129081978D B09 PM Brough B010 B00008 sfsf.testmailbox@mysite.com 54289
在可能的情况下,我希望将没有空格的行放在输出中,而不会看到所有工作。然后对于当前未处理的原始输入的下一行相同。并且这必须运行原始.txt文件的许多行,每次都可以更改。
答案 0 :(得分:0)
将每一行写入输出文件后,您正在调用OriginalInputFile.close()
。因此,在关闭文件之前,只会读取输入文件的第一行(此后,getline(OriginalInputFile,Inputfile)
将不会持续存在,因此顶部while
循环将在一次迭代后退出。>
这是未经测试的编辑,希望可以正常工作:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// Declarations
string Inputfile, Outputfile;
ifstream OriginalInputFile("Sample of input file.txt");
ofstream NewOutputFile("Output File.txt");
ifstream OutputFileRead("Output File.txt");
int main()
{
if (!OriginalInputFile.is_open()) {
cout << "Input file could not be opened! Terminating!" << endl;
return 1;
}
if (!NewOutputFile.is_open()) {
cout << "Output file could not be opened! Terminating!" << endl;
return 1;
}
NewOutputFile << " EXCEPTIONS REPORT " << endl;
NewOutputFile << " PP TO FS OO INTERFACE " << endl;
NewOutputFile << " =========================================================" << endl;
while ( getline (OriginalInputFile,Inputfile) )
{
for(unsigned int pos=Inputfile.find(" "); pos!=string::npos; pos=Inputfile.find(" "))
{
Inputfile.erase(pos, 1);
}
Outputfile = Inputfile;
NewOutputFile << Outputfile << endl;
}
OriginalInputFile.close();
while ( getline (OutputFileRead, Outputfile))
{
cout << Outputfile << endl;
}
NewOutputFile.close();
return 0;
}