Getline not reading from a file

I have made a program that will read the data from a file and then search for a specific ID, and then present the data for that entry.

Now this is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
ifstream DatRet("PersonData.txt");
if (DatRet.is_open())
{
	while (DatRet.good())
	{
		getline (DatRet,Full_Data);
	}
}

else 
{
	cout << "Unable to open File\nPress Enter to exit . . .";
	
	//To prevent Auto-Termination, and then end the program then and there
	cin.sync();
	cin.ignore();
	return 0;
}


The data for output is also done by this program in another section of the code.
The file does open. But on debugging I find that the string is empty (after the execution of this part of the code).

Can some one help me with why this is happening?
You need to set the delimiter for the getline function to the end of file character.

1
2
3
char endOfFile = 26;
...
getline (DatRet,Full_Data,endOfFile);


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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;


int main ()
{

	string myInput;

	cout<<"Enter your text: ";
	getline(cin, myInput);

	ofstream input("D:\help.txt", ios::out);
	input<<myInput;

	input.close();

	
	string myReader;
	ifstream output;
	output.open("D:\help.txt", ios::in);

	if(output.is_open()) {
		if(!output.eof()) {

			getline(output, myReader);
			cout<<myReader;

		}
	}

	else 
		cout<<"Can not open file!"<<endl;

	output.close();

cin.get(); cin.get();
return 0;
}
(And you have to include the file path with double backslashes)
Nope. It works fine like this in VS 2010
@Shacktar:
Thanks I rather than making a variable, simply put (char)26 as the delimiter. But what does it mean?

@Janlan:
It didn't work for me in VS C++ Express 2010. It only made a file in the specified directory after I included the double-backslashes.
C'mon I use VS Express 2010, on Windows7 and it works fine :))
But what does it mean?

Well, the default delimeter for the getline function is the newline character, meaning that it would read all the characters up to (but not including) the newline. This is what your initial call to getline did. In the loop, it would keep reading lines from the file and placing it into the string, Full_Data. Note that this did not append to Full_Data; after each iteration, Full_Data just contained the next line. In your original code, assuming it found the file, Full_Data would contain every character between the last newline and the end of file character at the end of the loop.

The end of file character (ascii 26) is placed automagically at the end of each file. Putting that as the delimeter in getline would make it read the whole file at once and place it in Full_Data, which is what you wanted.

I find it a bit odd that the above code works for Janlan, specifically because of the single backslash in the file paths. The reason this is an issue is, for instance, take the string "C:\news\tabloids\007". If you outputted it you would get the following:

C:
ews (horizontal tab) abloids

because of the special characters '\n', '\t', and '\0'. You need to escape the backslash (unless of course you want the effect as above). I guess certain compilers parse strings differently.
Last edited on
C'mon I use VS Express 2010, on Windows7 and it works fine :))


May be different settings? For me it created a file in the default folder named F:ersonData
Topic archived. No new replies allowed.