Trouble replacing line of file

This is a discussion on Trouble replacing line of file within the C Programming forums, part of the General Programming Boards category; hi again, i am having some dificulties replacing a certain line of a file.My problem in specific is writing to ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    13

    Trouble replacing line of file

    hi again, i am having some dificulties replacing a certain line of a file.My problem in specific is writing to the file, a certain string, overwriting the line i want. However, my program just writes to the end of file ( as it should, according to the code ).
    I will give you the code, along with examples:
    file def:
    Code:
    im      man
    this     strinks
    the line i want to replace
    Code:
    this    stinks
    i want to replace with chars ' '


    my program, if he detects a certain string in the line, it is supposed to replace the line with chars ' ', if(name_esq == this), then replace the line that has this, like so:
    Code:
    im    man
    however. it will do:
    Code:
    im       man
    this      stinks
    this is an excert of the program:

    PHP Code:
    #define MAXLINE[1024]
    int main()
    {
    char line2[1024];
    char *name_esq="this";
    char *definitions="definitions.txt";
    FILE def
    def
    =fopen(definitions"r+");


    while(
    fgets(line2MAXLINE def))
    /* line2 - line of file def*/
    {
    printf("anything\n");        /* for debugging */
    if(strstr(line2,name_esq))    /* if name_esq is in line2*/  
    {
    printf("if works\n");/* for debugging */
    printf("%s\n"line2);    /* for debugging */
    for(k=0line2[k]!= '\n';k++)
        {
        
    line2[k]=' ';                   
        
    fprintf(def,"%s\n",line2);
        
    /*replaces line2's chars with ' ' 
        */  
        
    }
        
        }
    /* I expect to replace the line in def,
        which has the occurence of name_esq
        but instead, it writes to the next line
        as in the example.
        */
    }

    any sugestions will be gratefully accepted.

    P.S. : if you think i overexplained, please warn me and i will try to rearrange it.

    Thanks in advance

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Read lines into memory from one file, and write out all lines (with the relevant one amended). When you're done, delete the original input file, and rename the output file to the name of the first one.

    Either that, or read and hold all lines in memory, then seek back to the beginning of the file, and write all lines out.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    13
    please give me a small example of how i can read lines into memory, and then read from memory, i am not sure i understand much about memory.
    Sorry for being this noob.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    char line2[1024]; <-- Memory
    Just make an array that can hold all of the lines in the file:
    Code:
    char lines[5][1024]; /* Magic! */
    My best code is written with the delete key.

  5. #5
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159
    ok that's all i can explain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. File being filled with NULLs
    By Tigers! in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2009, 06:28 PM
  3. Trouble writing to file using fwrite()
    By yougene in forum C Programming
    Replies: 4
    Last Post: 12-30-2008, 05:13 PM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM
  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