.txt to array

Hi!

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".

Best regards!

Operator>> skips any kind of space character.
The editor used makes no difference when you read the file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main ()
{
	char *cpFileName = "crap.txt";
	string saFileText [2500];
	// Append the text "\nDONE" to the file.
	ofstream oFile (cpFileName, ios_base::app);
	oFile << "\nDONE";
	oFile.close ();
	// Get input
	ifstream iFile (cpFileName);
	for (int i = 0;; ++i)
	{
		getline (iFile, saFileText [i]);
		if (saFileText [i] == "DONE") break;
	}
	iFile.close ();
	// Print the results
	for (int i = 0; saFileText [i] != "DONE"; ++i)
	{
		cout << "Line #" << i << " : " << saFileText [i] << endl;
	}
	// Keep the program open
	cin.get ();
	return 0;
}
I already have an array that is set to 2500.

does it mean that I dont need line 8?

string saFileText [2500];
what is saFileText?
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?
Last edited on
Here's something like that with a vector:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <fstream>
#include <vector>
#include <sstream>
using namespace 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.
Last edited on
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);

I personally would use getline to get all the car definitions, and then stringstreams to extract the attributes...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Car *cars[2500];
ifstream ifs("input.txt");
int lines;
string input;
sstream ss;
getline(ifs, input);
ss<<input;
ss>>lines;
for(int i=0; i<lines && !ifs.fail(); i++)
{
getline(ifs,input);
string model;
int year;
string doors; //Why a string here? O_O
int price;
ss<<input;
ss>>model>>year>>doors>>price;
cars[i] = new Car(model,year,doors,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 
Oh, you're right. I kinda overread the delimit part.
thanks a lot guys for your help.

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.
My example doesn't work with spaces. I believe there was a stream modifier for istream to change the default separator though.
Last edited on
Topic archived. No new replies allowed.