String Search with command line arguments

This is a discussion on String Search with command line arguments within the C Programming forums, part of the General Programming Boards category; I'm having trouble with a program that takes two command line arguments. The first argument is a string and the ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    73

    String Search with command line arguments

    I'm having trouble with a program that takes two command line arguments. The first argument is a string and the second argument is the name of a file. The program should then search the file (the file entered as the second argument) and print all the lines that contain the string entered in the first argument of the command line.

    The file must be read line by line so I have to use fgets() plus I need the use of strstr() to find the string in the line of the text file. But I am not getting any results in my program. Here is what my code looks like right now:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(int argc, char *argv[]) {
    
        FILE *fp;
        char buf[256];
    
        if (argc != 2) {
           printf("Supply two arguments: first a string, second a file name\n\n");
           system("PAUSE");
           exit(0);
        }
    
        else {
    
             if ((fp = fopen(argv[1], "r")) == NULL) {
                printf("Unable to open file %s\n\n", argv[1]);
                system("PAUSE");
                exit(0);
             }
    
             while (fgets(buf, 256, fp) != NULL && buf[0] != '\n') {
                   if (strstr(buf, argv[0])) {
                      fputs(buf, stdout);
                   }
             }
        }
        system("PAUSE");
        return 0;
    }
    The segment of the above code that is giving me problems is the
    entire while loop (highlighted in BOLD) with the fgets() statement. Everything above that works OK. The problem is that I am not getting any results appearing on the screen from the search.

    Any help, advice or suggestions would be greatly appreciated. I have included a sample "data.txt" file for reference.

    Thanks.
    Attached Files Attached Files

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    argv[0] is the program name
    argv[1] is the first argument

    You need to be strstr'ing on argv[2]

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    73

    Re:

    so if I wanted to run this program from the command line, would the sequence of statements be

    C:\> program name (C source file), string to search, data file name?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    It's just a matter of making what you type on the command line match up with your use of argv[] in your program

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    73

    Re:

    Yes it is working well now but I have one more question: what would be a good size to set the buffer as so that the program displays the entire line for a line where the string is found?

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>what would be a good size
    Maximum line length (incl \n), plus 1 for the \0.

    But then I guess you're going to say you don't know the line length. In that case, choose a number and be done. BUFSIZ is always a good one to use (check stdio.h for its actual value).

    Alternatively, find "smarter" ways of reading a line, building a buffer dynamically with the required dimensions.
    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. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM

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