473,396 Members | 2,057 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,396 software developers and data experts.

strtok but only want the last field

I have a file with lines like

1|22|333
4444|55555|666666
ccc|bb|a

I only need the last field. What I am doing now is using strtok to
split the lines and ignoring the first two fields. Can someone suggest
a better way to do this?
Jan 8 '08 #1
3 1539
In article <67**********************************@e4g2000hsg.g ooglegroups.com>,
vijay <vi******@gmail.comwrote:
>I have a file with lines like

1|22|333
4444|55555|666666
ccc|bb|a

I only need the last field. What I am doing now is using strtok to
split the lines and ignoring the first two fields. Can someone suggest
a better way to do this?
strrchr?

Depending on what you need them for, unix shell tools like sed or cut
might also be useful, and if they are they'd save you the trouble of
writing (much) code.
dave
(you could even go to The Dark Side and use Perl)

Jan 8 '08 #2
On Mon, 7 Jan 2008 18:54:54 -0800 (PST), vijay <vi******@gmail.com>
wrote in comp.lang.c:
I have a file with lines like

1|22|333
4444|55555|666666
ccc|bb|a

I only need the last field. What I am doing now is using strtok to
split the lines and ignoring the first two fields. Can someone suggest
a better way to do this?
Can you define what you mean by "better"? What's wrong with using
strtok(), assuming you use it correctly?

If it were me, I'd use strchr() four times, the last three starting at
the address returned by the previous call + 1. I would verify that
the first three all returned non-null pointers, and that the last one
returned a null pointer. That way I could recognize and reject
invalid input, and still have the string I read intact to output in an
error message.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Jan 8 '08 #3
vijay <vi******@gmail.comwrote:
I have a file with lines like
1|22|333
4444|55555|666666
ccc|bb|a
I only need the last field. What I am doing now is using strtok to
split the lines and ignoring the first two fields. Can someone suggest
a better way to do this?
#include <stdio.h> /* getchar(3) ferror(3) */
#include <stdlib.h> /* abort(3) */

#define MAXFIELDWIDTH 64

int main(void) {
char field[MAXFIELDWIDTH];
size_t p = 0;
int ch;

while (EOF != (ch = getchar())) {
switch (ch) {
case '\n':
printf("%.*s\n", (int)p, field);

/* FALL THROUGH */
case '|':
p = 0;

break;
default:
if (p >= sizeof field)
abort();

field[p++] = ch;
} /* switch() */
} /* while (!EOF) */

if (ferror(stdin))
abort();

return 0;
} /* main() */

Jan 8 '08 #4

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

Similar topics

4
by: JaNE | last post by:
as 1st, sorry on my clumsy english (now I realy miss some words) I have one language specific problem with my site. I would like to tell how meny pictures someone have in his or her gallery and I...
4
by: Lucius | last post by:
Hello everyone, I have a query problem. I'll put it like this. There is a 'publishers' table, and there is a 'titles' table. Publishers publish titles (of course). Now I want to make a query (in...
1
by: Bob C. | last post by:
I frequently find that the last field in an attached table does not show up on the list of that table's fields in the query design view. Any ideas? Bob C.
2
by: john.lum | last post by:
My overall objective is to create something akin to Google Suggest, where a query is done in response to changes in a text field presented to the user. I've got things working using the onkeyup...
2
by: David Lozzi | last post by:
Hello, I have an arraylist that loads a checkboxlist. I populate the arraylist using a custom class and pulling the data from the web.config file. When the checkboxlist is loaded, only the last...
3
by: solar | last post by:
I need to copy the tables orders and orderdetails containng only the last order.I am copying them to another database using the formula DoCmd.CopyObject db.Name, "orders", acTable, "orders"...
14
by: Tina | last post by:
My employer tracks productivity/performance of clinicians (how much they bill) each week, its averages for the month, and the 6 months. These averages are compared to their expected productivity....
10
by: mankolele | last post by:
Hi all. I am trying to get the input from a form to print it out on a page where the information filled on a form must be on the page in a letter manner.But now what is happening I do that but it...
14
by: SunnyC | last post by:
I have a table as below in test.mdb Access database. on a form, I want to use lookup to capture the the last field name in a text on the form, in this case, it is 200906. by using some code I...
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.