0

First time I've ever encountered something like this. The code is supposed to take in a .txt file with data, perform some calculations and output a .txt file. I have quite a bit of experience with this kind of stuff but I have encountered something that I cannot explain. I haven't changed my machine, system, compiler or anything. I have shortened the code a bit to clean up but this is all of the main code.

THE PROBLEM: For some reason, the program is not outputting the endl that I have written after result << "Found_Frames" << I have tried all kinds of things. I have put some test outputs before, after, and the behavior for all these tests have been quite absurd. Sometimes the test text will be printed but as soon as I add an endl to the line of the test text, the test text will disappear the next time I run the program.

#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);
}
phuclv
  • 37,963
  • 15
  • 156
  • 475
ktosayev
  • 17
  • 5
  • 1
    Did you open the stream in binary mode? – Jesse Good Jun 03 '13 at 22:32
  • 3
    "*all you need to know is that ofstream is called "result"*" - it seems you already know where the problem is or is not. Have you tried removing all the lines that you think are "irrelevant", and see if you can reproduce the problem? If you cannot, perhaps they are not so irrelevant – Andy Prowl Jun 03 '13 at 22:35
  • How did you determine that the file is empty? If on Linux, try `hexdump -C result.txt`. On any other system, look at the file size or open in a hex editor. There should be 5 or 6 bytes in the file. It seems more likely that you have the wrong newline translation. – paddy Jun 03 '13 at 22:37
  • I will get back with more info tomorrow, catching a flight right now. Thanks for the quick response guys! – ktosayev Jun 03 '13 at 22:43
  • @JesseGood - I don't think I did. – ktosayev Jun 04 '13 at 11:24
  • @AndyProwl I added in the rest of the code. I've tried commenting some things out, adding test outputs but still...all very unpredictable. – ktosayev Jun 04 '13 at 11:25
  • @paddy the file isn't empty. When you open the result file, the top row of text is up there but the second row disappears. – ktosayev Jun 04 '13 at 11:25
  • I figured it out - turns out I need to flush all my outputs with "endl" or "flush". I also tested this by noting that using "\n" removed the top row completely. Thanks for sticking with this, guys. – ktosayev Jun 04 '13 at 12:34

0 Answers0