Thread: Does this make any sense?

  1. #1
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231

    Does this make any sense?

    hey I made a program that should find how many times a certain string appears in a .txt file does this code make sense as I said before, and if you find errors please point them out. Thanks

    Code:
    #include <stdio.h>
    #include <string.h>
    int searchnum = 0;
    char getline[1000];
    char line[1000];
    char lineh;
    int find(FILE *pfile);
    
    main()
    {
          char search[20];
          strcpy(search,"hi");
          FILE *pfile;
          
          pfile = fopen("searchfile.txt","r");
          
          printf("searchtext: %s ",search);
          
          if (pfile == NULL)
          {
                    printf("file does not exist, you fail");
                    getch();
          
          
          
          
          }
          
          else{
               
               
        while(fputc(lineh,pfile) != EOF)
        {
                   if (lineh == '\0')
                   {
                            
                            
                            if (line == search)
                            {
                                     
                                     searchnum + 1;
                                     printf("found %d\n",searchnum);
                                     
                                     
                                     
                            } 
                   
                   }
                                    
                else
                {
                    printf("%c\n",lineh);
                    strcat(lineh,line);
                    
                }  
                    
                    
                    
                
               
               
               
        }
               
               printf("%d",searchnum);
               getch();
    }
               
    }
    hope you can help

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    1) When posting code, it's much nicer to the readers if you can try and get the spacing more consistent - it makes a huge difference when reading it for the first time.

    2) You're using fputc. Don't you mean fgetc?

    3)
    Code:
    if (line == search)
    That doesn't mean what you think it means. 'line' and 'search' are actually pointers to the beginnings of the strings. Even if the contents of those arrays are identical, they're located in two separate memory locations. Therefore, the pointers will never be equal. What you should is is strcmp(). It will return 0 if the strings have identical contents.

    4) I suppose it's possible that your implementation takes care of this for you, but strcat() expects pointers to null-terminated strings, not individual characters.

    5) getch() is not a standard C function. No biggie, especially when you're just learning, but just be aware of that. It doesn't exist on all compilers.

    One tip: Although it feels slower when you're initially writing the code, you'll find that you're far more productive and secure when you do a lot more testing. For each chunk of your code, write a quick test to verify that what goes in works, and what you think comes out. You could do this to verify that each character that is read is what was next in the file. You could also print out the contents of line each time, to verify that was is concatenated on to the string, is exactly what was read, etc...

  3. #3
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231
    I followed your advice which helped a lot but I changed lineh to an int to see if it would help with the strcat, but it doesn't. It craches when it gets to the strcat. the warning is

    [Warning] passing arg 1 of `strcat' makes pointer from integer without a cast
    how do I fix this.

    If you would like to see the code again, here it is.
    (I reformatted it hope it looks nice)

    Code:
    #include <stdio.h>
    #include <string.h>
    int searchnum = 0;
    char getline[1000];
    char line[1000];
    int lineh;
    
    
    main()
    {
          char search[20];//make a char for the search parameters
          strcpy(search,"hi");//make search equal 'hi'
          FILE *pfile;//make a file pointer
          
          pfile = fopen("searchfile.txt","r");//open the file for reading
          
          printf("searchtext: %s ",search);//show the user what is being searched for
          
          if (pfile == NULL)//if the file does not exist...
          {
                    printf("file does not exist, you fail");//... tell the user he/she is a fail :D
                    getch();//get a message from the keyboard then end the program
                    return 0;//tell there were no errors 
          }
          
          else//if the file does exist...
          {
                 
               
               
               
               while((lineh=fgetc(pfile)) != EOF)//...read it one character at a time
               {
                                     
                   
                                          printf("\nlineh at top:%c",lineh);//see what the program gets here to see if it taking
                                          //the right path
                                     
                                          if (lineh == '\0')//if the character is a whitespace...
                                          {
                            
                            
                                                    if (strcmp(line,search) == 0)//...see if it equals what we are searching for
                                                    {
                                     
                                                                            searchnum + 1;//if it does equal what we want add one 
                                                                                          //to searchnum
                                                                            printf("found %d\n",searchnum);//tell the user we found
                                                                                                                               //hi once
                                                                            strcat(NULL,line);//erase everything in line
                                     
                                     
                                     
                                                     } 
                   
                                            }
                                    
                                            else//if the character isn't a whitespace add to the line
                                            {
                                                     printf("\nlineh else: %c\n",lineh);//show what lineh equals
                                                     strcat(lineh,line);//append line to accomodate lineh
                                                     printf("\nline: %s",line);//tell the user what is in line at the moment
                    
                                             }  
                    }
          printf("%d",searchnum);//after the loop tell how many 'hi's were found
          getch();//get input from the keyboard then quit
          }
               
    }
    Thanks

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Code:
     if (lineh == '\0')    //if the character is a whitespace...
    Whitespace is not represented by a x'00'.
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231
    How to represent a white space then

  6. #6
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    iswhitespace()

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    isspace() is one way.
    Mainframe assembler programmer by trade. C coder when I can.

  8. #8
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231
    so something like

    Code:
    if (isspace(lineh))
    {}
    Last edited by xniinja; 08-03-2010 at 12:40 PM.

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Yes.
    Mainframe assembler programmer by trade. C coder when I can.

  10. #10
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231
    can someone also explain the problem with


    Code:
    char line[1000];
    int lineh;
    
    
    .......
    
    
    strcat(lineh,line);


    thanks again

  11. #11
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    strcat is expecting to pointers to strings. It will append the contents of the second string to the first. So what you're doing isn't defined.

  12. #12
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231
    Ok I started messing around with stuff and it works...with 7 warnings.
    It still doesn't append the line char though. Here is the ugly freak of my programming



    Code:
    #include <stdio.h>
    #include <string.h>
    int searchnum = 0;
    char getline[1000];
    char *line[100];
    char *lineh;
    int linea[100];
    
    main()
    {
          char search[20];//make a char for the search parameters
          strcpy(search,"hi");//make search equal 'hi'
          FILE *pfile;//make a file pointer
          
          pfile = fopen("searchfile.txt","r");//open the file for reading
          
          printf("searchtext: %s ",search);//show the user what is being searched for
          
          if (pfile == NULL)//if the file does not exist...
          {
                    printf("file does not exist, you fail");//... tell the user he is a fail :D
                    getch();//get a message from the keyboard then end the program
                    return 0;//tell there were no errors 
          }
          
          else//if the file does exist...
          {
                 
               
               
               
               while((lineh=fgetc(pfile)) != EOF)//...read it one character at a time
               {
                                     
                   
                                          printf("\nlineh at top:%c",lineh);//see what the program gets here to see if it taking
                                          //the right path
                                     
                                          if (isspace(lineh))//if the character is a whitespace...
                                          {
                            
                            
                                                    if (strcmp(line,search) == 0)//...see if it equals what we are searching for
                                                    {
                                     
                                                                            searchnum + 1;//if it does equal what we want add one 
                                                                                          //to searchnum
                                                                            printf("found %d\n",searchnum);//tell the user we found
                                                                                                           //hi once
                                                                            strcpy(line,"");//erase everything in line
                                     
                                     
                                     
                                                     } 
                   
                                            
                                                     else
                                                     {
                                                         printf("space");
                                                         strcpy(line,"");
                                                     }
                                            
                                            
                                            
                                            
                                            
                                            }
                                    
                                            else//if the character isn't a whitespace add to the line
                                            {
                                                     printf("\nlineh else: %c\n",lineh);//show what lineh equals
                                                     lineh = linea;
                                                     
                                                     printf("linea:%c",linea);
                                                     strcat(lineh,line);//append line to accomodate lineh
                                                     printf("\nline: %s",line);//tell the user what is in line at the moment
                    
                                             }  
                    }
          printf("%d",searchnum);//after the loop tell how many 'hi's were found
          getch();//get input from the keyboard then quit
          }
               
    }

    Can you see what I am doing wrong, thanks.

  13. #13
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Post your warnings.
    Mainframe assembler programmer by trade. C coder when I can.

  14. #14
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231
    Oh right. yes indeed. here they are
    (I am using Dev-C++ compiler)

    C:\Dev-Cpp\search2.c In function `main':
    32 C:\Dev-Cpp\search2.c [Warning] assignment makes pointer from integer without a cast
    32 C:\Dev-Cpp\search2.c [Warning] comparison between pointer and integer
    43 C:\Dev-Cpp\search2.c [Warning] passing arg 1 of `strcmp' from incompatible pointer type
    50 C:\Dev-Cpp\search2.c [Warning] passing arg 1 of `strcpy' from incompatible pointer type
    60 C:\Dev-Cpp\search2.c [Warning] passing arg 1 of `strcpy' from incompatible pointer type
    72 C:\Dev-Cpp\search2.c [Warning] assignment from incompatible pointer type
    75 C:\Dev-Cpp\search2.c [Warning] passing arg 2 of `strcat' from incompatible pointer type
    and code so you don't have to find the lines


    Code:
    #include <stdio.h>
    #include <string.h>
    int searchnum = 0;
    char getline[1000];
    char *line[100];
    char *lineh;
    int linea[100];
    
    main()
    {
          char search[20];//make a char for the search parameters
          strcpy(search,"hi");//make search equal 'hi'
          FILE *pfile;//make a file pointer
          
          pfile = fopen("searchfile.txt","r");//open the file for reading
          
          printf("searchtext: %s ",search);//show the user what is being searched for
          
          if (pfile == NULL)//if the file does not exist...
          {
                    printf("file does not exist, you fail");//... tell the user he is a fail :D
                    getch();//get a message from the keyboard then end the program
                    return 0;//tell there were no errors 
          }
          
          else//if the file does exist...
          {
                 
               
               
               /*first 2 errors*/    
                while((lineh=fgetc(pfile)) != EOF)//...read it one character at a time
               {
                                     
                   
                                          printf("\nlineh at top:%c",lineh);//see what the program gets here to see if it taking
                                          //the right path
                                     
                                          if (isspace(lineh))//if the character is a whitespace...
                                          {
                            
                            
                                                    if (strcmp(line,search) == 0)//...see if it equals what we are searching for
                                                    {
                                     
                                                                            searchnum + 1;//if it does equal what we want add one 
                                                                                          //to searchnum
                                                                            printf("found %d\n",searchnum);//tell the user we found
                                                                                                           //hi once
                                                                            strcpy(line,"");//erase everything in line
                                     
                                     
                                     
                                                     } 
                   
                                            
                                                     else
                                                     {
                                                         printf("space");
                                                         strcpy(line,"");
                                                     }
                                            
                                            
                                            
                                            
                                            
                                            }
                                    
                                            else//if the character isn't a whitespace add to the line
                                            {
                                                     printf("\nlineh else: %c\n",lineh);//show what lineh equals
                                                     lineh = linea;
                                                     
                                                     printf("linea:%c",linea);
                                                     strcat(lineh,line);//append line to accomodate lineh
                                                     printf("\nline: %s",line);//tell the user what is in line at the moment
                    
                                             }  
                    }
          printf("%d",searchnum);//after the loop tell how many 'hi's were found
          getch();//get input from the keyboard then quit
          }
               
    }

  15. #15
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    For the first several warnings, change this:
    Code:
    char *line[100];
    char *lineh;
    to this
    Code:
    char line[100];
    char lineh;
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. make with debug options
    By Andaluz in forum Linux Programming
    Replies: 4
    Last Post: 01-19-2010, 10:24 AM
  2. Does this even remotely make sense
    By hckr83 in forum C Programming
    Replies: 6
    Last Post: 12-23-2005, 05:02 PM
  3. Question about atheists
    By gcn_zelda in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 08-11-2003, 11:50 AM
  4. 'functions' in make?
    By mart_man00 in forum C Programming
    Replies: 1
    Last Post: 06-21-2003, 02:16 PM
  5. Foreigners don't make sense
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 04-25-2002, 02:31 PM