944,087 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2121
  • C RSS
Mar 2nd, 2006
0

Extracting a field in a txt file

Expand Post »
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...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
squall8985 is offline Offline
7 posts
since Mar 2006
Mar 2nd, 2006
0

Re: Extracting a field in a txt file

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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 2nd, 2006
0

Re: Extracting a field in a txt file

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 "
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,957 posts
since Aug 2005
Mar 2nd, 2006
0

Re: Extracting a field in a txt file

Quote 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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
squall8985 is offline Offline
7 posts
since Mar 2006
Mar 2nd, 2006
0

Re: Extracting a field in a txt file

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. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,957 posts
since Aug 2005
Mar 2nd, 2006
0

Re: Extracting a field in a txt file

  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,957 posts
since Aug 2005
Mar 2nd, 2006
0

Re: Extracting a field in a txt file

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).
Reputation Points: 10
Solved Threads: 0
Newbie Poster
squall8985 is offline Offline
7 posts
since Mar 2006
Mar 2nd, 2006
0

Re: Extracting a field in a txt file

Quote 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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,957 posts
since Aug 2005
Mar 2nd, 2006
0

Re: Extracting a field in a txt file

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Sorting from a file
Next Thread in C Forum Timeline: Need immediate help ( Challenging software )





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC