添加“endl”会删除所有内容

时间:2013-06-03 22:29:15

标签: c++ iostream ifstream ofstream endl

我第一次遇到过这样的事情。代码应该包含带有数据的.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);
}

0 个答案:

没有答案