| | |
Extracting a field in a txt file
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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:
In C++ with the string class, you would do the same thing in a different way:
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.
C Syntax (Toggle Plain Text)
char *deposit = strrchr ( line, ' ' ); if ( deposit != NULL ) { ++deposit; /* Skip the delimiter */ /* Process deposit */ }
C Syntax (Toggle Plain Text)
string::size_type index = line.find_last_of ( ' ' ); if ( index != string::npos ) { string deposit = line.substr ( index + 1 ); // Process deposit }
I'm here to prove you wrong.
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
C Syntax (Toggle Plain Text)
"1 John US 1234.00 "
•
•
Join Date: Mar 2006
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
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
C Syntax (Toggle Plain Text)
"1 John US 1234.00 "
I already write a code to extract the deposit field
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> void main() { char client[200]; char *deposit; float deposits[8]; int i; FILE *f; f = fopen("clients.txt", "r"); i = 0; while (!feof(f)) { fflush(stdin); fgets(client, 200, f); deposit = strrchr ( client, ' ' ); sscanf(deposit, "%f", &deposits[i]); ++i; } for ( i = 0; i < 8; i++) { printf("%f\n", deposits[i]); } }
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.
C Syntax (Toggle Plain Text)
#include <ctype.h> void trim(char* buf) { char* s = buf + strlen(buf)-1; while( s >= buf) { if( !isspace(*s)) { *(s+1) = 0; break; } s--; } }
C Syntax (Toggle Plain Text)
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.
•
•
•
•
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).
If you don't know what a function does then avoid using it until you read and understand its behaviors.
>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.
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.
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: Sorting from a file
- Next Thread: Need immediate help ( Challenging software )
| Thread Tools | Search this Thread |
* adobe api append array arrays bash binarysearch centimeter char character cm copyanyfile copypdffile createcopyoffile createprocess() csyntax directory dynamic execv feet fgets file floatingpointvalidation fork frequency function getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux highest homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer license linked linkedlist linux linuxsegmentationfault list lowest match matrix meter microsoft mqqueue multi mysql oddnumber odf open openwebfoundation overwrite pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scheduling segmentationfault send shape single socketprogramming stack standard strchr string strings suggestions test testautomation unix urboc user whythiscodecausesegmentationfault win32api windows.h windowsapi






