Reading Datas From A Txt File

I'm trying to read the txt file into my main program but I'm stuck trying to get it to read. So far I've already got the address but I don't know how to get the age. Line 5 is the age. Just in case you're wondering how I got the address, I used fscanf to get the address and put it into an array then I just glued the array together to form the address.

1
2
3
4
5
V's Place
123 Fake Street
Fake City, Fake State 12345

21 
And what prevents you from using it to get the age?
1
2
3
4
5
 for (i=0;i<11;i++)     
 {    
      fscanf(addressfile,"%s",addressData[i]);
 } 
 fscanf(addressfile,"%i",age);


the first one scan the address but the second won't scan the age
Last edited on
Probably you have already read age into array: you will read 11 elements and there is 10 in address part, so 11th time you will read age into addressdata[10]
I've just adjusted it to addressData[10] and it cuts off the zip code.
1
2
3
4
5
V's Place
123 Fake Street
Fake City, Fake State 12345

Age:21  

fscanf(addressfile,"Age:%i",age);
Do you think it's possible to do this and get the age? I've already tried it but I didn't get the age. I'm not if that's legal.
Why do you use fscanf instead of C++ streams?
1
2
3
4
5
6
7
8
9
10
std::string address;
std::getline(std::cin, address);
address += " ";
std::string temp;
std::getline(std::cin, temp);
address += temp + " ";
std::getline(std::cin, temp);
address += temp;
int age
std::cin >> age;
I know that c++ streams would make it a lot easier but my professor wants us to use fscanf. Thanks for your help MiiNiPaa. It's greatly appreciated.
Topic archived. No new replies allowed.