Replace word in text file

Discussion in 'C Programming' started by William Payne, Nov 5, 2003.

  1. Hello, I need to write a program that opens a text file and scans the entire
    file for a specific line and when that line is found, a particular word on
    that line is to be replaced. The new word is given as an argument to the
    program. I wrote a small test program that doesn't work because strcmp()
    fails to find a matching line. Here's the code:

    #include <stdio.h>
    #include <string.h>

    int main(int argc, char** argv)
    {
    FILE* in_file = NULL;
    FILE* out_file = NULL;
    char* temporary_file_name = NULL;
    char line[128];

    if(argc < 2)
    {
    fprintf(stderr, "You must supply a word.\n");

    return 1;
    }

    in_file = fopen("test.txt", "r");

    if(in_file == NULL)
    {
    fprintf(stderr, "Error opening test.txt\n");

    return 1;
    }

    temporary_file_name = tmpnam(NULL);

    out_file = fopen(temporary_file_name, "w");

    while(fgets(line, sizeof(line), in_file))
    {
    printf("Line read: %s\n", line);

    if(strcmp(line, "set imap_user=root") == 0)
    {
    printf("Replacing.\n");
    strcpy(line, "set imap_user=");
    strcat(line, argv[1]);
    }

    fputs(line, out_file);
    }

    fclose(in_file);
    fflush(out_file);
    fclose(out_file);

    remove("test.txt");
    rename(temporary_file_name, "test.txt");

    return 0;
    }

    And here's the original file test.txt:
    set spoolfile=imap://sysinst.ida.liu.se/INBOX
    set folder=imap://sysinst.ida.liu.se/INBOX
    set imap_user=root
    set ssl_starttls=no
    set sendmail="/usr/sbin/sendmail -oem -oi"

    Why doesn't it work?

    / William Payne
    William Payne, Nov 5, 2003
    #1
    1. Advertising

  2. William Payne wrote:

    > Hello, I need to write a program that opens a text file and scans the
    > entire file for a specific line and when that line is found, a particular
    > word on that line is to be replaced. The new word is given as an argument
    > to the program. I wrote a small test program that doesn't work because
    > strcmp() fails to find a matching line.


    That's because fgets doesn't strip the newline (assuming it finds one in the
    available space).

    > while(fgets(line, sizeof(line), in_file))
    > {
    > printf("Line read: %s\n", line);
    >
    > if(strcmp(line, "set imap_user=root") == 0)


    I suggest you replace this line with:

    if(strcmp(line, "set imap_user=root\n") == 0)

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
    Richard Heathfield, Nov 5, 2003
    #2
    1. Advertising

  3. William Payne

    Ed Morton Guest

    On 11/5/2003 2:58 PM, William Payne wrote:
    > Hello, I need to write a program that opens a text file and scans the entire
    > file for a specific line and when that line is found, a particular word on
    > that line is to be replaced. The new word is given as an argument to the
    > program. I wrote a small test program that doesn't work because strcmp()
    > fails to find a matching line. Here's the code:


    <snip>
    > while(fgets(line, sizeof(line), in_file))
    > {
    > printf("Line read: %s\n", line);
    >
    > if(strcmp(line, "set imap_user=root") == 0)


    fgets() copies the newline char to the end of the string, so "line" ends in a
    newline whereas "set imap_user=root" doesn't.

    Ed.
    Ed Morton, Nov 5, 2003
    #3
  4. William Payne

    Mike Wahler Guest

    "William Payne" <> wrote in message
    news:bobo9k$cdg$...
    > Hello, I need to write a program that opens a text file and scans the

    entire
    > file for a specific line and when that line is found, a particular word on
    > that line is to be replaced. The new word is given as an argument to the
    > program. I wrote a small test program that doesn't work because strcmp()
    > fails to find a matching line. Here's the code:
    >
    > #include <stdio.h>
    > #include <string.h>
    >
    > int main(int argc, char** argv)
    > {
    > FILE* in_file = NULL;
    > FILE* out_file = NULL;
    > char* temporary_file_name = NULL;
    > char line[128];
    >
    > if(argc < 2)
    > {
    > fprintf(stderr, "You must supply a word.\n");
    >
    > return 1;
    > }
    >
    > in_file = fopen("test.txt", "r");
    >
    > if(in_file == NULL)
    > {
    > fprintf(stderr, "Error opening test.txt\n");
    >
    > return 1;
    > }
    >
    > temporary_file_name = tmpnam(NULL);
    >
    > out_file = fopen(temporary_file_name, "w");
    >
    > while(fgets(line, sizeof(line), in_file))
    > {
    > printf("Line read: %s\n", line);
    >
    > if(strcmp(line, "set imap_user=root") == 0)


    Change the above line to:

    if(strcmp(line, "set imap_user=root\n") == 0)

    > {
    > printf("Replacing.\n");
    > strcpy(line, "set imap_user=");
    > strcat(line, argv[1]);


    strcat(line, "\n");

    /* you should also provide protection against 'strcat()'
    overflowing the array 'line' */

    > }
    >
    > fputs(line, out_file);
    > }
    >
    > fclose(in_file);
    > fflush(out_file);
    > fclose(out_file);
    >
    > remove("test.txt");
    > rename(temporary_file_name, "test.txt");
    >
    > return 0;
    > }
    >
    > And here's the original file test.txt:
    > set spoolfile=imap://sysinst.ida.liu.se/INBOX
    > set folder=imap://sysinst.ida.liu.se/INBOX
    > set imap_user=root
    > set ssl_starttls=no
    > set sendmail="/usr/sbin/sendmail -oem -oi"
    >
    > Why doesn't it work?


    'fgets()' stores the newline (if any) that it reads.

    My above changes aren't foolproof, i.e. if the line read
    has 'trailing spaces' before the newline, or if 'fgets()'
    doesn't read a newline, then it won't work.

    -Mike
    Mike Wahler, Nov 5, 2003
    #4
  5. "Richard Heathfield" <> wrote in message
    news:bobood$62v$...
    > William Payne wrote:
    >
    > > Hello, I need to write a program that opens a text file and scans the
    > > entire file for a specific line and when that line is found, a

    particular
    > > word on that line is to be replaced. The new word is given as an

    argument
    > > to the program. I wrote a small test program that doesn't work because
    > > strcmp() fails to find a matching line.

    >
    > That's because fgets doesn't strip the newline (assuming it finds one in

    the
    > available space).
    >
    > > while(fgets(line, sizeof(line), in_file))
    > > {
    > > printf("Line read: %s\n", line);
    > >
    > > if(strcmp(line, "set imap_user=root") == 0)

    >
    > I suggest you replace this line with:
    >
    > if(strcmp(line, "set imap_user=root\n") == 0)
    >
    > --
    > Richard Heathfield :
    > "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    > C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    > K&R answers, C books, etc: http://users.powernet.co.uk/eton


    Thanks for the quick reply, Mr Heathfield. It appears that you are right
    about fgets() not stripping the newline because I got an empty line between
    the printf()-statements in the while()-loop. So I wrote strcmp() as you
    suggested, but it still fails to match the string. =( Any ideas?

    / William Payne
    William Payne, Nov 5, 2003
    #5
  6. William Payne

    Ed Morton Guest

    On 11/5/2003 3:14 PM, William Payne wrote:
    > "Richard Heathfield" <> wrote in message
    > news:bobood$62v$...

    <snip>
    >>I suggest you replace this line with:
    >>
    >> if(strcmp(line, "set imap_user=root\n") == 0)

    <snip>
    > Thanks for the quick reply, Mr Heathfield. It appears that you are right
    > about fgets() not stripping the newline because I got an empty line between
    > the printf()-statements in the while()-loop. So I wrote strcmp() as you
    > suggested, but it still fails to match the string. =( Any ideas?


    Not all OSs just use "\n" as the line terminator. Try adding "\n\r" instead of
    just "\n". This approach still fails if "fgets()" doesn't read to the end of
    the line...

    Ed.

    > / William Payne
    >
    >
    Ed Morton, Nov 5, 2003
    #6
  7. "Mike Wahler" <> wrote in message
    news:SIdqb.1915$...
    >
    > "William Payne" <> wrote in message
    > news:bobo9k$cdg$...
    > > Hello, I need to write a program that opens a text file and scans the

    > entire
    > > file for a specific line and when that line is found, a particular word

    on
    > > that line is to be replaced. The new word is given as an argument to the
    > > program. I wrote a small test program that doesn't work because strcmp()
    > > fails to find a matching line. Here's the code:
    > >
    > > #include <stdio.h>
    > > #include <string.h>
    > >
    > > int main(int argc, char** argv)
    > > {
    > > FILE* in_file = NULL;
    > > FILE* out_file = NULL;
    > > char* temporary_file_name = NULL;
    > > char line[128];
    > >
    > > if(argc < 2)
    > > {
    > > fprintf(stderr, "You must supply a word.\n");
    > >
    > > return 1;
    > > }
    > >
    > > in_file = fopen("test.txt", "r");
    > >
    > > if(in_file == NULL)
    > > {
    > > fprintf(stderr, "Error opening test.txt\n");
    > >
    > > return 1;
    > > }
    > >
    > > temporary_file_name = tmpnam(NULL);
    > >
    > > out_file = fopen(temporary_file_name, "w");
    > >
    > > while(fgets(line, sizeof(line), in_file))
    > > {
    > > printf("Line read: %s\n", line);
    > >
    > > if(strcmp(line, "set imap_user=root") == 0)

    >
    > Change the above line to:
    >
    > if(strcmp(line, "set imap_user=root\n") == 0)
    >
    > > {
    > > printf("Replacing.\n");
    > > strcpy(line, "set imap_user=");
    > > strcat(line, argv[1]);

    >
    > strcat(line, "\n");
    >
    > /* you should also provide protection against 'strcat()'
    > overflowing the array 'line' */
    >
    > > }
    > >
    > > fputs(line, out_file);
    > > }
    > >
    > > fclose(in_file);
    > > fflush(out_file);
    > > fclose(out_file);
    > >
    > > remove("test.txt");
    > > rename(temporary_file_name, "test.txt");
    > >
    > > return 0;
    > > }
    > >
    > > And here's the original file test.txt:
    > > set spoolfile=imap://sysinst.ida.liu.se/INBOX
    > > set folder=imap://sysinst.ida.liu.se/INBOX
    > > set imap_user=root
    > > set ssl_starttls=no
    > > set sendmail="/usr/sbin/sendmail -oem -oi"
    > >
    > > Why doesn't it work?

    >
    > 'fgets()' stores the newline (if any) that it reads.
    >
    > My above changes aren't foolproof, i.e. if the line read
    > has 'trailing spaces' before the newline, or if 'fgets()'
    > doesn't read a newline, then it won't work.
    >
    > -Mike
    >
    >


    Thanks for your reply, Mr Wahler. I tried this loop as you and others
    suggested:

    while(fgets(line, sizeof(line), in_file))
    {
    printf("Line read: %s", line);

    if(strcmp(line, "set imap_user=root\n") == 0)
    {
    printf("Replacing.\n");
    strcpy(line, "set imap_user=");
    strcat(line, argv[1]);
    strcat(line, "\n");
    }

    fputs(line, out_file);
    }

    But strcmp() never returns 0 so no replacement is made. So then I tried
    replacing the call to strcmp() with the following:
    if(strstr(line, "set imap_user=root") != NULL)

    and now it founds a match one the correct line and correctly replaces the
    word. Very good, but it annoys me that I don't know why strcmp() doesn't
    work.

    / William Payne
    William Payne, Nov 5, 2003
    #7
  8. William Payne

    Alex Guest

    Ed Morton <> wrote:


    > On 11/5/2003 3:14 PM, William Payne wrote:
    >> "Richard Heathfield" <> wrote in message
    >> news:bobood$62v$...

    > <snip>
    >>>I suggest you replace this line with:
    >>>
    >>> if(strcmp(line, "set imap_user=root\n") == 0)

    > <snip>
    >> Thanks for the quick reply, Mr Heathfield. It appears that you are right
    >> about fgets() not stripping the newline because I got an empty line between
    >> the printf()-statements in the while()-loop. So I wrote strcmp() as you
    >> suggested, but it still fails to match the string. =( Any ideas?


    > Not all OSs just use "\n" as the line terminator. Try adding "\n\r" instead of
    > just "\n". This approach still fails if "fgets()" doesn't read to the end of
    > the line...


    ITYM "\r\n". Also, I think that you're wrong.

    IIRC, when a file is opened as a text stream, '\n' is the newline
    character, regardless of the system particulars. When dealing with
    binary streams, this does become and issue, but this is not the case
    here.

    At any rate, I think that it is far better to either truncate the
    newline sequence, or use strncmp.

    Alex
    Alex, Nov 5, 2003
    #8
  9. William Payne

    Ed Morton Guest

    On 11/5/2003 3:25 PM, William Payne wrote:
    <snip>
    > But strcmp() never returns 0 so no replacement is made. So then I tried
    > replacing the call to strcmp() with the following:
    > if(strstr(line, "set imap_user=root") != NULL)
    >
    > and now it founds a match one the correct line and correctly replaces the
    > word. Very good, but it annoys me that I don't know why strcmp() doesn't
    > work.


    See my previous response. This should work for you:

    while(fgets(line, sizeof(line), in_file))
    {
    printf("Line read: %s\n", line);

    if(strncmp(line, "set imap_user=root", sizeof "set imap_user=root" - 1 ) =
    = 0)
    {

    i.e. just compare the number of chars that you want to match, ignoring any
    new-line characters from the input file so your code isn't dependent on the
    OS/tool that produced the input file. In your final code, you'll probably want
    to define a string constant or macro for "set imap_user=root" rather than
    hard-coding it twice, but you get the idea. You should probably also add a
    "strlen" check to ensure that line isn't actually longer than you expected (e.g.
    "set imap_user=rootcrop")

    Ed.

    > / William Payne
    >
    >
    Ed Morton, Nov 5, 2003
    #9
  10. William Payne

    Ed Morton Guest

    On 11/5/2003 3:42 PM, Alex wrote:
    > Ed Morton <> wrote:

    <snip>
    >>Not all OSs just use "\n" as the line terminator. Try adding "\n\r" instead of
    >>just "\n". This approach still fails if "fgets()" doesn't read to the end of
    >>the line...

    >
    >
    > ITYM "\r\n". Also, I think that you're wrong.


    Could be, but just using "\n" doesn't work for him. I suppose there could just
    be white-space at the end of the line. Either way...

    <snip>
    > At any rate, I think that it is far better to either truncate the
    > newline sequence, or use strncmp.


    Truncating the newline sequence might be tough if it really isn't always "\n"
    and you'd also want to truncate trailing white-space. I'd go with "strncmp()"
    plus "strlen()".

    Ed.

    > Alex
    Ed Morton, Nov 5, 2003
    #10
  11. William Payne

    -berlin.de Guest

    Ed Morton <> wrote:


    > On 11/5/2003 3:25 PM, William Payne wrote:
    > <snip>
    >> But strcmp() never returns 0 so no replacement is made. So then I tried
    >> replacing the call to strcmp() with the following:
    >> if(strstr(line, "set imap_user=root") != NULL)
    >>
    >> and now it founds a match one the correct line and correctly replaces the
    >> word. Very good, but it annoys me that I don't know why strcmp() doesn't
    >> work.


    > See my previous response. This should work for you:


    > while(fgets(line, sizeof(line), in_file))
    > {
    > printf("Line read: %s\n", line);


    > if(strncmp(line, "set imap_user=root", sizeof "set imap_user=root" - 1 ) =
    > = 0)
    > {


    > i.e. just compare the number of chars that you want to match, ignoring any
    > new-line characters from the input file so your code isn't dependent on the
    > OS/tool that produced the input file. In your final code, you'll probably want
    > to define a string constant or macro for "set imap_user=root" rather than
    > hard-coding it twice, but you get the idea. You should probably also add a
    > "strlen" check to ensure that line isn't actually longer than you expected (e.g.
    > "set imap_user=rootcrop")


    > Ed.


    Another thing that could go wrong is that what looks like a space
    character is in reality a tab character, so you may have to check
    with

    if ( strncmp( line, "set imap_user=root",
    sizeof "set imap_user=root" - 1 ) == 0 ||
    strncmp( line, "set\timap_user=root",
    sizeof "set\timap_user=root" - 1 ) == 0 )

    for both cases.
    Regards, Jens
    --
    _ _____ _____
    | ||_ _||_ _| -berlin.de
    _ | | | | | |
    | |_| | | | | | http://www.physik.fu-berlin.de/~toerring
    \___/ens|_|homs|_|oerring
    -berlin.de, Nov 5, 2003
    #11
  12. Ed Morton wrote:

    >
    >
    > On 11/5/2003 3:42 PM, Alex wrote:
    >> Ed Morton <> wrote:

    > <snip>
    >>>Not all OSs just use "\n" as the line terminator. Try adding "\n\r"
    >>>instead of
    >>>just "\n". This approach still fails if "fgets()" doesn't read to the end
    >>> of the line...

    >>
    >>
    >> ITYM "\r\n". Also, I think that you're wrong.

    >
    > Could be,


    Trust me. He's right, you're wrong. Sorry, but there it is.

    Whatever the platform's idea of newline is, a text file created in that
    platform's native format, if opened in text mode in a C program, will
    appear to have '\n' newlines, no matter what magic the runtime library must
    do in order to achieve that.

    (This doesn't mean that a Windows text file, opened on Linux, will magically
    lose its '\r' characters. It won't.)

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
    Richard Heathfield, Nov 5, 2003
    #12
  13. William Payne

    Ed Morton Guest

    On 11/5/2003 4:36 PM, Richard Heathfield wrote:
    > Ed Morton wrote:
    >
    >
    >>
    >>On 11/5/2003 3:42 PM, Alex wrote:
    >>
    >>>Ed Morton <> wrote:

    >>
    >><snip>
    >>
    >>>>Not all OSs just use "\n" as the line terminator. Try adding "\n\r"
    >>>>instead of
    >>>>just "\n". This approach still fails if "fgets()" doesn't read to the end
    >>>>of the line...
    >>>
    >>>
    >>>ITYM "\r\n". Also, I think that you're wrong.

    >>
    >>Could be,

    >
    >
    > Trust me. He's right, you're wrong. Sorry, but there it is.


    What? How dare you? That's it - you're killfiled for 90 days.

    That is the appropriate response to being politely corrected these days, isn't
    it ;-).

    Thanks,

    Ed.
    Ed Morton, Nov 5, 2003
    #13
  14. William Payne

    CBFalconer Guest

    William Payne wrote:
    >
    > Hello, I need to write a program that opens a text file and scans
    > the entire file for a specific line and when that line is found,
    > a particular word on that line is to be replaced. The new word is
    > given as an argument to the program. I wrote a small test program
    > that doesn't work because strcmp() fails to find a matching line.
    > Here's the code:
    >
    > #include <stdio.h>
    > #include <string.h>
    >
    > int main(int argc, char** argv)
    > {
    > FILE* in_file = NULL;
    > FILE* out_file = NULL;
    > char* temporary_file_name = NULL;
    > char line[128];
    >
    > if(argc < 2)


    style note: if is not a function, follow it with a blank.

    > {
    > fprintf(stderr, "You must supply a word.\n");
    > return 1;


    Undefined behaviour. #include <stdlib.h> and use return
    EXIT_FAILURE.

    > }
    > in_file = fopen("test.txt", "r");
    > if(in_file == NULL)
    > {
    > fprintf(stderr, "Error opening test.txt\n");
    > return 1;


    same thing here.

    > }
    > temporary_file_name = tmpnam(NULL);
    > out_file = fopen(temporary_file_name, "w");
    >
    > while(fgets(line, sizeof(line), in_file))
    > {
    > printf("Line read: %s\n", line);
    > if(strcmp(line, "set imap_user=root") == 0)


    Can never succeed. If the input line was less than 127 chars it
    had at least a terminal '\n' in it. If longer the first 127 chars
    wouldn't match your comparee.

    > {
    > printf("Replacing.\n");
    > strcpy(line, "set imap_user=");
    > strcat(line, argv[1]);


    and even if it did compare, the result would have no final '\n'.

    > }
    >
    > fputs(line, out_file);
    > }
    > fclose(in_file);
    > fflush(out_file);
    > fclose(out_file);
    >
    > remove("test.txt");
    > rename(temporary_file_name, "test.txt");
    >
    > return 0;
    > }
    >

    .... snip ...
    >
    > Why doesn't it work?


    Because you were very very sloppy (you asked). See above. Read
    the descriptions of all the standard functions you are using. You
    get points for proper declaration of main, returning a value, and
    testing fopen results. You lose points for failure to check the
    success of fclose, fflush, remove, and rename, failure of which
    could result in permanent loss of data.

    You might be advised to use a more forgiving input function. You
    are welcome to ggets, available at:

    <http://cbfalconer.home.att.net/download/ggets.zip>

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Nov 5, 2003
    #14
  15. William Payne

    Alex Guest

    Ed Morton <> wrote:


    > On 11/5/2003 3:42 PM, Alex wrote:
    >> Ed Morton <> wrote:

    > <snip>
    >>>Not all OSs just use "\n" as the line terminator. Try adding "\n\r" instead of
    >>>just "\n". This approach still fails if "fgets()" doesn't read to the end of
    >>>the line...

    >>
    >>
    >> ITYM "\r\n". Also, I think that you're wrong.


    > Could be, but just using "\n" doesn't work for him. I suppose there could just
    > be white-space at the end of the line. Either way...


    In fact, I suggested truncating the newline precisely because
    it makes spotting other input anomalies (i.e. white space) much
    easier.

    > <snip>
    >> At any rate, I think that it is far better to either truncate the
    >> newline sequence, or use strncmp.


    > Truncating the newline sequence might be tough if it really isn't always "\n"


    ....but it is!

    > and you'd also want to truncate trailing white-space. I'd go with "strncmp()"
    > plus "strlen()".


    If you mean using the latter as an argument to the former, sure,
    but how you get the length is not germane to my point.

    Alex
    Alex, Nov 5, 2003
    #15
  16. William Payne

    Alex Guest

    Ed Morton <> wrote:


    > On 11/5/2003 3:25 PM, William Payne wrote:
    > <snip>
    >> But strcmp() never returns 0 so no replacement is made. So then I tried
    >> replacing the call to strcmp() with the following:
    >> if(strstr(line, "set imap_user=root") != NULL)
    >>
    >> and now it founds a match one the correct line and correctly replaces the
    >> word. Very good, but it annoys me that I don't know why strcmp() doesn't
    >> work.


    > See my previous response. This should work for you:


    > while(fgets(line, sizeof(line), in_file))
    > {
    > printf("Line read: %s\n", line);


    > if(strncmp(line, "set imap_user=root", sizeof "set imap_user=root" - 1 ) =
    > = 0)
    > {


    Yes, this should work. However, I would recommend that

    a) The string should be a constant defined elsewhere so that
    you do not have to adjust both strings in case you decide
    to change the format.

    b) Use strlen instead of sizeof and lose the "-1".

    Design advice to the OP:

    I am guessing that you are reading in options from a file.
    Things like that are much better served by some sort of a table
    form such as:

    struct Option
    {
    char *p_option;
    char *p_value;
    }

    Parse your input into an array or linked list of such structures
    in a normalized fashion, discarding newlines and unnecessary
    whitespace. Then you won't have to deal with input anomalies
    elsewhere and using strcmp should suffice.

    Alex
    Alex, Nov 5, 2003
    #16
  17. William Payne

    Ed Morton Guest

    Alex wrote:
    > Ed Morton <> wrote:

    <snip>
    > Yes, this should work. However, I would recommend that
    >
    > a) The string should be a constant defined elsewhere so that
    > you do not have to adjust both strings in case you decide
    > to change the format.


    That is what I suggested in the section you snipped.

    > b) Use strlen instead of sizeof and lose the "-1".


    Probably not a big deal in this code, but just for information: Isn't
    sizeof more efficient?

    Ed.
    Ed Morton, Nov 6, 2003
    #17
  18. William Payne

    Andy Guest

    On Wed, 5 Nov 2003 22:25:52 +0100, "William Payne"
    >Thanks for your reply, Mr Wahler. I tried this loop as you and others
    >suggested:
    >
    >while(fgets(line, sizeof(line), in_file))
    > {
    > printf("Line read: %s", line);
    >
    > if(strcmp(line, "set imap_user=root\n") == 0)
    > {
    > printf("Replacing.\n");
    > strcpy(line, "set imap_user=");
    > strcat(line, argv[1]);
    > strcat(line, "\n");
    > }
    >
    > fputs(line, out_file);
    > }
    >
    >But strcmp() never returns 0 so no replacement is made.


    You don't mention what compiler you're using, but the code works fine
    when compiled with Turbo C 2.01.

    > So then I tried
    >replacing the call to strcmp() with the following:
    >if(strstr(line, "set imap_user=root") != NULL)
    >
    >and now it founds a match one the correct line and correctly replaces the
    >word. Very good, but it annoys me that I don't know why strcmp() doesn't
    >work.
    >
    >/ William Payne
    >
    Andy, Nov 6, 2003
    #18
  19. William Payne

    Alex Guest

    Ed Morton <> wrote:


    > Alex wrote:
    >> Ed Morton <> wrote:

    > <snip>
    >> Yes, this should work. However, I would recommend that
    >>
    >> a) The string should be a constant defined elsewhere so that
    >> you do not have to adjust both strings in case you decide
    >> to change the format.


    > That is what I suggested in the section you snipped.


    My apologies, I missed it.

    >> b) Use strlen instead of sizeof and lose the "-1".


    > Probably not a big deal in this code, but just for information: Isn't
    > sizeof more efficient?


    Might be. However, as well all know, premature optimization is
    the root of all evil. :) With that in mind, 'strlen' is more
    idiomatic for this case.

    Alex
    Alex, Nov 7, 2003
    #19
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rynox
    Replies:
    4
    Views:
    29,067
    Malte
    Nov 18, 2005
  2. Guest
    Replies:
    4
    Views:
    292
    Guest
    May 12, 2006
  3. mscir
    Replies:
    0
    Views:
    312
    mscir
    Oct 12, 2005
  4. Chris Angelico
    Replies:
    9
    Views:
    232
    Andrew Cooper
    Jul 29, 2012
  5. Tim Chase
    Replies:
    10
    Views:
    374
    Robert Miles
    Aug 31, 2012
Loading...

Share This Page