我第一次遇到这样的事情。该代码应该接收一个带有数据的 .txt 文件,执行一些计算并输出一个 .txt 文件。我对这类东西有相当多的经验,但我遇到了一些我无法解释的事情。我没有改变我的机器、系统、编译器或任何东西。我已经稍微缩短了代码以进行清理,但这是所有主要代码。
问题:由于某种原因,在我尝试了各种事情之后,程序没有输出endl
我写的东西。result << "Found_Frames" <<
我在之前、之后放置了一些测试输出,所有这些测试的行为都非常荒谬。有时会打印测试文本,但只要我endl
在测试文本行中添加一个,下次运行程序时测试文本就会消失。
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cmath>
#include <vector>
#include <string>
#include <iomanip>
#define _USE_MATH_DEFINES using namespace std;
//Declare Subprograms string findFrames(string, int);
//Main Program int main(void) {
//Parameter
List----------------------------------------------------------------
string shipSpec, Finding, Recommendation, Rectification,
infilename, header, findFramesOutput, foundFrames;
int numOfRows;
//Number of columns in input file
int numOfColumns=26;
//Number of letters to store after each finding of "fr."
int numLetters=6;
//------------------------------------------------------------------------------
//Setup input
ifstream raw;
cout << "Please enter the input file name> " << flush;
while (true)
{
getline( cin, infilename );
raw.open( infilename.c_str() );
if (raw) break;
cout << "Invalid file. Please enter a valid input file name> " << flush;
}
cout << "Number of data rows?" " ";
cin >> numOfRows;
//Setup Output
ofstream result;
result.open(string("processed_"+infilename).c_str());
if (result.fail())
{
cout << "Failed to open output file." << endl;
exit(1);
}
//Setup columnn headers
int rowCounter;
int columnCounter;
columnCounter = 0;
while (columnCounter < 2)
{
//Input header from ifsream
getline(raw, header, '\t');
//Output header to ofstream
result << header << '\t';
columnCounter+=1;
}
//go through the rest of the headers, don't do anything
while (columnCounter < numOfColumns)
{
raw>>header;
result << header << '\t';
columnCounter+=1;
}
//output column header for the list of frames
result << "Found_Frames" << endl;
//start in/outputting data. Row counter starts at 1 now
rowCounter = 1;
while (rowCounter < numOfRows)
{
//reset the column counter
columnCounter = 0;
while (columnCounter < numOfColumns)
{
//Input first two values which are the ship name and the report
//ID number.
while (columnCounter < 2)
{
string shipSpec;
//Input name
raw >> shipSpec;
//Output name
result << setw(30) << shipSpec;
columnCounter+=1;
}
//Read Findings/Rectifications/Recommendations
while (columnCounter < numOfColumns)
{
//declare local variable to store the string of info
string inString;
//Input string until \t is encountered
getline(raw, inString, '\t');
//Search and store frame numbers
findFramesOutput = findFrames(inString, numLetters);
//Append the output string to a global string variable
foundFrames.append(findFramesOutput);
//Add space to global string variable
foundFrames.append(" ");
//Output inString
result << inString << '\t';
//Reiterate to keep adding all the different frame number
//findings
columnCounter+=1;
}
//Output frame numbers (global variable)
result << foundFrames;
//Start a new line
result << endl;
rowCounter+=1;
}
}
//Close input file
raw.close();
//Close output file and return
result.close();
return(0);
}