C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
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:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #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: 8
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 Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,803 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 11 h 40 m 35 sec
Reputation Power: 437
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: 8
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 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,442 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 h 22 m 8 sec
Reputation Power: 797
>> 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 Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,803 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 11 h 40 m 35 sec
Reputation Power: 437
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: 8
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 online now
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 266 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: 3 Days 19 h 1 m 34 sec
Reputation Power: 12
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: 8
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 online now
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 266 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: 3 Days 19 h 1 m 34 sec
Reputation Power: 12
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: 8
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 online now
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 266 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: 3 Days 19 h 1 m 34 sec
Reputation Power: 12
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: 8
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 Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,803 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 11 h 40 m 35 sec
Reputation Power: 437
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: 8
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


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