473,378 Members | 1,457 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,378 software developers and data experts.

How to get data between 2 delimiters using strtok

i'm using strtok function to read a file....
but it does not give correct output if the delimiter is without any value in between..
example ~ is my delimiter..
and text file line is-
data1~data2~data3~~data4
strtok function will not give me NULL value between 2 delimiter tht is data3~~data4
Please help me to read this null char also .... and parse the line properly..
May 19 '07 #1
4 3828
Savage
1,764 Expert 1GB
i'm using strtok function to read a file....
but it does not give correct output if the delimiter is without any value in between..
example ~ is my delimiter..
and text file line is-
data1~data2~data3~~data4
strtok function will not give me NULL value between 2 delimiter tht is data3~~data4
Please help me to read this null char also .... and parse the line properly..
Hi,

what is the purpose of using double delimiter beetwen 2 data's?

Please, can u explain liitle better ur problem?
May 19 '07 #2
AdrianH
1,251 Expert 1GB
i'm using strtok function to read a file....
but it does not give correct output if the delimiter is without any value in between..
example ~ is my delimiter..
and text file line is-
data1~data2~data3~~data4
strtok function will not give me NULL value between 2 delimiter tht is data3~~data4
Please help me to read this null char also .... and parse the line properly..
Sorry, strtok() will run all delimiters that it encounters together and process it as one.

Try using these:
Expand|Select|Wrap|Line Numbers
  1. // read in a token
  2. scanf("%<bufferSize>[^~]", buffer);
  3.  
  4. // ignore \n\r chars
  5. scanf("%*[\r\n]");
  6.  
  7. // read in a char
  8. getchar();
  9.  
or others like them (see scanf and related functions). <bufferSize> is to be replace with a number indicating the buffers size. Remeber to check the return value.

Adrian
May 19 '07 #3
AdrianH
1,251 Expert 1GB
Sorry, strtok() will run all delimiters that it encounters together and process it as one.

Try using these:
Expand|Select|Wrap|Line Numbers
  1. // read in a token
  2. scanf("%<bufferSize>[^~]", buffer);
  3.  
  4. // ignore \n\r chars
  5. scanf("%*[\r\n]");
  6.  
  7. // read in a char
  8. getchar();
  9.  
or others like them (see scanf and related functions). <bufferSize> is to be replace with a number indicating the buffers size. Remeber to check the return value.

Adrian
I hope to have a somewhat complete parsing tutorial posted here at TSDN before the end of the weekend. Stay tuned. :)


Adrian
May 19 '07 #4
AdrianH
1,251 Expert 1GB
Done. See here for more info on parsing in C.


Adrian
May 19 '07 #5

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

Similar topics

19
by: Johnny Google | last post by:
Here is an example of the type of data from a file I will have: Apple,4322,3435,4653,6543,4652 Banana,6934,5423,6753,6531 Carrot,3454,4534,3434,1111,9120,5453 Cheese,4411,5522,6622,6641 The...
33
by: Geometer | last post by:
Hello, and good whatever daytime is at your place.. please can somebody tell me, what the standard behavior of strtok shall be, if it encounters two or more consecutive delimiters like in...
5
by: gpaps87 | last post by:
hi, i wanted to know whether we can use strtok command to mark delimiters as tokens as well.In Java,we have a command: StringTokennizer(String str, String delimiters, boolean delimAsToken) ...
4
by: Marco Trapanese | last post by:
Hi, I'm trying to parse strings on an Atmel AVR device. I use the WinAVR C Compiler (GCC) The strings to parse are like this: command -par0 -par1 -parn I use strok_r function:
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.