I have a problem, I would like to crate a function, lets call it readFromFile()
and I want it to read the data from a .txt file and put it in a array, I've done this before but never with a file that wasn't made with VS.
lets say it looks like this.
crap.txt
2450
BMW 1960 4 doors 2000
Jaguar 1986 2 doors 2500
Audi A8 2010 4 doors 20000
Mercedes 1998 4 doors 1000
Ferrari 2003 2 doors 50000
Now I would like the function to read the first line and take that number and set my nrOfCars to 2450 (because there are that many cars in that textfile)
then I would like it to put everything in a array with a capacity that has already been set to 2500.
So the first slot in the array should contain BMW with all the info.
That shouldn't be hard I just have no idea what I should write because every "word" is separated by "tab".
saFileText is a string array that stores the lines of the textfile. What I don't understand is, what's with the "DONE"? The OP never requested such a feature, shouldn't that part check for EOF? Pretty much the same with the next loop, it relies on a token string that is very likely never gonna be there- rather than that, counting the iterations of the first loop and just repeating them seems like the better approach. Aside from that, I don't think string has implemented the == and != operators. Wouldn't you have to use string::compare here?
#include <fstream>
#include <vector>
#include <sstream>
usingnamespace std;
int main() {
// open the file for input
ifstream fin( "input.txt" );
string line;
// read line count from file; assuming it's the first line
getline( fin, line );
// convert the string to an integer
stringstream ss( line );
int n;
ss >> n;
// reserve vector size to line count
vector< string > lines( n );
// read each line and put it into the vector in the reserved indicies
int i = 0;
while( i < n && getline( fin, line ) ) {
lines[i] = line;
++i;
}
// the vector lines now contains each line of the file, as a string
return 0;
}
Creating a Car class and storing that in the vector seems to be the way to go. Then parse out the fields from the string while reading it in. Do some searches, I'm sure there are examples similar to this on here.
I've never worked with vectors so I would like to stick to arrays because of that.
my default constructor creates an array with 2500 slots and as I explained above I would like to get the info and send it to the constructor Car(string model, int year, string doors, int price);
With a similar idea as above, I would use stringstreams and getline to extract the data--mainly because you said the values are tab-delimited. That leads me to beleive that spaces are part of the string, so using operator>>() doesn't sound applicable.
1 2 3 4 5 6 7 8
string line( "BMW\t1960\t4 doors\t2000" );
stringstream ss( line );
string make, year, etc, price;
getline( ss, make, '\t' ); // might want to check that it succeeded
getline( ss, year, '\t' );
getline( ss, etc, '\t' );
getline( ss, price, '\t' );
Car car( make, year, etc, price ); // you'll have to convert the strings to the appropriate types for your constructor
but lets say I use hanst99s example, will it be able to read "Audi A8 turbo" into model? so it doesnt jump over the space and think A8 is year and turbo is doors?
Now I have an array with cars and I have a function addCar() and I add more cars, now I want to save everything in my array to a textfile but I want the texfile to have the same format as the one I read the info from before so I can be able to read in the information to my array.