replacing a line in a file

This is a discussion on replacing a line in a file within the C Programming forums, part of the General Programming Boards category; I have a file which looks like this : Code: level 1 name hans score 2 level 2 name peter ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    61

    replacing a line in a file

    I have a file which looks like this :
    Code:
    level 1
    name hans
    score 2
    level 2
    name peter
    score 4
    level 3
    name me
    score 3
    If I break the record, I want to update the score in the file. I don't have a clue how to do it though. This is what I have so far :
    Code:
    void writeHighScore(HighScore *hs){
    	FILE *f;
    	char mod[12], value[12];
    	int ival, okWrite = 0;
    	
    	f = fopen("test.dat", "r+");
    	if ( !f ) return;
    
    	while (!feof(f)) {
    		if (fscanf(f, "%s %s", mod, value) != 2) break;
    
    		if ( H_strCompare(mod, "level") ){
    			ival = atoi(value);
    			if (ival == G->field->level){
    				printf ("%s %s\n", mod, value);
    				okWrite = 1;
    			}
    		}
    		else if ( H_strCompare(mod, "name") ){
    			if (okWrite){
    				printf ("%s %s\n", mod, value);
    				//what to do here ?
    			}
    		}
    		else if ( H_strCompare(mod, "score") ){
    			if (okWrite){
    				printf ("%s %s\n", mod, value);
    				//what to do here ?
    				okWrite = 0;
    			}
    		}
    	}
    
    	fclose(f);
    }
    Is there a way to delete the contents of those lines, and replacing them by another one ?

    Thanks in advance.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1) Use binary mode and write the binary integer out. Then you can just seek to the point, and rewrite that integer / long / whatever, assuming you've opened the file correctly.
    2) Use text mode, read in the file, rewrite the whole thing with the changes you've made in memory.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

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. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

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