INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

append text to a file

append text to a file

(OP)
Hi Guys

I only use C++ If i have to, and I am stuck with a problem. I am trying to append a line of text to a file. and I don't understand why the file is deleted when the second write is attempted. Meaning I first create and write a text file and then close the file.


But when I attempt to write to it again the original text file is deleted and the new text file (same name) is created with the contents of the second _write event. Below is the code it is fairly simple. By the way I also tried using the _O_APPEND mode option - the results were the same. Any ideas...??





Sleep(20);

if ((mcdb.GetDeviceCount())>0) //lets see if the system has any writable devices
{
char buffer[] = "This is a test of '_write' function";

int fh;
unsigned byteswritten;
if( (fh = _open( "Dynnonet.txt", _O_RDWR | _O_CREAT ,
_S_IREAD | _S_IWRITE )) != -1 )

{


if(( byteswritten = _write( fh, buffer, sizeof( buffer ))) == -1 )
perror( "Write failed" );
else
printf( "Wrote %u bytes to file\n", byteswritten );
Sleep(100);
_close( fh );
}




}






Sleep(5000);







Sleep(20);

if ((mcdb.GetDeviceCount())>0) //lets see if the system has any writable devices
{
char buffer[] = "This is a test part2 of '_write' function";

int fh;
unsigned byteswritten;

if( (fh = _open( "Dynnonet.txt", _O_RDWR | _O_CREAT ,
_S_IREAD | _S_IWRITE )) != -1 )

{
if(( byteswritten = _write( fh, buffer, sizeof( buffer ))) == -1 )
perror( "Write failed" );
else
printf( "Wrote %u bytes to file\n", byteswritten );
Sleep(100);
_close( fh );
}




}


RE: append text to a file

>By the way I also tried using the _O_APPEND mode option - the results were the same.
By the way it works fine with _O_APPEND flag (VC++ 2008 and 2010).

Of course, you rewrite old contents of the file because file pointer points to the origin of the file after _open w/o _O_APPEND flag. Use _seek(fh,0,SEEK_END) call if you want to append data to the existent file w/o _O_APPEND mode.

Try to avoid using of non-standard low level <io.h> stuff (_open, _write et al). It's C++ (and XXI century here;)...

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close