Read file into array

Pages: 12
Hey!

I have this function that allwoes me to save the content of my array to a txt file.
this is the output.

3
Name LastName
03/19/11
1328	36.52	224.764	28.22
Name LastName
03/19/11
996	36.852	208.496	6.64
Name LastName
03/19/11
650	60.775	0	68.25

the first line is how many objects there are.
second line is userName with first and last name.
third is date.
forth line is, int, double,double, double.

Now to my problem.
I would like to write a function that reads the first line and sets my "nrOfObjecs" to 3 then read the rest of the file and send the info to my array. the numbers are separated by a /t

My problem is that I don't know how to convert the info from string to doubles and ints.
I've read about stringstream but still don't understand.

Any help is appreciated.
Last edited on
You know this:
1
2
int i;
cin>>i;


right? Well, with stringstreams it's just the same thing. Though in this case you'd probably use an ifstream anyways, no need to do it the roundabout way if stringstreams.
the first 3 lines shouldn't be a problem but the 4th is.
First of all I forgot something in the output above, this is how it should look like.
3
Name LastName
03/19/11
string 1328	36.52	224.764	28.22
Name LastName
03/19/11
string 996	36.852	208.496	6.64
Name LastName
string 03/19/11
650	60.775	0	68.25


there is a string before all the numbers and the string is followed by a tab, so the tab is a delimiter on line 4.
so if i do a "getline" ill get everything.
just have a loop that runs three times
1
2
3
4
5
6
7
8
9
10
11
12
13
ifstream fin(fileName);

fin >> numIterations;

for (int i = 0; i < numIterations; i++) {
    fin >> str1 >> str2;
    fin.ignore();
    fin >> str3; //or however you want to read in the date
    fin.ignore();
    fin >> str4 >> int1 >> dub1 >> dub2 >> dub3;
    fin.ignore();
}
//etc 

Last edited on
You can pass a delimiter character as a third optional parameter to getline.
http://cplusplus.com/reference/string/getline/

prophets solution doesn't work cause it doesn't account for the possibly infinite amount of spaces in the tab delimited string.

(infinite not literal. Of course the number is finite, but I mean the number can be arbitrarily high).
Last edited on
so you mean that the code you wrote will work even if there is a tab delimieter?
No it won't. That's why I told you to use getline for the string and cin>> for the others.
that code will work when delimited by any white space (tabs count as whitespace). hanst is right that if the strings have any white space within them, the code will break, but the basic logic is sound. you should be able to work out the kinks that are going to be unique to your input file
Last edited on
the code isn't finished but do you mean something like this? and why isn't my last getline working?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void DH::read()
{
	ifstream in("Kostdagböcker.txt");
	int nrOfDiets=0;
	int kcal=0;
	double carb=0;
	double protein=0;
	double lipid=0;
	string name=" ";
	string usrName=" ";
	string date=" ";
	in>>nrOfDiets;
	for(int i=0;i<this->nrOfDiets;i++)
	{
		getline(in,usrName);
		getline(in,date);
		getline(in,name,"\t");
	}

	
	in.close();
}
that look right to me, except your tab character needs to be wrapped in single quotes
Why do you read the date as a string?

And it's not working because it's supposed to be '\t', which is a character, not "\t", which is a zero-terminated string.
Last edited on
if I want to "grab"
1328
on line 4 what should I do?
because getline(in,kcal,'\t'); isn't working.
getline() is for (c-)strings. so after this getline(in,name,'\t'); just use the >> operator. eg: in >> kcal;
but how do i skip the \t?
getline(), the global function, is not for C-strings. It's for C++ std::strings.
Each call of getline() will get a string up to the delimiting character. So if you were on line 4 of your file, you would have the following:
1
2
3
4
5
getline(in, some_str, '\t');
// some_str now has "string"
getline(in, some_str, '\t');
// some_str now has "1328"
// ...etc. 

Assuming each of the items are separated by tabs on that line.
yeah every item on line 4 is tab-delimited but how can I convert some_str with value 1328 to an int? and then the next some_str1 with value 36.52 to double?
Why do you read the date as a string?

because the user enters the date and its not needed for any kind of calculation so i found string to be the most suiting.
try using eclipse wascana as c++ ide
youcan get it at

http://eclipselabs.org/p/wascana


thanks for the tip, but this is the last thing that I have to fix then I'm all done
but how do i skip the \t?

you don't need to. the >> operator ignores white space, tabs included

try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void DH::read()
{
	ifstream in("Kostdagböcker.txt");
	int nrOfDiets=0;
	int kcal=0;
	double carb=0;
	double protein=0;
	double lipid=0;
	string name=" ";
	string usrName=" ";
	string date=" ";
	in>>nrOfDiets;
	for(int i=0;i<this->nrOfDiets;i++)
	{
		getline(in,usrName);
		getline(in,date);
		getline(in,name,'\t');
                in >> kcal >> carb >> protien >> lipid;
                in.ignore();
	}	
	in.close();
}

Last edited on
i fixed it like this, its a bit ugly but it works.
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
void DH::read()
{

	ifstream in("Kostdagböcker.txt");
	string strKcal=" ";
	int kcal=0;
	string strCarb=" ";
	double carb=0.0;
	string strProtein=" ";
	double protein=0.0;
	string strLipid=" ";
	double lipid=0.0;
	string name;
	string usrName;
	string usrName1;
	string usrName2;
	string date;
	in>>this->nrOfDiets;

	if(in.fail()!=true)
	{
		for(int i=0;i<this->nrOfDiets && in.good();i++)
		{
			getline(in,usrName1,' ');
			getline(in,usrName2);
			usrName=usrName1+ " " + usrName2;
			getline(in,date);
			getline(in,name,'\t');
			getline(in,strKcal,'\t');
			getline(in,strCarb,'\t');
			getline(in,strProtein,'\t');
			getline(in,strLipid);
		
			
			kcal=atoi(strKcal.c_str());
			carb=atoi(strCarb.c_str());
			protein=atoi(strProtein.c_str());
			lipid=atoi(strLipid.c_str());

			this->dh[i]=new Diet(name,kcal,protein,carb,lipid,usrName,date);
			
		}
	}
	else
		cout<<"Gick inte att öppna filen!"<<endl;
	
	in.close();
}

Pages: 12