473,385 Members | 1,351 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.

Getting array values using strtok

hi all,
if we use strtok toseparate a line into two parts,say rola : 23

Expand|Select|Wrap|Line Numbers
  1. for(i=0;i<index;i++){
  2. ptr=strtok(line[i],":");
  3. while(ptr!=NULL)
  4. {
  5. strcpy(num[i],ptr);
  6. ptr=strtok(NULL,":");
  7. }
  8. }
the second part(23) is saved in the array num,,,,,how do we save the first part(rola) in a second array???
Mar 28 '10 #1
3 4604
jkmyoung
2,057 Expert 2GB
ptr=strtok(NULL,":");
This makes no sense and will always return null. Why even have the while loop?

I think you want something like:
strncpy(array2[i], line[i], (ptr-line[i])/sizeof(char));

http://www.cppreference.com/wiki/c/string/strncpy
Mar 29 '10 #2
Banfa
9,065 Expert Mod 8TB
ptr=strtok(NULL,":");
This makes no sense and will always return null. Why even have the while loop?
I think you need to read the strtok documentation yourself. You call it the first time with a pointer to your string and the list of terminators and it returns the first token. Then you call it on subsequent times with a pointer to NULL and the terminators and it returns the 2nd, 3rd ... etc token on the line until it finally returns NULL when there nothing left on the line.

Strictly speaking there is no need to save the first part, that is what line[i] is after the first call to strtok.

Generally strtok is a poor library function because it used static internal buffers to perform the tokenisation and so is not re-entrant or thread safe.
Mar 29 '10 #3
jkmyoung
2,057 Expert 2GB
Interesting; I have indeed blundered. See reference:
http://www.cppreference.com/wiki/c/string/strtok

You are probably overwriting the first result. I still do not recommend the use of your while loop as you don't really seem to need to loop.
Mar 29 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Roderik | last post by:
Hi, I have an array in javascript of which the contents are manipulated by the user. When he is done, he calls a function to reload the page. Then conclusion depending on the input should be...
26
by: Kai Jaensch | last post by:
Hello, i am an newbie and i have to to solve this problem as fast as i can. But at this time i donīt have a lot of success. Can anybody help me (and understand my english :-))? I have a...
3
by: Kai Jaensch | last post by:
Hello, i am an newbie and i have to to solve this problem as fast as i can. But at this time i donīt have a lot of success. Can anybody help me (and understand my english :-))? I have a...
3
by: Goh, Yong Kwang | last post by:
I'm trying to create a function that given a string, tokenize it and put into a dynamically-sized array of char* which is in turn also dynamically allocated based on the string token length. I...
15
by: John Smith | last post by:
I would like to parse a string into an array. I found on the net the following codes which parse a string and print it. The result is exactly what I want: char * pch; pch = strtok (buffer," ");...
5
by: Robert Oschler | last post by:
I am converting a Perl script over to "C" for a potential open source project. I need some open source "C" code that will give me the same functionality of a Perl Style associative array: ...
11
by: miketigerwoods | last post by:
I'm having problems where I need to parse a command line, and place each token from strtok() into an array. I've read here: http://www.phim.unibe.ch/comp_doc/c_manual/C/CONCEPT/arrays.html (at...
1
by: Marcelo | last post by:
Hi, I have to do the following, and would like you suggestions on how I could achieve it the best way: I have a text file in the following format: 12345 54321 98765 56789 (3 spaces...
2
by: bluebeta | last post by:
Hi, I am using embedded visual C++ 4.0 and I want to use function strtok to split string into array. my sample code is as below: ...
6
by: Studlyami | last post by:
Okay, i have developed a file parser in C++ that i am trying to being into a c# program which is proving to be a lot more difficult than i thought. first i scan a file (which i opened using a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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.