Basic File IO with structure (small reading problem)

I have written 2 functions, 1 writes data into a normal .txt file and the other reads the .txt file and shows it.

I'm having an issue with reading a string separated by a space.

I want the code to look like this:

Name: firstName lastName
Phone: 425-123-1232
PIN: 7090

Instead, I'm getting this:
Name: firstName
Phone: lastName
Pin: 425-123-1232

My u1.name is supposed to be 1 whole string of a first name and last name separated by a space.

Any help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void writeFile()
{
	ofstream fout(FILENAME.c_str());
	fout << u1.name << endl;
	fout << u1.phoneNumber << "\n";
	fout << u1.servicePin << "\n";
	fout.close();
	
}

void readFile()
{
	int num = 0;
	ifstream fin (FILENAME.c_str());
	if (fin)
   {
		while (fin >> u1.name >> u1.phoneNumber >> u1.servicePin)
			cout << "Name: " << u1.name << endl;
		cout << "Phone: " << u1.phoneNumber << endl;
		cout << "PIN: " << u1.servicePin << endl;
			fin.close();
   }

}
The >> and << operators separate input by whitespace (spaces, tabs, newlines, etc).
You can use getline() instead.

Assuming my file f has
firstName lastName

written in it,
I can put it into one string by doing:
1
2
std::string name_line;
getline(f, name_line); // name_line will now contain "firstName lastName" 


Let me know if you need help applying that.

__________________________
Edit: Also, if you know you'll always have a first and last name in your file,
you can concatenate two strings:
1
2
3
4
string first_name;
string last_name;
f >> first_name >> last_name;
string full_name = first_name + " " + last_name;
Last edited on
Would it be something like this? My output is

Name: (blank)
Phone: lastName
PIN: 425-123-1232

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void readFile()
{
	int num = 0;
	string name_line;
	
	ifstream fin (FILENAME.c_str());
	if (fin)
   {
		while (fin >> u1.name >> u1.phoneNumber >> u1.servicePin)
	   getline(fin, name_line);
		u1.name = name_line;
		cout << "Name: " << u1.name << endl;
		cout << "Phone: " << u1.phoneNumber << endl;
		cout << "PIN: " << u1.servicePin << endl;
			fin.close();
   }

}
In your while loop, you still have it trying to do the >> operator on the u1.name property.

What does your file look like? I can show a better explanation if I know the file format.
Last edited on
My txt file looks like this, but with actual words and numbers

firstName lastName
number
PIN
Ohhh I didn't know where to put the getline. I got it to show properly now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void readFile()
{
	int num = 0;
	string name_line;
	
	ifstream fin (FILENAME.c_str());
	if (fin)
   {
		while (getline(fin, u1.name) >> u1.phoneNumber >> u1.servicePin)
		cout << "Name: " << u1.name << endl;
		cout << "Phone: " << u1.phoneNumber << endl;
		cout << "PIN: " << u1.servicePin << endl;
			fin.close();
   }

}
If that's the only thing in your file, a loop isn't really necessary.
If you want to use getline, try this:
Note that if you use getline, "firstName lastName" must be the first line of your file in this situation.

1
2
3
4
5
6
if (fin)
{
    getline(fin, u1.name);
    fin >> u1.phoneNumber >> u1.servicePin;
    //...cout'ing
}


Alternative, string concatenation:
1
2
3
4
5
6
7
8
if (fin)
{
    std::string temp_first;
    std::string temp_last;
    fin >> temp_first >> temp_last >> u1.phoneNumber >> u1.servicePin;
    u1.name = temp_first + " " + temp_last;
    //cout
}


Edit: Didn't see your post, glad you got it :)
Last edited on
Topic archived. No new replies allowed.