LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-18-2005, 06:26 PM   #1
Anubhab123
LQ Newbie
 
Registered: Dec 2005
Posts: 1

Rep: Reputation: 0
Using Strings in C strtok and strcmp errors


Hello all i'm having a problem with the function strtok().i'm trying to compare the tokenized string with another string however when i use the function strcmp() it generates a segmentation fault
i wrote this

void check(FILE *file){
char buf[100]
char store[100][100];
int n=0;
char *token;
char *name="DOG";

while(fgets(buf,sizeof(buf),file)!=NULL){
strcpy(store[n],buf);
n++;
}

while(n>=0){
token=strtok(store[n]," ");
if(strcmp(token,name)==0){
printf("BINGO");
}
n--;
}

}


A file is properly passed in and it looks like

THE DOG IS BIG!



my big problem is that it keeps saying segmentation fault whenever i try to call strcmp with token.I tried making another string that isnt tokenized or anything and tried the method with that and it worked but for some reason if its a tokenized string it gives the segfault error.Any help would be appreciated!
 
Old 12-18-2005, 07:03 PM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
I'm not sure exactly what you are looking to do here, but if your trying to find DOG in the each line check out then you probably want something that looks like this:

Code:
void check(FILE *file)
{
        char buf[100];
        char store[100][100];
        int n=0;
        char *token;
        char *name="DOG";

        while(fgets(buf,sizeof(buf),file)!=NULL){
                strcpy(store[n],buf);
                n++;
        }

        while(n>=0){
                token=strtok(store[n]," ");
                do {
                        printf ("Token = %s\n",token);
                        if(strncmp(token,name,strlen(name))==0){
                                printf("BINGO");
                        }
                } while ((token = strtok(NULL," ")) != NULL);
                n--;
        }
}
I attempted to use the function you provided and while it didn't compile (missing a semi-colon) it also didn't seg fault when I fixed it. I also switched the strcmp to a strncmp as it is always better to use the strncmp (much safer).

PS. Please use code blocks when you post code, it makes it easier for us to see!

Last edited by jtshaw; 12-18-2005 at 07:05 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[C]How to compare two strings w/o using strcmp() kponenation Programming 22 11-23-2005 08:29 AM
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM
strtok pantera Programming 2 12-19-2004 01:04 PM
c++ strings-strcmp function...... sachitha Programming 4 09-12-2004 07:28 AM
strsep vs. strtok irfanhab Programming 1 05-01-2004 01:08 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration