regarding reading and writting file

V

Vijay

Hi All,

I have written below code to read and write a file (Big file).
But every time, one extra character(may be NULL char) has been written
in output file.

Can any one please tell me why it is happing and how we can solve
this?

CString temp;
ifstream in(str_FilePath,ios::binary);
FILE *fp;

fp = fopen("C:\\pp11.txt","wb");

while (! in.eof() )
{
getline(in,str_Line);
str_Line+= "\n";
temp=str_Line.c_str();

/*Doing some operation*/

fputs( temp.GetBuffer(temp.GetLength()), fp );
temp.ReleaseBuffer();
}

Thanks in advance.
Regards,
Vijay
 
A

Alf P. Steinbach

* Vijay:
I have written below code to read and write a file (Big file).
But every time, one extra character(may be NULL char) has been written
in output file.

Can any one please tell me why it is happing and how we can solve
this?

CString temp;
ifstream in(str_FilePath,ios::binary);
FILE *fp;

fp = fopen("C:\\pp11.txt","wb");

while (! in.eof() )
{
getline(in,str_Line);
str_Line+= "\n";
temp=str_Line.c_str();

/*Doing some operation*/

fputs( temp.GetBuffer(temp.GetLength()), fp );
temp.ReleaseBuffer();
}

Post a minimal but complete /standard C++/ program that exhibits the
problem.

E.g., "CString" is not a standard C++ class.

But first, please read the FAQ item titled "How do I post a question
about code that doesn't work correctly?", currently available at e.g.
<url: http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8>.

And please follow the instructions there when reposting.

It's also a good idea to read the other FAQ items in that section,
perhaps especially 5.9 (it's unclear whether it applies or not, since
you didn't post a complete and standard C++ program).

Cheers,

- Alf
 
J

Jim Langston

Vijay said:
Hi All,

I have written below code to read and write a file (Big file).
But every time, one extra character(may be NULL char) has been written
in output file.

Can any one please tell me why it is happing and how we can solve
this?

CString temp;
ifstream in(str_FilePath,ios::binary);
FILE *fp;

fp = fopen("C:\\pp11.txt","wb");

while (! in.eof() )

eof() is not true until AFTER the end of file is hit.
{
getline(in,str_Line);

At the end of the file, getline finds the end of file, eof() is now true,
but you're not checking it. I'm not sure if in is unchanged or empty.
str_Line+= "\n";

You add a newline after the end of file has already been hit (if in is empty
when eof is hit, in will now contain "\n"
temp=str_Line.c_str();

/*Doing some operation*/

fputs( temp.GetBuffer(temp.GetLength()), fp );
temp.ReleaseBuffer();

Now you write "\n" to the output file, even though the input file has
already hit end of file.
 
V

Vijay

eof() is not true until AFTER the end of file is hit.


At the end of the file, getline finds the end of file, eof() is now true,
but you're not checking it. I'm not sure if in is unchanged or empty.


You add a newline after the end of file has already been hit (if in is empty
when eof is hit, in will now contain "\n"




Now you write "\n" to the output file, even though the input file has
already hit end of file.

Thanks for reply.

"> At the end of the file, getline finds the end of file, eof() is now
true,
but you're not checking it. I'm not sure if in is unchanged or empty."

Can you tell me how we can check eof in this code after getline?


-Vijay
 
V

Vijay

eof() is not true until AFTER the end of file is hit.


At the end of the file, getline finds the end of file, eof() is now true,
but you're not checking it. I'm not sure if in is unchanged or empty.


You add a newline after the end of file has already been hit (if in is empty
when eof is hit, in will now contain "\n"




Now you write "\n" to the output file, even though the input file has
already hit end of file.




- Show quoted text -- Hide quoted text -

- Show quoted text -

Thanks. I got solution.

Thanks a lot.
Vijay
 
J

James Kanze

I have written below code to read and write a file (Big file).
But every time, one extra character(may be NULL char) has been written
in output file.
Can any one please tell me why it is happing and how we can solve
this?

There's a lot of non-C++ in there, so it's hard to tell, but one
thing is sure:
CString temp;
ifstream in(str_FilePath,ios::binary);
FILE *fp;
fp = fopen("C:\\pp11.txt","wb");

More than one thing... What happens here if the open fails?
while (! in.eof() )

It is almost always an error to test for eof() in the condition
of a while. The results of the test have no real signification
until an input has failed, and you must check failure after
every input. The classical idiom here would be:

std::string line ;
while ( getline( in, line ) ) //...
{
getline(in,str_Line);
str_Line+= "\n";
temp=str_Line.c_str();
/*Doing some operation*/
fputs( temp.GetBuffer(temp.GetLength()), fp );
temp.ReleaseBuffer();

This is also very wierd. What's wrong with just:
fputs( line.c_str(), fp ) ;
Even better, of course, would be to use an std::eek:fstream, and
write:
dest << line << std::endl ;
(If the results turn out too slow, you might want to replace
std::endl with simply '\n'. I wouldn't do this until I had
something working, however.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top