Parsing text file works in windows compiler but not linux

This is a discussion on Parsing text file works in windows compiler but not linux within the C++ Programming forums, part of the General Programming Boards category; Hey, I have to read numbers from a text file. Heres an example input: 6 8 1 2 10 1 ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    6

    Parsing text file works in windows compiler but not linux

    Hey, I have to read numbers from a text file. Heres an example input:

    6 8
    1 2 10
    1 3 9
    2 3 7
    2 4 2
    3 5 5
    4 5 3
    4 6 8
    5 6 4

    The parser I coded works fine in NetBeans and Dev c++, however we are required to run it on a linux terminal also.
    Heres how the last column looks after I read it in from the text file in linux:

    10
    7
    2
    5
    3
    8
    4

    it seems to skip the 9. Heres my code for reading the info from the text file "input.txt"

    Code:
    //first I get all the lines and add them to a string vector
    ifstream myFile("input.txt");
        if (myFile.is_open())
        {
            while (!myFile.eof())
            {
                getline(myFile,line);
                inputLines.push_back(line);
            }
    
            myFile.close();
        }
     
        //this while loop will parse all the info from the text file
        while (lineNumber < inputLines.size())
        {
            //converts the read in line to a c sting
            cstrInput = new char[line.size()+1];
            strcpy(cstrInput,inputLines[lineNumber].c_str());
            
            //parse the line, looking for a space or " "
            parser = strtok(cstrInput," ");
            
            while (parser != NULL)
            {
                //all input is integers, so convert the num to an integer
                num = atoi(parser);
                
                
                if (infoType == 1)
                {
                    //infoType is one, and line number is 0, so I'm parsing nV
                    if (lineNumber == 0)
                    {
                        //create a new set with nV amount
                        theSet = new dis_set(num);
                    } 
                    
                    //u is the first column
                   //not the first line, so info is u values
                    else
                    {
                        theEdge.u = num;
                        
                    }
                } 
                
                else if (infoType == 2)
                {
                    //infoType is two, and line number is 0, so I'm parsing nE
                    if (lineNumber == 0)
                    {
                        //create a new heap with nE amount
                        theHeap = new min_heap(num);
                      
                    } 
                    else
                    {
                       //v is the second coloumn 
                       //not the first line, so info is v value
                        theEdge.v = num;
                    }
                } 
    
                //if infoType is 3, than I'm reading a cost value.
                else if (infoType == 3)
                {
                    if (lineNumber == 0)
                        ;
                    else
                    
                   //cost is the third coloumn
                   theEdge.cost = num;
                }
    
                parser = strtok(NULL," ");
                infoType++;
            }
    
            //insert the newly created edge into the heap
            theHeap->insert(theEdge);
            lineNumber++;
            infoType = 1; // set infoType to 1
        }
    The first line of the text file just tells me how many vertices (nV) and edges (nE) I have.

    I create a loop that goes through the string vector. Each loop it creates an Edge (an edge is a struct made up of 3 ints which are two vertices and a cost). I set the first value to Edge.u, I set the second value to Edge.v, and i set the third Edge.cost.

    I then insert the edge into a min-heap. But, like I stated above the parse code works in NetBeans and Dev c++, but it doesnt work properly in a linux terminal.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Is the line not read? Does the edge get lost during processing? With debugger or cout statements, see if you can see where the data disappears.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    6
    Ok, after I read in every line, I print the vector I get this:
    *** 6 8
    *** 1 2 10
    *** 1 3 9
    *** 2 3 7
    *** 2 4 2
    *** 3 5 5
    *** 4 5 3
    *** 4 6 8
    *** 5 6 4
    ***

    which is the original text file. (I added the "***" in the cout call).
    Then I print the out the num every time I add it to the edge. And I get this:

    edge u= 1, edge v= 2, edge cost= 10

    edge u= 2, edge v= 3, edge cost= 7

    edge u= 2, edge v= 4, edge cost= 2

    edge u= 3, edge v= 5, edge cost= 5

    edge u= 4 ,edge v= 5, edge cost= 3

    edge u= 4, edge v= 6, edge cost= 8

    edge u= 5, edge v= 6, edge cost= 4

    So it seems to just skip the line "1 3 9" completely, and I'm not sure why its doing that because it works fine in NetBeans, but then it skips that line in the linux terminal.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You don't show how, or if, you init lineNumber and infoType.

    Could you be incrementing lineNumber somewhere else, like min_heap?

    Could the row with 10 at the end not have a blank following it, whereas all the other rows do?

    Put a final "else" after your last "else if (infoType==3)" to see if you are not keeping your state properly.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    An issue, but probably not the issue: you appear to be adding an edge even for the line that isn't an edge -- if there's no auto resizing of your min_heap then you're probably going to lose an edge, although why you would lose that particular one I don't see.

    I don't see how you can get out of that loop without calling insert, though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 03:25 AM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 02:17 PM
  5. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21