Thread: Trouble replacing line of file

  1. #1
    Registered User
    Join Date
    Apr 2004

    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:
    im      man
    this     strinks
    the line i want to replace
    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:
    im    man
    however. it will do:
    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

    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]=' ';                   
    /*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
    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
    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
    char line2[1024]; <-- Memory
    Just make an array that can hold all of the lines in the file:
    char lines[5][1024]; /* Magic! */
    My best code is written with the delete key.

  5. #5
    Join Date
    Apr 2004
    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, 10:39 PM
  2. File being filled with NULLs
    By Tigers! in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2009, 05: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, 01:19 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM