Thread: Parsing text file works in windows compiler but not linux

  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,336
    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
    Chappell Hill, Texas
    Posts
    2,332
    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.
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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, 04: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, 03:17 PM
  5. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM