Hi everyone quick c question. I have a file that is formatted in a special way. The first line is a person's first and last name, the second line is his phone number and the third is adress. this a peace of the file, it's much larger but it looks like this:
Mans Hansson
8510309159
Bössgränd 90, 373 02 RAMDALA
Olliver Lindberg
8602024898
Sandviken 76, 710 27 DYLTABRUK
Eskil Johnsson7901105838
Löberöd 29, 521 29 KÄTTLISTORP
And basically I want to read the first three lines once at a time. What I mean is I want to read line one and store in a variable called name, line 2 in phone_number and line 3 in adress and then repeat until end of file. this is my code:
int main(){
fstream fs;
fs.open("/Users/brah79/Downloads/skola/c /OOP/uppgift1/personInfo.txt");
string name;
string phone_number;
string adress;
while(!fs.eof()){
for(int i = 0; i < 3; i ){
getline(fs, name);
getline(fs, phone_number);
getline(fs, adress);
}
cout << "first name: " << name << endl;
cout << "person nummer: " << phone_number << endl;
cout << "adress: " << adress << endl;
}
fs.close();
return 0;
}
this is the output:
first name: Eskil Johnsson
phone number: 7901105838
adress: Löberöd 29, 521 29 KÄTTLISTORP
question 1: my code is only reading the last three lines so basically information about the last person only.
question 2: the name of the person consists of first and last name and I could not figure out a way to separate them into 2 variables.
Hope my question is clear and someone can help.
CodePudding user response:
Try using fscanf()
. It allows you to read formatted inputs from files. Use the value returned from the function to check if you are reading the name, the phone number or the address. If the input is always consistent (name followed by phone number followed by address) you need to change how you call fscanf()
.
CodePudding user response:
For a simple way to understand what's happening, lets unroll the for
loop (that means we do all the statements in the loop separately).
That will give us this code:
while(!fs.eof()){
// First iteration of the for loop
getline(fs, name);
getline(fs, phone_number);
getline(fs, adress);
// Second iteration of the for loop
getline(fs, name);
getline(fs, phone_number);
getline(fs, adress);
// Third iteration of the for loop
getline(fs, name);
getline(fs, phone_number);
getline(fs, adress);
cout << "first name: " << name << endl;
cout << "person nummer: " << phone_number << endl;
cout << "adress: " << adress << endl;
}
From this it will be easy to see that you discard the data you read in the first and second iteration of the for
loop.
It's also possible to see that you read the whole file (assuming it's only three records) in the first iteration of the while
loop.
The correct code, without the for
loop and without the bad while
loop condition, it should instead be something like this:
// Read three lines, until reaching EOF or an error
while(getline(fs, name) &&
getline(fs, phone_number) &&
getline(fs, adress))
{
// Print the three lines we just read
cout << "first name: " << name << endl;
cout << "person nummer: " << phone_number << endl;
cout << "adress: " << adress << endl;
}