C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 5th, 2003, 05:56 PM
sjbates sjbates is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2001
Location: England, UK
Posts: 41 sjbates User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 12
Split Function for C on Redhat

Hi

I am currently working on a program written in C by myself on Redhat Linux 8 where it reads in a file line by line and i need to split up information on that line.

Example Line:
ExampleName=ExampleText\n

What i've been trying to do is split the text at the equals sign (=) so i have two variables where one contains the text "ExampleName" and the other contains "ExampleText".

Plus i need to remove the text "\n".

I've already searched through Google and this forum for both but cant seem to find anything, maybe i'm looking for the wrong thing. If anyone can point me in the direction of any tutorials or information i would be very greatful.

Thanks, Steven.

Reply With Quote
  #2  
Old March 5th, 2003, 07:33 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,249 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 5 Days 17 h 27 m 51 sec
Reputation Power: 1985
As far as I know, C does not have a split function. However, you could use strtok to tokenize your string.

The first call would be something like:
strtok(input_string,"=\n");

strtok returns a pointer to the first token, minus the delimiter ('=' or '\n'). Then call strtok again with a NULL argument:
strtok(NULL,"=\n");

strtok will then return a pointer to the second token, or NULL if none was found. BTW, the delimiter string can be different in the second call, but the NULL is needed to continue working on the same input string.

Hope that helps. I know I sure got spoiled by Perl's split function.

Reply With Quote
  #3  
Old March 5th, 2003, 09:22 PM
sjbates sjbates is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2001
Location: England, UK
Posts: 41 sjbates User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 12
Hi

I think i've understood your explanation of strtok but the problem i've got now is when i compile. I believe i have my variables assigned incorrectly. I am trying to add your code to some i seen on the web about opening a file.

Code:
while(fgets(file_line, 255, temp_file)!=NULL) {
	config_name = strtok(file_line,"=\n");
	config_value = strtok(NULL,"=\n");
}


and a bit higher up i have the variable for the file content defined as (this was what the code i found was using):

Code:
char file_line[255]= {0,};


With this as i am new to C i am unsure what i could change it to to fix my problem.

When i compile with gcc i get the following error message:

Code:
test.c:38: incompatible types in assignment
test.c:39: incompatible types in assignment


I presume this is to do with file_line as line 38 and 39 are the lines which contain strtok().

Thanks for your help so far.

Reply With Quote
  #4  
Old March 6th, 2003, 12:21 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,401 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 8 h 41 m 14 sec
Reputation Power: 4080
>> I presume this is to do with file_line as line 38 and 39 are the lines which contain strtok().
Why don't you show us how you've declared config_name and config_value. Looking at the warnings, I'm guessing you've got config_name and config_value declared as char arrays.

Reply With Quote
  #5  
Old March 6th, 2003, 12:31 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,249 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 5 Days 17 h 27 m 51 sec
Reputation Power: 1985
Being a creature of habit, I tend to think in terms of how I normally use a function.

As I recall, strtok() uses the char array you pass it, substituting the delimiter with '\0', AKA NULL. Or it may copy to a static buffer first -- I forget.

Normally, I declare a char* and assign the return value of strtok to it. If not NULL, then I strcpy the string to a variable:

Code:
char *cp;
char field1[80];

if ((cp = strtok(file_line,"=\n")) != NULL)
   strcpy(field1,cp);

Reply With Quote
  #6  
Old March 6th, 2003, 09:07 AM
sjbates sjbates is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2001
Location: England, UK
Posts: 41 sjbates User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 12
Hi

I've got config_name and value like this "char config_name[255]".

I'm going to have another look now and see if i can get it working but as i said im new to all this so its taking a while but im willing to learn.

Thanks.

Reply With Quote
  #7  
Old March 6th, 2003, 12:42 PM
3dfxMM 3dfxMM is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 272 3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 6 Days 17 h 57 m 24 sec
Reputation Power: 17
config_name and config_value need to be declared as "char *".

Using your example as the contents of file_line:

ExampleName=ExampleText\n

The first call to strtok will return a pointer to the beginning of file_line and replace the '=' with a null byte.

The second call will return a pointer to file_line[12] and replace the '\n' with a null byte. Calling it in a loop will result in config_name and config_value pointing to the appropriate values from the last line placed in file_line. To get the results you want would require that you do something similar to what dwise1_aol suggested.

Reply With Quote
  #8  
Old March 6th, 2003, 03:09 PM
sjbates sjbates is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2001
Location: England, UK
Posts: 41 sjbates User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 12
Hi

I've tried both the example code given and changing to "char *config_name;" but i keep getting the following error.

Code:
warning: assignment makes pointer from integer without a cast


I'm going to try and work it out and see if i can get it going.

Thanks.

Reply With Quote
  #9  
Old March 6th, 2003, 03:17 PM
3dfxMM 3dfxMM is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 272 3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 6 Days 17 h 57 m 24 sec
Reputation Power: 17
Could you be more specific about which line of code is generating the error?

Reply With Quote
  #10  
Old March 6th, 2003, 03:27 PM
sjbates sjbates is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2001
Location: England, UK
Posts: 41 sjbates User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 12
Hi

The same error is reported for both the following lines:

Code:
config_name = strtok(file_line,"=\n");
config_value = strtok(NULL,"=\n");


and i have the following variables defined in the page

Code:
char file_line[255]= {0,};
char *config_name;
char *config_value;


Thanks.

Reply With Quote
  #11  
Old March 6th, 2003, 06:29 PM
3dfxMM 3dfxMM is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 272 3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 6 Days 17 h 57 m 24 sec
Reputation Power: 17
It apparently thinks that strtok is returning an int which makes me think that maybe you are missing the definition of strtok. Are you including string.h?

Reply With Quote
  #12  
Old March 6th, 2003, 06:52 PM
sjbates sjbates is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2001
Location: England, UK
Posts: 41 sjbates User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 12
Hi

I am not using string.h and i am new to all this so i may have got it wrong. All i want to do is find whats on both sides of the quals sign in a line taken from a file.

Thanks.

Reply With Quote
  #13  
Old March 6th, 2003, 07:15 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,249 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 5 Days 17 h 27 m 51 sec
Reputation Power: 1985
Quote:
I am not using string.h and i am new to all this so i may have got it wrong. All i want to do is find whats on both sides of the quals sign in a line taken from a file.


Bingo!

One of the "features" of C is that if a function prototype does not exist, then it will assume that function will return an int. You should have gotten a warning about it, though.

In my first C class, I made the same kind of mistake. In our first assignment, we were to pass a value to the square-root function, sqrt(), and then print out the value and its square root. I kept getting the weirdest value and just could not figure it out. Same problem: I forgot to include the math.h header, so it assumed sqrt took and returned integers.

If you include string.h, then it should work a lot better.

Reply With Quote
  #14  
Old March 6th, 2003, 07:42 PM
sjbates sjbates is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2001
Location: England, UK
Posts: 41 sjbates User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 12
Thank you so much, i added the line "#include <string.h>" and its now working great.

Everything great now and i can carry on learning C, thanks for all your help.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Split Function for C on Redhat

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap