Extracting a field in a txt file

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

Join Date: Mar 2006
Posts: 7
Reputation: squall8985 is an unknown quantity at this point 
Solved Threads: 0
squall8985 squall8985 is offline Offline
Newbie Poster

Extracting a field in a txt file

 
0
  #1
Mar 2nd, 2006
1 John US 1234.00
2 Jessica Alba United Kingdom 23789.00
3 David UK 12.00



Suppose i've this data in my txt file. How am i going to scan the file to get the deposit field(the one in italic) for each user?

Thank you...
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,756
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 740
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Code Goddess

Re: Extracting a field in a txt file

 
0
  #2
Mar 2nd, 2006
You search for the last delimiter (a space) and take whatever characters exist after it. This works because the deposit field is the last in the record line:
  1. char *deposit = strrchr ( line, ' ' );
  2.  
  3. if ( deposit != NULL ) {
  4. ++deposit; /* Skip the delimiter */
  5.  
  6. /* Process deposit */
  7. }
In C++ with the string class, you would do the same thing in a different way:
  1. string::size_type index = line.find_last_of ( ' ' );
  2.  
  3. if ( index != string::npos ) {
  4. string deposit = line.substr ( index + 1 );
  5.  
  6. // Process deposit
  7. }
I see an issue in further tokenization though. You allow for both one and two word names and locations, but you don't differentiate between the two. So if you write code to break the fields up into tokens, either John will work, or Jessica alba will work, but not both. Your file format isn't conducive to easy tokenizing.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,473
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: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Extracting a field in a txt file

 
0
  #3
Mar 2nd, 2006
you will probably want to truncate all trailing spaces first before using Narue's solution. Otherwise it will not work if the line is like this
  1. "1 John US 1234.00 "
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 7
Reputation: squall8985 is an unknown quantity at this point 
Solved Threads: 0
squall8985 squall8985 is offline Offline
Newbie Poster

Re: Extracting a field in a txt file

 
0
  #4
Mar 2nd, 2006
Originally Posted by Ancient Dragon
you will probably want to truncate all trailing spaces first before using Narue's solution. Otherwise it will not work if the line is like this
  1. "1 John US 1234.00 "
I see the issue there. How am i going to truncate all the trailing spaces?

I already write a code to extract the deposit field

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. void main()
  5. {
  6. char client[200];
  7.  
  8. char *deposit;
  9.  
  10. float deposits[8];
  11.  
  12. int i;
  13.  
  14. FILE *f;
  15.  
  16. f = fopen("clients.txt", "r");
  17.  
  18. i = 0;
  19.  
  20. while (!feof(f)) {
  21.  
  22. fflush(stdin);
  23. fgets(client, 200, f);
  24.  
  25. deposit = strrchr ( client, ' ' );
  26.  
  27. sscanf(deposit, "%f", &deposits[i]);
  28.  
  29. ++i;
  30. }
  31.  
  32.  
  33.  
  34. for ( i = 0; i < 8; i++)
  35. {
  36. printf("%f\n", deposits[i]);
  37. }
  38.  
  39. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,473
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: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Extracting a field in a txt file

 
0
  #5
Mar 2nd, 2006
just write a short trim() function that searches the buffer backwards from end to beginning and put a 0 byte just after the first non-white-space character. In C do it like this: c++ would be similar. make sure you do not pass a string literal to this function because string literals are normally in read-only memory.
  1. #include <ctype.h>
  2.  
  3. void trim(char* buf)
  4. {
  5. char* s = buf + strlen(buf)-1;
  6. while( s >= buf)
  7. {
  8. if( !isspace(*s))
  9. {
  10. *(s+1) = 0;
  11. break;
  12. }
  13. s--;
  14. }
  15. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,473
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: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Extracting a field in a txt file

 
0
  #6
Mar 2nd, 2006
  1. fflush(stdin);

That is non-standard and may cause unpredictable results. fflush() is only guarenteed to work on output devices. Why are you using it in your program anyway -- the program isn't getting any input from stdin. Just delete that line.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 7
Reputation: squall8985 is an unknown quantity at this point 
Solved Threads: 0
squall8985 squall8985 is offline Offline
Newbie Poster

Re: Extracting a field in a txt file

 
0
  #7
Mar 2nd, 2006
fflush(stdin);

Basically wut does fflush do? I dont quite understand it. I read a few books about it but still doesnt quite understand how and the purpose of fflush(stdin).
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,473
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: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Extracting a field in a txt file

 
0
  #8
Mar 2nd, 2006
Originally Posted by squall8985
fflush(stdin);

Basically wut does fflush do? I dont quite understand it. I read a few books about it but still doesnt quite understand how and the purpose of fflush(stdin).
There is NEVER an appropriate use fflush(stdin) -- fflush is used to force the operating system to write buffered information to the hard drive (or other mass storage device), stdin is a file handle used for keyboard input or sometimes redirected data file input, so fflush(stdin) has undefined behavior.

If you don't know what a function does then avoid using it until you read and understand its behaviors.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,756
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 740
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Code Goddess

Re: Extracting a field in a txt file

 
0
  #9
Mar 2nd, 2006
>Basically wut does fflush do? I dont quite understand it.
Then why are you using it in your code? There's nothing more dangerous than using something you don't fully understand.

>There is NEVER an appropriate use fflush(stdin)
Unless the implementation defines it, but that's not something we encourage.

>you will probably want to truncate all trailing spaces first before using Narue's solution
This step could be necessary, or it could be a glorified no-op depending on the formatting of the file. I would change your statement to "You may need to truncate all trailing spaces". That way you don't scare the OP into using something that isn't needed.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC