Thread: Debug help needed

  1. #1
    Registered User
    Join Date
    Oct 2005

    Debug help needed

    I am trying to program a clone for the Unix command fgrep in C for my CSC class and I am having trouble in this part of my program. It is the part where I search for the specified word in a line (which was attained by fgets and sent to the function when called). It is simply supposed to return 1 if the word was found in the line, or 0 if it was not. I may not be going about this in the best way, but this is what I have:

    int searchLine(char *line, char query[]){
        int j;  
        int i = 0;
        int pos = 0;
        char tempWord[BUFL];
        initString(tempWord, BUFL);
        printf("\nQuerying for %s(%d) in:\n   %s\n", query, strlen(query), line);
            if(line[i] == '\n'){
            } else if(line[i] == ' '){
                printf("*** Space Found @ pos %d\n", i);
                initString(tempWord, BUFL);
                pos = 0;
                for(j = 0; j < strlen(query); j++){
                    if(tempWord[j] != query[j]){
                    if(j == strlen(query) - 1){
                        printf("*** Search Sucess");
                        return 1;
            } else {
                tempWord[pos] = line[i];
                printf("Pos: %d | String=%s\n", pos, tempWord);
            if(i > MAXLEN){
        return 0;
    There are a couple of debugging prints in it, which should not effect the actual running of the code. The function initString simply fills the entire string with NUL bytes (probably should be called clearString).

    Any help will be greatly appreciated.

    EDIT: I didn't give an example of the output of the program:

    Querying for chicken(7) in:
       alksdf;alkjsf alksdfjdk chicken sdfdsf
    Pos: 0 | String=a
    Pos: 1 | String=al
    Pos: 2 | String=alk
    Pos: 3 | String=alks
    Pos: 4 | String=alksd
    Pos: 5 | String=alksdf
    Pos: 6 | String=alksdf;
    Pos: 7 | String=alksdf;a
    Pos: 8 | String=alksdf;al
    Pos: 9 | String=alksdf;alk
    Pos: 10 | String=alksdf;alkj
    Pos: 11 | String=alksdf;alkjs
    Pos: 12 | String=alksdf;alkjsf
    *** Space Found @ pos 13
    Pos: 1 | String=
    Pos: 2 | String=
    Pos: 3 | String=
    It keeps going without anything in the tempWord variable (which is, I assume, result of initString), but I thought it would ad the next word (the chars in "line" after a space is found) to tempWord starting at "pos = 0"

    Sorry for the long post
    Last edited by Achy; 11-15-2005 at 10:42 PM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Why not just use the library function which already does this?

    int searchLine(char *line, char *query){
        return (strstr(line,query) == NULL) ? 0 : 1;
    Or is implementing this portion a large part of the entire project?

  3. #3
    Registered User
    Join Date
    Oct 2005
    At first I thought NO, but now that I think about it, that might be okay. I will ask my professor about it. Technically, finding the string within the line is not the complete purpose of the project; printing the line that has the string is. Hopefully it will be okay

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    If you can't use strstr(), you could use strncmp(), which might help. Unless you can't use strncmp(), either.

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary not built with debug info - why?
    By ulillillia in forum C Programming
    Replies: 15
    Last Post: 12-11-2008, 01:37 AM
  2. makefiles - debug & release?
    By cpjust in forum C Programming
    Replies: 6
    Last Post: 10-26-2007, 04:00 PM
  3. Debug error, help needed
    By wsy in forum C++ Programming
    Replies: 10
    Last Post: 08-11-2006, 06:19 PM
  4. Results in Debug and Release mode are different
    By jaro in forum C Programming
    Replies: 11
    Last Post: 05-27-2006, 11:08 AM
  5. Ask about Debug Assert Failed
    By ooosawaddee3 in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2002, 11:07 PM