Thread: W+ file blues.

  1. #1
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212

    Unhappy W+ file blues.

    Code:
    FILE *superFile = fopen("test.txt","w+");
    Erases the contents of the file test.txt. Isn't this supposed to open the file, leaving it intact and still allowing you to read and write? If not, is there another way?

  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
    > leaving it intact and still allowing you to read and write?
    No, that's what "a+" does

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    FILE *superFile = fopen("test.txt","a");

    I think
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Thought a wrote to the end of the file.
    I'll try a+.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Thought a wrote to the end of the file.
    It does (initially), if you want to write elsewhere, then you'll need to fseek to it first

  6. #6
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    it's not seeking to the beginning.

    Code:
    FILE *fp = fopen("a.txt","a+");
    fseek(fp,0,SEEK_SET);
    fputc('x',fp);
    fclose(fp);
    It still adds to the end

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Odd, it works here - gcc version 3.0.3(DJGPP)

    Code:
    #include <stdio.h> 
    
    int main ( ) {
      FILE *fp = fopen( "test.txt", "a+" );
      int ch;
    
      fseek( fp, 0, SEEK_SET );
      ch = fgetc(fp);
      printf( "Ch read=%c\n", ch );
    
      fseek( fp, 0, SEEK_SET );
      fputc('@',fp);
      printf( "Wrote @\n" );
    
      fseek( fp, 0, SEEK_SET );
      ch = fgetc(fp);
      printf( "Ch read=%c\n", ch );
    
      fclose( fp );
    
      return 0;
    }
    > a.exe
    Ch read=H
    Wrote @
    Ch read=@

  8. #8
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Huh. Dev-c++ sucks .

    Maybe windows.h is ****ing it up?

  9. #9
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    r - Open text file for reading. The stream is positioned at the beginning of the file.

    r+ - Open for reading and writing. The stream is positioned at the beginning of the file.

    w - Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.

    w+ - Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file.

    a - Open for writing. The file is created if it does not exist. The stream is positioned at the end of the file.

    a+ - Open for reading and writing. The file is created if it does not exist. The stream is positioned at the end of the file.

    (From the fopen() man page)
    Jason Deckard

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >r+ - Open for reading and writing. The stream is positioned at the beginning of the file.

    This one sounds like it would do the trick.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM