473,385 Members | 1,602 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

strtok challenge

Hi Everyone,

I have a file with a set of strings per line and each line ends with a
'\n' and each string separated by a whitespace.
After reading each line and using strtok() to split the string into 3
strings, the last string is saved with the '\n' character still
attached. I would like to know if strtok can be used to fix this
problem or do I have to use strlen and and if statement to solve this?

Any help is appreciated!

/S

Nov 6 '07 #1
6 4363
In <11**********************@22g2000hsm.googlegroups. comSheldon <sh******@gmail.comwrites:
I have a file with a set of strings per line and each line ends with a
'\n' and each string separated by a whitespace.
After reading each line and using strtok() to split the string into 3
strings, the last string is saved with the '\n' character still
attached. I would like to know if strtok can be used to fix this
problem or do I have to use strlen and and if statement to solve this?
You could include '\n' in strtok's separator string and call strtok an
additional time.

Or you could remove the '\n' when you first read the line from your input,
before you even call strtok.

Generally, any program that deals with line-oriented input needs to decide
if it cares about the newline character at the end of every line. If not,
it's generally best to strip it out immediately after reading the line,
before the line is processed.

--
John Gordon A is for Amy, who fell down the stairs
go****@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

Nov 6 '07 #2
On Tuesday 06 Nov 2007 11:16 pm Sheldon <sh******@gmail.comwrote in
article <11**********************@22g2000hsm.googlegroups. com>:
Hi Everyone,

I have a file with a set of strings per line and each line ends with a
'\n' and each string separated by a whitespace.
A string in C is defined as being terminated by a null character.
Sequences of characters delimited by whitespace do not meet C's
definition of a string.
After reading each line and using strtok() to split the string into 3
strings, the last string is saved with the '\n' character still
attached. I would like to know if strtok can be used to fix this
problem or do I have to use strlen and and if statement to solve this?
Yes, strtok() can be sufficient for this. Just supply the newline
character in the delimiter string supplied to strtok().

Nov 6 '07 #3
John Gordon wrote On 11/06/07 13:00,:
In <11**********************@22g2000hsm.googlegroups. comSheldon <sh******@gmail.comwrites:

>>I have a file with a set of strings per line and each line ends with a
'\n' and each string separated by a whitespace.
After reading each line and using strtok() to split the string into 3
strings, the last string is saved with the '\n' character still
attached. I would like to know if strtok can be used to fix this
problem or do I have to use strlen and and if statement to solve this?


You could include '\n' in strtok's separator string
Yes.
and call strtok an
additional time.
No need.

--
Er*********@sun.com
Nov 6 '07 #4
On Tue, 06 Nov 2007 17:46:53 +0000, Sheldon wrote:
Hi Everyone,

I have a file with a set of strings per line and each line ends with a
'\n' and each string separated by a whitespace. After reading each line
and using strtok() to split the string into 3 strings, the last string
is saved with the '\n' character still attached. I would like to know if
strtok can be used to fix this problem or do I have to use strlen and
and if statement to solve this?

Any help is appreciated!

/S
Hello /S use \n in your delimiter array.

Jas.
Nov 6 '07 #5
On 6 Nov, 19:00, John Gordon <gor...@panix.comwrote:
In <1194371213.093758.200...@22g2000hsm.googlegroups. comSheldon <shejo...@gmail.comwrites:
I have a file with a set of strings per line and each line ends with a
'\n' and each string separated by a whitespace.
After reading each line and using strtok() to split the string into 3
strings, the last string is saved with the '\n' character still
attached. I would like to know if strtok can be used to fix this
problem or do I have to use strlen and and if statement to solve this?

You could include '\n' in strtok's separator string and call strtok an
additional time.

Or you could remove the '\n' when you first read the line from your input,
before you even call strtok.

Generally, any program that deals with line-oriented input needs to decide
if it cares about the newline character at the end of every line. If not,
it's generally best to strip it out immediately after reading the line,
before the line is processed.

--
John Gordon A is for Amy, who fell down the stairs
gor...@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
thanks!
It works with strtok(line, " \n");
/S

Nov 6 '07 #6
In <1194372436.531526@news1nwkEric Sosman <Er*********@sun.comwrites:
and call strtok an additional time.
No need.
Quite right. Not sure why I thought that was needed.

--
John Gordon A is for Amy, who fell down the stairs
go****@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

Nov 6 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: ern | last post by:
I'm using strtok( ) to capture lines of input. After I call "splitCommand", I call strtok( ) again to get the next line. Strtok( ) returns NULL (but there is more in the file...). That didn't...
20
by: bubunia2000 | last post by:
Hi all, I heard that strtok is not thread safe. So I want to write a sample program which will tokenize string without using strtok. Can I get a sample source code for the same. For exp:...
8
by: hu | last post by:
hi, everybody! I'm testing the fuction of strtok(). The environment is WinXP, VC++6.0. Program is simple, but mistake is confusing. First, the below code can get right outcome:"ello world, hello...
4
by: Michael | last post by:
Hi, I have a proble I don't understand when using strtok(). It seems that if I make a call to strtok(), then make a call to another function that also makes use of strtok(), the original call is...
3
by: nomad5000 | last post by:
Hi everybody! I'm having trouble using strtok to fill a matrix with int nrs. from a file. the code that is not working is the following: #include <iostream> #include <fstream> #include...
29
by: Pietro Cerutti | last post by:
Hello, here I have a strange problem with a real simple strtok example. The program is as follows: ### BEGIN STRTOK ### #include <string.h> #include <stdio.h>
11
by: Lothar Behrens | last post by:
Hi, I have selected strtok to be used in my string replacement function. But I lost the last token, if there is one. This string would be replaced select "name", "vorname", "userid",...
11
by: magicman | last post by:
can anyone point me out to its implementation in C before I roll my own. thx
12
by: Pilcrow | last post by:
Here is a quick program, together with its output, that illustrates what I consider to be a deficiency of the standard function strtok from <string.h>: I am using C:\>gcc --version gcc (GCC)...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.