10 Replies - 524 Views - Last Post: 19 June 2011 - 07:55 AM Rate Topic: -----

#1 gsapienza  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 23
  • Joined: 17-June 11

fstream, stream extraction returning odd behavior

Posted 17 June 2011 - 07:13 PM

Ok, so I have a problem with a infile stream extraction part of my code. The first time I extract the contents of the document, all is well. However, after the if city==ARCS statement on line 34, the stream no longer works and believes that there is no more text in the file, where there in fact is. I have tested the rest of the code manually and everything else seems to work as expected.

I have also attached the file that should be read.

Any help would be greatly appreciated.

Thanks.

#include <iostream>
#include <fstream>
#include "genlib.h"
#include "simpio.h"
#include "cmpfn.h"
#include "vector.h"
#include "PQueue.h"
#include "DataStruct.h"

ifstream infile;

int main() {
    cout << "Name of File: ";
    string file= GetLine();
    infile.open(file.c_str());
    datastruct dstruct;
    
    string city= "";
    double x, y= 0;

    while (true) {
        infile >> city >> x >> y;
        dstruct.addNode(city, x, y);

        if (city=="ARCS") {
            break;
        }
    }
    
    while (!infile.fail()) {
        string from= "";
        string to= "";
        int distance= 0;
        infile >> from >> to >> distance;
        dstruct.addArc(from, to, distance);
    }
    
    dstruct.get("WashingtonDC");
return 0;
} 

Attached File(s)

  • Attached File  Small.txt (255bytes)
    Number of downloads: 40


Is This A Good Question/Topic? 0
  • +

Replies To: fstream, stream extraction returning odd behavior

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: fstream, stream extraction returning odd behavior

Posted 17 June 2011 - 07:37 PM

On line 22 you attempt to extract a string followed by two doubles, but when the line containing "ARCS" is encountered there are no doubles to extract causing infile to fail. Therefore the while loop beginning on line 30 is never entered.
Was This Post Helpful? 0
  • +
  • -

#3 gsapienza  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 23
  • Joined: 17-June 11

Re: fstream, stream extraction returning odd behavior

Posted 17 June 2011 - 07:59 PM

Ok, that makes sense but when I changed it around to the code below I still had the same problem. Seems like a simple thing going wrong.

while (true) {
        infile >> city;

        if (city=="ARCS") {
            break;
        } else {
            infile >> x >> y;
            dstruct.addNode(city, x, y);
        }
    }
    
    while (!infile.fail()) {
        string from= "";
        string to= "";
        int distance= 0;
        infile >> from >> to >> distance;
        dstruct.addArc(from, to, distance);
    }
    
    dstruct.get("WashingtonDC");
return 0;
}


Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: fstream, stream extraction returning odd behavior

Posted 17 June 2011 - 08:06 PM

Seems like that should have fixed it. Are you sure that you saved & compiled the revised code?
Was This Post Helpful? 0
  • +
  • -

#5 gsapienza  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 23
  • Joined: 17-June 11

Re: fstream, stream extraction returning odd behavior

Posted 17 June 2011 - 08:13 PM

Yeah, definitely. I can't understand why it's not working.
Was This Post Helpful? 0
  • +
  • -

#6 gsapienza  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 23
  • Joined: 17-June 11

Re: fstream, stream extraction returning odd behavior

Posted 18 June 2011 - 10:04 PM

If this helps anyone, when I debug the code using a breakpoint on line 16, the while loop executes the amount of times that it should yet does not write anything to the variables.
Was This Post Helpful? 0
  • +
  • -

#7 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: fstream, stream extraction returning odd behavior

Posted 19 June 2011 - 01:53 AM

You're checking for the wrong thing in your while condition; you ought to be checking for the status of the stream at the time you read from it, rather than afterwards when its too late. At the moment your code risks "overflowing" by repeating too many times.

    while (infile >> city >> x >> y)
    {
        dstruct.addNode(city, x, y);
    }
    
    string to, from;
    int distance;
    infile.clear();  //The previous loop set the 'bad' flag so clear it.
    while (infile >> from >> to >> distance)
    {
        dstruct.addArc(from, to, distance);
    } 
It may be worth adding some debug statements to check that you're reading everything which you expect. (i.e. just to rule out that the issue isn't with your .AddArc function)

I tested your file with the following code, and the output seemed to be OK on my system:
    while (infile >> city >> x >> y)
    {
        std::cout << "city: " << city
                  << " x: " << x
                  << " y: " << y << std::endl;
    }
    
    string to, from;
    int distance;
    infile.clear();  //The previous loop set the 'bad' flag so clear it.
    while (infile >> from >> to >> distance)
    {
        std::cout << "from: " << from
                  << " to: " << to
                  << " distance: " << distance << std::endl;
    } 


Output:
city: WashingtonDC x: 5.71 y: 2.25
city: Minneapolis x: 3.76 y: 2.93
city: SanFrancisco x: 0.278 y: 2.31
city: Dallas x: 3.31 y: 1.04
from: Minneapolis to: SanFrancisco distance: 1777
from: Minneapolis to: Dallas distance: 935
from: Minneapolis to: WashingtonDC distance: 1600
from: SanFrancisco to: WashingtonDC distance: 2200
from: Dallas to: SanFrancisco distance: 1540
from: Dallas to: WashingtonDC distance: 1319

This post has been edited by Bench: 19 June 2011 - 02:24 AM

Was This Post Helpful? 1
  • +
  • -

#8 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: fstream, stream extraction returning odd behavior

Posted 19 June 2011 - 04:42 AM

You ought to be testing the status of infile immediately after calling open to verify that it is good to begin with.

If that proves to be OK, I would suspect your addNode function, because the following code works correctly for me.

    while (true) {
        infile >> city;
        if(city == "ARCS") {
            break;
            }
        else {
            infile >> x >> y;
        }
//        dstruct.addNode(city, x, y);
        cout << city << " " << x << " " << y << endl;

        if (city=="ARCS") {
            break;
        }
    }
    

    while (!infile.fail()) {
        string from= "";
        string to= "";
        int distance= 0;
        infile >> from >> to >> distance;
//        dstruct.addArc(from, to, distance);
        cout << from << " " << to << " " << distance << endl;
    }


Was This Post Helpful? 0
  • +
  • -

#9 gsapienza  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 23
  • Joined: 17-June 11

Re: fstream, stream extraction returning odd behavior

Posted 19 June 2011 - 06:43 AM

Ok, I got something working with this code, but it does not work with the addArc function when reading from the file. However, when I assign the values in manually without using the file, addArc springs to like and does its thing.

int main() {
    cout << "Name of File: ";
    string file= GetLine();
    infile.open(file.c_str());
    datastruct dstruct;
    
    string city= "";
    double x, y= 0;

    while (infile >> city >> x >> y) {
        dstruct.addNode(city, x, y);
    }
    
    string from, to= "";
    int distance= 0;
    infile.clear();
    while (infile >> from >> to >> distance) {
        dstruct.addArc(from, to, distance);
        cout << from << endl;
        cout << to << endl;
        cout << distance << endl;
    }



and here is the seemingly simple addArc function:

void datastruct::addArc(string name, string destination, int distance) {
    Map<int>Arcs;
    place= places.get(name);
    Arcs= place->map;
    Arcs.put(destination, distance);
    place->map= Arcs;
}


Was This Post Helpful? 0
  • +
  • -

#10 gsapienza  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 23
  • Joined: 17-June 11

Re: fstream, stream extraction returning odd behavior

Posted 19 June 2011 - 07:44 AM

After some more testing I can confirm that the get function is the problem. Does anyone see any problems with this code?

void datastruct::get(string name) {
    place= places.get(name);
    cout << name << endl;
    cout << place->x << endl;
    cout << place->y << endl;

    string temp;
    Map<int>::Iterator itr = place->map.iterator();
        while (itr.hasNext()) {
            string temp= itr.next();
            cout << temp + " ";
            cout << place->map.get(temp) << endl;
        }
}


Was This Post Helpful? 0
  • +
  • -

#11 gsapienza  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 23
  • Joined: 17-June 11

Re: fstream, stream extraction returning odd behavior

Posted 19 June 2011 - 07:55 AM

I got it, looks like on line 11 of the get function I got my Java and C++ confused. Thanks for all of your help guys.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1