hi, sorry my english

im using visual studio 2005, my project is in win32 console application

im training to use files, my problem is that i cant do a ifstream get all stuff of a .txt file, it get just the first line, i can do put all in the same line, but i want let the .txt file 'well', not all confuse...

here the part of the code:

...

        char txt_records[1000];
        ofstream file_records_writin("records.txt");
	        file_records_writin<<"\t\t\t\t\tRECORDS\n"<<"name-------------------------------time\n\n"<<endl;
	file_records_writin.close();
	ifstream file_records_readin("records.txt");
	file_records_readin>>txt_records;

	cout<<txt_records;

...

all it shows is a "RECORDS" in the midle of the screen..
what i have to do to it show all stuff? ( cause it DO write every thing in the .txt file, i verified)

Recommended Answers

All 4 Replies

The >> and << file operators only read until it encounters the next non-whitespace object in the file. In your case, it is the word "RECORDS".

Your buffer is 1000 characters long, so you cannot read more than 1000 characters at a time. So, try this instead:

...

        char txt_records[1000];

        ofstream file_records_writin("records.txt");
	file_records_writin
          <<"\t\t\t\t\tRECORDS\n"
          <<"name-------------------------------time\n\n"
          <<endl;
	file_records_writin.close();

	ifstream file_records_readin("records.txt");
	file_records_readin.read( txt_records, 1000 );
	cout<<txt_records;

...

You would be better off, though, reading each record one at a time, using getline().

I hope this helps.

yeah it helped!

but now you have to explain me what just happened o.o"

from where is that .read and how it works?

tanx for the help, thats new to me, i also have heard about getline() but i dont know how use it yet

The C++ >> I/O operator corresponds to the C scanf() function. It is not really designed to input strings or binary data, even though you see it all the time.

A good reference (especially in C++) is invaluable.
I like cppreference.com, because it is simple. For more detail, check out cplusplus.com.

The .read() is a method of the istream class.

There is also a sticky thread at the top of the C++ forum for books you can get, and you can google for c++ tutorials. These will help you out a lot.

Good luck.

tanx any way

it was weird, didnt matter if i fit it or not, but when i did cout in the txt_records, after the last characteres( im sure it was the last), it was showing junk stuff, i think it was some vector instability, so i "couted" it whit a for and all get ok ! ¦)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.