Reading text file ...

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2007
Posts: 5
Reputation: mancode1007 is an unknown quantity at this point 
Solved Threads: 0
mancode1007 mancode1007 is offline Offline
Newbie Poster

Reading text file ...

 
0
  #1
Aug 13th, 2008
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...
  1. 123 John Smith 80
  2. 222 chris brown 50
  3. 325 christine 60
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,620
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1492
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Reading text file ...

 
0
  #2
Aug 13th, 2008
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. char iobuf[] = "222 chris brown 50";
  2. char tbuf[40] = {0};
  3. char* ptr;
  4. // find the location of the first character in the name
  5. //
  6. // find the first space
  7. ptr = iobuf;
  8. while(*ptr && !isspace(*ptr) )
  9. ptr++;
  10. // now find the first non-space
  11. while(*ptr && isspace(*ptr))
  12. ptr++;
  13. // ok, we're at the first letter of the name
  14. // so copy remainder to new buffer
  15. strcpy(tbuf,ptr);
  16. // now locate end of string
  17. ptr = tbuf + strlen(tbuf) - 1;
  18.  
  19. // back up to first space
  20. while(!isspace(*ptr) )
  21. ptr--;
  22. // back up to first non-space
  23. while(isspace(*ptr) )
  24. ptr--;
  25. // we're now at the end of the name. So truncate the string
  26. *(ptr+1) = 0;
  27.  
  28. // print the name
  29. 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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,620
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1492
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Reading text file ...

 
0
  #3
Aug 13th, 2008
Another method
  1. char iobuf[] = "222 chris brown 50";
  2. char* ptr;
  3. // find beginning of name
  4. while( !isalpha(*ptr))
  5. ptr++;
  6. memmove(iobuf,ptr,strlen(ptr)+1);
  7. // find end of name
  8. ptr = iobuf + strlen(iobuf)-1;
  9. while(!isalpha(*ptr))
  10. ptr--;
  11. // truncate
  12. *(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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,048
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 178
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Reading text file ...

 
0
  #4
Aug 14th, 2008
Originally Posted by Ancient Dragon View Post
Another method
  1. char iobuf[] = "222 chris brown 50";
  2. char* ptr;
  3. // find beginning of name
  4. while( !isalpha(*ptr))
  5. ptr++;
  6. memmove(iobuf,ptr,strlen(ptr)+1);
  7. // find end of name
  8. ptr = iobuf + strlen(iobuf)-1;
  9. while(!isalpha(*ptr))
  10. ptr--;
  11. // truncate
  12. *(ptr+1) = 0;
That obtains the string name out of the buffer, but I believe that what the OP is asking leads to hash table lookup.
Originally Posted by mancode1007 View Post
[...]
for example when the user enter 222 for the id it should print out the id and the name .... 222 chris brown
Most likely it's not going to help you, but perhaps you get the idea of the scope of it. Here
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Reading text file ...

 
1
  #5
Aug 14th, 2008
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC