12 Replies - 659 Views - Last Post: 01 September 2011 - 03:44 PM Rate Topic: -----

#1 skatingrocker17  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 120
  • Joined: 01-September 11

Need to read in A LOT of data from a .txt file

Posted 01 September 2011 - 06:34 AM

I'm currently writing a program that deals with booking flights. I need to read in all of the flight information from a file, the information in the file is just separated by a single space.
Here's just a few lines of the file...

Quote

AA 12 BOS SAN 299.99 200
DT 908 JFK ORD 149.99 150
AA 73 LAX SAN 39.99 30
US 401 BOS SAN 314.99 100
CO 73 ORD SFO 238.99 150
CO 782 BOS SAN 309.99 250
US 770 BOS ORD 194.99 83
DT 908 JFK MIA 99.99 80
US 780 BOS ORD 194.99 83
CO 76 ORD SFO 228.99


Here's what I have for the function so far..
int readFlights(Flight flights[], ifstream & flightFile)
{
    string      airline, orig, dest;
    int         nFlights, flightNum, seats;
    float       price;

    // TODO TODO TODO TODO
		ifstream infile;
	infile.open ("flights.txt");
        while(!infile.eof) // To get you all the lines.
        {
	        
        }
	infile.close();
}



What I have to do is set the appropriate element in the array flights to the values just read from the file. How should I go about doing this?
I've done a much more basic concept of this before with a motorcycle inventory list but it doesn't seem to really help here.
Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Need to read in A LOT of data from a .txt file

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: Need to read in A LOT of data from a .txt file

Posted 01 September 2011 - 06:45 AM

First I would suggest that you consider std::vector to replace your array so you don't have to worry about your array not being large enough to hold the information.

Second you should not use eof() to control your entry loop. The eof() function will not fail until after you read past the end of file.

Third to get the actual entries you could use the extraction operator>>.

Also you should always check that your file opened correctly.

Jim
Was This Post Helpful? 1
  • +
  • -

#3 skatingrocker17  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 120
  • Joined: 01-September 11

Re: Need to read in A LOT of data from a .txt file

Posted 01 September 2011 - 06:52 AM

Now I remember using >>, I just have to remember how to integrate it.

As for the vectors, I know about them and know somewhat how they work but I've never actually used one before so I'll probably just try to use an array.

This post has been edited by JackOfAllTrades: 01 September 2011 - 07:34 AM
Reason for edit:: Removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: Need to read in A LOT of data from a .txt file

Posted 01 September 2011 - 07:00 AM

For vectors start here and maybe this. They will make things much simpler.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 Coding in the name of  Icon User is offline

  • D.I.C Head

Reputation: 67
  • View blog
  • Posts: 232
  • Joined: 07-July 10

Re: Need to read in A LOT of data from a .txt file

Posted 01 September 2011 - 07:06 AM

First of all since you're using C++ you don't have to use arrays. I would recommend a vector. Secondly, it doesn't look like the flightFile parameter of your function is being used because you're creating a new ifstream inside the function. I would choose between passing the filename as a string (if you definitely only want to read these values from a file), or better take a reference to an istream. ifstream derives from istream so it will work for that too (see polymorphism and inheritence).

This is how I would write it:

/* Takes an istream which can be an fstream, and a vector of Flights
The stream passed as an argument is returned at the end so you can check that the operation succeeded like this:

if (ReadFlights(stream, flights))
{
    // Success
}
else
{
    // Failure
}

istream& ReadFlights(istream& stream, vector<Flight>& flights)
{
	string line;
	
        // Getline will read the next line from the stream and store it in line
	while (getline(stream, line))
	{
                // Each line represents a new Flight
		Flight newFlight;

                // A stringstream is used so that we can use formatted extraction
                // The underlying string is initialised to the same value as line
		stringstream lineStream(line);
		
                // Now we let stringstream do the work of extracting each piece of text from the line into newFlight's member variables
                // This works well here because each of the elements in the text file is separated (or delimited) by a space character, which stringstream recognises as a place to stop reading
                // Numeric values are automatically converted from their textual, lexical representations to their numeric values depending on the target type
		lineStream >> newFlight.airLine >> newFlight.flightNum
			>> newFlight.orig >> newFlight.dest >> newFlight.price
			>> newFlight.seats
		;
		
                // The flight information has been extracted from the line and stored in newFlight
                // Now a copy of that is pushed to the back of the flights vector and the original copy goes out of scope soon after
		flights.push_back(newFlight);
	}
	
	return stream;
}

Was This Post Helpful? 1
  • +
  • -

#6 skatingrocker17  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 120
  • Joined: 01-September 11

Re: Need to read in A LOT of data from a .txt file

Posted 01 September 2011 - 07:24 AM

I'm trying to integrate the vector and so far so good but I don't know if I will be able to continue the rest of the program using a vector. We're just supposed to write certain parts of the code and the rest of the code is expecting an array, not a vector.

This post has been edited by JackOfAllTrades: 01 September 2011 - 07:34 AM
Reason for edit:: Removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,659
  • Joined: 23-August 08

Re: Need to read in A LOT of data from a .txt file

Posted 01 September 2011 - 07:36 AM

Looks like you should be using an array of structs or classes to hold your data. Please tell us you're not expected to use a set of parallel arrays for each column.

EDIT: Never mind...I see there's a Flight class or struct involved.

The RIGHT way to do this would be to overload the >> operator for your class or struct, but I'm guessing that exceeds your level at this point.

EDIT 2: If you are using arrays (and not using dynamic memory allocation), the array passed in must be bounded (have a maximum number of entries). That number will need to be passed to the function, and while reading the file you need to ensure you do not overrun that boundary.

This post has been edited by JackOfAllTrades: 01 September 2011 - 07:44 AM

Was This Post Helpful? 1
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: Need to read in A LOT of data from a .txt file

Posted 01 September 2011 - 07:36 AM

There is not much difference between an array and a vector, except a vector can dynamically grow and an array is of a fixed size. You can reference a specific element of the vector exactly as you would an array using the [index].

Jim
Was This Post Helpful? 1
  • +
  • -

#9 skatingrocker17  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 120
  • Joined: 01-September 11

Re: Need to read in A LOT of data from a .txt file

Posted 01 September 2011 - 07:50 AM

View Postjimblumberg, on 01 September 2011 - 07:36 AM, said:

There is not much difference between an array and a vector, except a vector can dynamically grow and an array is of a fixed size. You can reference a specific element of the vector exactly as you would an array using the [index].

Jim

That's right, I remember learning about vectors we we've just never used them. I don't really see the point in even learning an array then because vectors are more efficient.
Anyway could something along these lines work?
int readFlights(Flight flights[], ifstream & flightFile)
{
    string      airline, orig, dest;
    int         nFlights, flightNum, seats;
    float       price;

    // TODO TODO TODO TODO
		ifstream indata;
		nFlights = 0;

	//Now we will open the file

	indata.open("flights.txt");
	if (! indata)
		cout << "Error" << endl;
		

	//We will now run a loop to get al of the data from the file and assign it to the proper array
	while (!indata.eof()&& nFlights)
	{	
		indata >> airline;
		indata >> flightNum;
		indata >> orig;
		indata >> dest;
		indata >> price;
		indata >> seats;

		indata.ignore();
  		nFlights++;
		
	}
}

Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is offline

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: Need to read in A LOT of data from a .txt file

Posted 01 September 2011 - 08:36 AM

In the following snippet:
int readFlights(Flight flights[], ifstream & flightFile)

Are you going to use an array or a vector? For a vector the code should look like:
int readFlights(std::vector<Flight> flights, ifstream & flightFile)


In the following snippet:
	indata.open("flights.txt");
	if (! indata)
		cout << "Error" << endl;


If the file didn't open why would you continue with the program. Maybe you should consider ending the program after telling the user about the problem.

In the following snippet:
	//We will now run a loop to get al of the data from the file and assign it to the proper array
	while (!indata.eof()&& nFlights)
	{	
		indata >> airline;
		indata >> flightNum;
		indata >> orig;
		indata >> dest;
		indata >> price;
		indata >> seats;

Using eof() to control a loop will cause problems. The eof() flag is not set until after the problem occurs, therefore you will have one extra (bad) entry. Also you are getting a whole bunch of entries but never filling in your vector/array with these values. You would be getting the entire line into a std::string using getline() and then parsing that string with the string stream class. However you can do the input the on a single line like:
	
while (indata >> airline >> flightNum >> orig >> dest >> price >> seats)
{


This will retrieve the information into the variables and test the stream condition in one motion.

In the following snippet:

		indata.ignore();
  		nFlights++;
		
	}

I don't see the purpose of the ignore() you should be getting all the data into the variables, using ignore will possibly cause you to loose the first character in the next line. If you are using a vector: What is the purpose of the nFlights variable? A vector the vector knows it's size, this is one of the major benefits of using the vector.

Somewhere you will need to add data to your vector or array.

Also you are passing the ifstream & flightFile into this function so you should be using this stream instead of trying to open the file again. If you try to open the same file multiple times you will probably have problems.

Jim

This post has been edited by jimblumberg: 01 September 2011 - 08:38 AM

Was This Post Helpful? 1
  • +
  • -

#11 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1431
  • View blog
  • Posts: 4,966
  • Joined: 19-February 09

Re: Need to read in A LOT of data from a .txt file

Posted 01 September 2011 - 09:06 AM

Have you been given the following function head/prototype to use? It seems strange you have this but don't use it.

int readFlights(Flight flights[], ifstream & flightFile)


This post has been edited by #define: 01 September 2011 - 09:07 AM

Was This Post Helpful? 1
  • +
  • -

#12 skatingrocker17  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 120
  • Joined: 01-September 11

Re: Need to read in A LOT of data from a .txt file

Posted 01 September 2011 - 03:07 PM

View Post#define, on 01 September 2011 - 09:06 AM, said:

Have you been given the following function head/prototype to use? It seems strange you have this but don't use it.

int readFlights(Flight flights[], ifstream & flightFile)


I am very rusty when it comes to C++. I have taken 2 previous classes in fall of 2010 and spring of 2011 but I did not program or look at code the entire summer, which was a big mistake.

This is what I was given, including the comment:
////////////////////////////////////////
// 'readFlights' reads flight info from file 'flightFile'
//     into array 'flights' of objects of class Flight
// SENTINEL in airline position terminates the info
// returns number of read flights
// exits program if 'flightFile' not opened

int readFlights(Flight flights[], ifstream & flightFile)


I plan on using an array, that is what the rest of the code is set up for. There's 9 files including the .txt file but we're only filling in the blanks for one of the .cpp files.

This post has been edited by skatingrocker17: 01 September 2011 - 03:09 PM

Was This Post Helpful? 0
  • +
  • -

#13 skatingrocker17  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 120
  • Joined: 01-September 11

Re: Need to read in A LOT of data from a .txt file

Posted 01 September 2011 - 03:44 PM

Some things are starting to come back to me. I'm thinking an array of structs would be a good way to go. I know how to make an array, and how to make a structure but how should I go about creating an array of sturcts?

I've done this before in a previous program from my last CS class but I need to find it...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1