Not re-writing to a text file.

This is a discussion on Not re-writing to a text file. within the C++ Programming forums, part of the General Programming Boards category; I have had a small program written for me that writes information to a text file and then uses it ...

  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 01: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
    32,854
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 05: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, 07: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21