| | |
Reading text file ...
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2007
Posts: 5
Reputation:
Solved Threads: 0
Hey guyz.. I want to know how to print certain line from the text file and certain part of the record for example
data.txt...
I only want to print out the name from the record in the text file..
for example when the user enter 222 for the id it should print out the id and the name .... 222 chris brown
anyone please help ....thx
data.txt...
C Syntax (Toggle Plain Text)
123 John Smith 80 222 chris brown 50 325 christine 60
for example when the user enter 222 for the id it should print out the id and the name .... 222 chris brown
anyone please help ....thx
Since the name may or may not contain spaces you will have to
1) read the entire line into a character array. You can use fgets() to do that.
2) use a char pointer to advance from the beginning of the string to the beginning of the name. Copy all remaining characters into another character buffer.
3) in the new buffer from 2) above locate the end of the string, back up to the first space, then back up some more until there are no more characters. That will locate the end of the name. At that spot add a NULL character -- '\0'.
1) read the entire line into a character array. You can use fgets() to do that.
2) use a char pointer to advance from the beginning of the string to the beginning of the name. Copy all remaining characters into another character buffer.
3) in the new buffer from 2) above locate the end of the string, back up to the first space, then back up some more until there are no more characters. That will locate the end of the name. At that spot add a NULL character -- '\0'.
C Syntax (Toggle Plain Text)
char iobuf[] = "222 chris brown 50"; char tbuf[40] = {0}; char* ptr; // find the location of the first character in the name // // find the first space ptr = iobuf; while(*ptr && !isspace(*ptr) ) ptr++; // now find the first non-space while(*ptr && isspace(*ptr)) ptr++; // ok, we're at the first letter of the name // so copy remainder to new buffer strcpy(tbuf,ptr); // now locate end of string ptr = tbuf + strlen(tbuf) - 1; // back up to first space while(!isspace(*ptr) ) ptr--; // back up to first non-space while(isspace(*ptr) ) ptr--; // we're now at the end of the name. So truncate the string *(ptr+1) = 0; // print the name printf("%s\n", tbuf);
Last edited by Ancient Dragon; Aug 13th, 2008 at 11:24 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Another method
C Syntax (Toggle Plain Text)
char iobuf[] = "222 chris brown 50"; char* ptr; // find beginning of name while( !isalpha(*ptr)) ptr++; memmove(iobuf,ptr,strlen(ptr)+1); // find end of name ptr = iobuf + strlen(iobuf)-1; while(!isalpha(*ptr)) ptr--; // truncate *(ptr+1) = 0;
Last edited by Ancient Dragon; Aug 13th, 2008 at 11:48 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
•
•
Another method
C Syntax (Toggle Plain Text)
char iobuf[] = "222 chris brown 50"; char* ptr; // find beginning of name while( !isalpha(*ptr)) ptr++; memmove(iobuf,ptr,strlen(ptr)+1); // find end of name ptr = iobuf + strlen(iobuf)-1; while(!isalpha(*ptr)) ptr--; // truncate *(ptr+1) = 0;
•
•
•
•
[...]
for example when the user enter 222 for the id it should print out the id and the name .... 222 chris brown
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
More duplicated effort - http://cboard.cprogramming.com/showthread.php?t=106219
Consider...
http://www.catb.org/~esr/faqs/smart-...ons.html#forum
Consider...
http://www.catb.org/~esr/faqs/smart-...ons.html#forum
![]() |
Similar Threads
- c++ reading text file (C++)
- How to select data frm text file based on a condition (C)
- Help with a 2D array from a text file (C++)
- read text file (C)
- Reading in a text file string is not complete (Pascal and Delphi)
- reading txt file into array (C++)
Other Threads in the C Forum
- Previous Thread: memory leak
- Next Thread: getting the name of the members of a C structure
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming spoonfeeding stack standard string strings structures student suggestions systemcall test testautomation unix user variable voidmain() wab win32api windows.h






