find a string

This is a discussion on find a string within the C Programming forums, part of the General Programming Boards category; Hi!I'm new to C programming and I wanted to ask where is the error in this code: I have a ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    2

    find a string

    Hi!I'm new to C programming and I wanted to ask where is the error in this code:
    I have a file text ( frase.txt),placed in the same folder and I have to search for a string in this text and if I find that string I have to print it.


    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    
    int main(){
    const char searchingString[20];
    FILE *fillle= fopen("frase","r");
    
    char *s;
    char *token;
    printf("Introduce the string:");
    scanf("%s",searchingString);
    
    while(fgets(s,128,fillle)!=NULL){
    
    token = strtok(s ," ");
    
    if(strcmp(token,searchingString) == 0)
    printf("%s\n",searchingString);
    else printf("0\n");
    while (strtok!=NULL) {
    token= strtok(NULL," ");
    if(strcmp(token,searchingString) == 0)
    printf("%s\n",searchingString);
    else printf("0\n");
    }
    }
    }
    When I compile the file it doesn't give me any problem and when I try the program it prints on the monitor Introduce the string,but when I give it it says segmentation fault!
    What's the problem?thanks!

    Sorry for my English!

  2. #2
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Code:
    char *s;  // This pointer is uninitialized. 
    . . .
    fgets(s, 128, fillle);  // You're using an uninitialized pointer.

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Why declare searchstring as const when you don't want it to be constant!
    's' is pointer to char and you are using it without initializing.
    Change it to
    Code:
    char s[WHATEVER_SIZE_YOU_WANT];
      /*  or */
    s = malloc( WHATEVER_SIZE_YOU_WANT);      // don't forget to free when you're done!
    strictly s = malloc( sizeof(*s) * WHATEVER_SIZE_YOU_WANT);
    Last edited by Bayint Naung; 05-28-2010 at 03:47 AM.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2
    I've changed my code as you've said,but it stills give me the same problem..

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    int main(){
    char searchingString[20]; 
    FILE *fillle= fopen("frase","r");
    char s[512];
    char *token;
    printf("Introduce the string:");
    scanf("%s",searchingString);
    while(fgets(s,512,fillle)!=NULL){
    token = strtok(s ," ");
    if(strcmp(token,searchingString) == 0)
    printf("%s\n",searchingString);
    else printf("0\n");
    while (strtok!=NULL) {
    token= strtok(NULL," ");
    if(strcmp(token,searchingString) == 0)
    printf("%s\n",searchingString);
    else printf("0\n");
    }
    }
    }

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    strtok != NULL is always true. Perhaps you mean to check while token != NULL -- although, even then, you're one step too late since you use the new value of token before checking it.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Also read this - SourceForge.net: Indentation - cpwiki

    Your code is only 20 lines long (or so), and already it is very hard work to follow what is going on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Also, use int main(void) and return 0 at the end of main.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. find a string based on the location of another string
    By rlilley in forum C Programming
    Replies: 3
    Last Post: 02-19-2009, 11:29 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM

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