Thread: Modify text file

  1. #1
    Registered User
    Join Date
    Jun 2012

    Modify text file

    Modify text file-sales-jpg

    #include <stdio.h>#include <stdlib.h>
    int main()
        FILE *fpstream;
        char ch[50];
        printf("Input: ");
        scanf("%[^\n]", ch);
        if ( (fpstream = fopen("sales.txt","a") ) == NULL)
            printf("Cannot open sales.txt file.");
        return 0;
    How to just modify 18292.60 to 1111?

  2. #2
    Registered User
    Join Date
    Mar 2010
    The easiest thing to do is to write the whole file out again. I.e.

    • Read a piece of data from sales.txt
    • Write it out to another file, e.g. temp.txt
    • If it needed changing, change it before writing it to the temp file
    • Close both files, replace sales.txt with temp.txt

    It is possible to modify the file in place without making a copy, by opening it for read and write and then carefully moving the position indicator.
    This would be especially painful where the length of the replacement text is not the same as the original, as in your example.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    By the way, if you are not restricted to this format for some reason, I suggest that you consider the use of SQLite instead of trying to manage your own flat file database.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    To change a field value in a text file you usually read and re-write the whole thing. If you don't want to read the entire file into memory but instead process it a line at a time, you can rename the original file (using a name given by tmpnam), write the lines to a new file with the original name, then remove the original (renamed) file. (The function names in bold are in stdio.h.)

    To actually modify the value in place without reading/writing the entire file, you can make it a binary file so that, for instance, the double 1.0 and the double 1.23456789 take up exactly the same amount of space, so you can fseek to the appropriate position and write the new value.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-25-2011, 05:54 PM
  2. modify txt file in C
    By byteM in forum C Programming
    Replies: 1
    Last Post: 12-11-2007, 01:12 PM
  3. how can i modify a text in a file
    By Marth in forum C Programming
    Replies: 6
    Last Post: 05-05-2005, 05:08 AM
  4. any way to modify static text? lol
    By jverkoey in forum Windows Programming
    Replies: 3
    Last Post: 04-27-2003, 06:34 AM
  5. Can't modify the text in
    By Magos in forum Windows Programming
    Replies: 3
    Last Post: 08-15-2002, 03:27 PM