Need Help Reading A Text File!

I have made a method/function to read lines from a text file and stores them in one string until end of the file. Then the function returns the size of the string. This generally works but on some text files it stops before the end of the file. Here is my code.

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
AP_Text_File::AP_Text_File(){

File_Name="text.txt";
File_Directory="text.txt";
}
int AP_Text_File::return_Number_Char(){

ofstream writefile;//--------------------------------------------used to write to files
ifstream readfile;//---------------------------------------------used to open files
string content;
	
	 string filename;
	filename=File_Name;//-----------------------------------temporary sets the file name

	 readfile.open(filename.c_str());//-------------------------------opens the name with name 

	 if(readfile.fail()){//-------------------------------------------If file does not exist.... 
	writefile.open(filename.c_str());//------------------------------The file will be created
	//cout<<"Creating "<<filename<<endl;//-----------------------------displays what is being created
	};
 
	 if(!readfile.fail()){//------------------------------------------If the file already exists then...
		 while(!readfile.eof()){//------------------------------------While the open file is not at the end of file...
			 string store;
				getline(readfile, store);//---------------------------gets that line from file and stores it in store
				content=content+store;
		 };
		 
	 };


	 writefile.close();//---------------------------------------------Closes the file (took me along time lol)
	readfile.close();//----------------------------------------------Closes the file


	return content.size();


};


Any Ideas Why?
on some text files it stops before the end of the file

what files exactly?

 
while(!readfile.eof())


did you try using:

1
2
3
string store;
while(readfile >> store);
content += sore;
The file is a text file.

I tried: while(readfile >> store); did not work.

Text file contains the following:
ÿØÿà JFIF  ` `  ÿÛ C 		

>

 $.' ",#(7),01444'9=82<.342ÿÛ C			<

<2!!22222222222222222222222222222222222222222222222222ÿÀ tÖ" ÿÄ           	
ÿÄ µ   } !1AQa"q2‘¡#B±Á&RÑð$3br‚	
[]<>
%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖ×ØÙÚáâãäåæçèéêñòóôõö÷øùúÿÄ        	
ÿÄ µ  w !1AQaq"2B‘¡±Á	#3Rð&brÑ
[$4á%ñ]<>
&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz‚ƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖ×ØÙÚâãäåæçèéêòóôõö÷øùúÿÚ   ? ðï5I0Áïž”å|ã3ۚ΢•‡sGÍP2XÇ­1¯('éTh¢Â-5ëò ù¨šâWþ,AÅEE; ¤“Ôæ’Š( ¢Š( ¢Š( ¢Š( ¢Š( ¢Š(§ø{/“ã½%óò‰¹üz_Æ)®é'ŸšÚoÑ“ükÉ</1ƒÄúkŸô˜ÇæÀW±|eéÚ$»FÒ³.zõ*¥!ô<׆é^±àhLšO“ÆF
y:’&½«áDBûÓ†êçÚ¯uK»Xã¼IY\¥— úûT)ò䐿˜™%H®–x Žþ-…žNxïYWºhk†‹OÉ”Þmû«þ÷øu昌ÐÍ=¡u<ä.ïZ†Ê9_Ìg8Î «VÛG]<¯w™˜[ò³„ÛíëZi£,åÆŠ£®8mRí­§(±†
¹nÏ¥TÌö2HSf1šìîü=¬ÌÑäžõ¨ieÓ區ªNO4ǤE¥\6~aÁúÖ'ˆ¦¤KžG>™?ã]¸;‡¨}k'Åñù:’q÷·~›Æ“î<9


and goes on....
Last edited on
UPS! :D

remove semicolon lol

1
2
3
string store;
while(readfile >> store)  //semicolon removed ^^
content += sore;
Still getting the wrong answer...
You must be reading in text mode. It's possible it's picking up an EOF signal somewhere in your binary, so you'll need to have it in binary read mode.
That's Interesting but makes sense! Thanks for the help everyone.
@Binary Thoughts
Your code have something wrong:
1. youfunction returns a number of a text. so, when the "text.text" isn't exist you should not creat new. if you want , you can do that somewhere.
2. you don't need a string to store the content. you just need to: read a line, get the size of the line, sum.
3. in you code, if "readfile.open(filename.c_str())" succeful, then "writefile.open(filename.c_str())" will not be executed. so writefile.close()...
4.when you use "getline(readfile, store);", the function drops the '\n'. you should deal with it specially.
Topic archived. No new replies allowed.