Thread: writing into a file

  1. #1
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342

    writing into a file

    I am facing a weird problem when i try to write the contents of a char array into a file.

    I first do:
    Code:
          fp=fopen(filename,"w");
          fprintf( fp1 ,"%s" , buf);
          fclose(fp);
    here, buf points to the starting location of my char array...

    the contents of this array will be re-written during the execution , and so, to transfer the new content into a file I do :

    Code:
          fp=fopen(filename,"a");
          fprintf( fp1 ,"%s",buf);
          fclose(fp);
    again , here buf points to the start ...

    the problem is : after the execution , when i open the file with "cat" , the contents of the file is displayed correctly..

    when i open it thru a text editor, I see non-ascii characters at the end of each line
    Last edited by kris.c; 06-30-2006 at 02:26 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well since your other threads involve reading from the network, my guess is your strings lack a \0 in the correct place, so you end up with random junk after each bit of valid text.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM