Thread: Not re-writing to a text file.

  1. #1
    Frustrated Programmer :( phantom's Avatar
    Join Date
    Sep 2001
    Posts
    163

    Question Not re-writing to a text file.

    I have had a small program written for me that writes information to a text file and then uses it again later. I have tried to contact the original programmer but haven't been successful

    My problem is that the text file is open at the start of the program
    Code:
    fp = fopen("C:\\info.txt", "w");  //'W' should wipe any info already in file, 'A' is for append
    but the following code is used to put the information into the text file
    Code:
    fflush(fp);
    char *szData = g_Data.GetString();
    fprintf(fp, szData);
    which results on the information being appended to info.txt, not what I was after!

    My problem is - How do I delete what's in the file before I write the data to the file?
    Last edited by phantom; 06-22-2010 at 12:54 AM. Reason: Spelling error
    My site to register for all my other websites!
    'Clifton Bazaar'

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Use fseek() to move the 'cursor' for the next read/write operation.

    > fprintf(fp, szData);
    If your data contains a % character, you're sunk!
    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.

  3. #3
    Frustrated Programmer :( phantom's Avatar
    Join Date
    Sep 2001
    Posts
    163
    I'm sorry but I don't know how to program this I have tried to learn from google and the best I have got is
    char *szData = g_Data.GetString();
    fseek(szData, 0, SEEK_SET);
    fprintf(fp, szData); //Print to file
    fflush(fp);
    but this returns the error
    error C2664: 'fseek' : cannot convert parameter 1 from 'char *' to 'struct _iobuf *'
    My site to register for all my other websites!
    'Clifton Bazaar'

  4. #4
    The Autodidact Dante Wingates's Avatar
    Join Date
    Apr 2010
    Location
    Valhalla
    Posts
    56
    use rewind(FILE *file)...

    Also, "w" stands for "write", not "wipe".. You could also try using "r+", but rewind should be enough for you.

    Code:
    char *szData = g_Data.GetString();
    fseek(szData, 0, SEEK_SET);
    fprintf(fp, szData); //Print to file
    fflush(fp);
    What are you trying to do? As far as I remember(forgive me if Im wrong), the first parameter should be a FILE pointer. So fseek(FILE *, 0, SEEK_SET). Isnt szData the data to be written using fprintf?

  5. #5
    Frustrated Programmer :( phantom's Avatar
    Join Date
    Sep 2001
    Posts
    163
    Got it working now, thanks for the help
    My site to register for all my other websites!
    'Clifton Bazaar'

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM