Thread: Help with an strange error?

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    69

    Exclamation Help with an strange error?

    Hello,

    I'm currently writing a program for my C course at University where I have to read in lines of input from stdin, replace a specified word with another word (from command line arguments), and ouput to stdout. I have that part working with the following code:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    main(int argc, char *argv[]) {
        char buf[BUFSIZ];
        char *tok;
        
        // read lines from stdin
        while ( fgets(buf, BUFSIZ, stdin) ) {
            // tokenize line using strtok
            for (tok = strtok(buf," "); tok != NULL; tok = strtok(NULL," ")) {
                if (strcmp(tok,argv[1]) == 0) printf("%s ",tok = argv[2]);
                else if (strchr(tok,'\n')) printf("%s",tok);
                else printf("%s ",tok);
            }
        }
    }
    What I also have to do, though, is if the word I'm looking for has the newline character in it, I have to output a message to stderr. What I want to do is make a new string that has the newline character concatenated onto argv[1] as follows:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    main(int argc, char *argv[]) {
        char buf[BUFSIZ];
        char *tok;
        char *error = strcat(argv[1],"\n");
        
        // read lines from stdin
        while ( fgets(buf, BUFSIZ, stdin) ) {
            // tokenize line using strtok
            for (tok = strtok(buf," "); tok != NULL; tok = strtok(NULL," ")) {
            
                if (strcmp(tok,error) == 0) {
                    fprintf(stderr, "Invalid input.\n");
                    exit(1);
                }
                
                if (strcmp(tok,argv[1]) == 0) printf("%s ",tok = argv[2]);
                else if (strchr(tok,'\n')) printf("%s",tok);
                else printf("%s ",tok);
            }
        }
    }
    After I compile and run this code, argv[1] does not get replaced by argv[2]. I think it's because when I create the error string, I change what argv[1] is. Is there something to stop this from happening? I'm thinking that there must be a way to make a copy of argv[1], but I don't know how to do this. Any help would be greatly appreciated.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>char *error = strcat(argv[1],"\n");
    This is incorrect. You shouldn't amend argv[x] memory. You'd do better with a new array:

    char error[length];
    sprintf(error, "%s\n", argv[1]);

    Just make sure that length is long enough to hold the users input but the extra character. This isn't an idiot proof way of doing it, there are better ways, but for now, try this.

    Or, how about this:
    Stick with the concept of your original program, but instead of using strcmp(), use strncmp(), checking only the first few bytes of the data from the stdin token. Something like this:
    Code:
    len = strlen(argv[1]);
    if (strncmp(tok, argv[1], len) == 0 && tok[len] == '\0')
    {
        /* Found a match */
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Plus, you should use int main( ).
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    69

    Thanks!

    Hey, thanks for the help, guys. I really appreciate it. Things are working great now!

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by XSquared
    Plus, you should use int main( ).
    And how will the application access the command line args then?
    http://faq.cprogramming.com/cgi-bin/...&id=1043284376
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM