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:
char *deposit = strrchr ( line, ' ' );
if ( deposit != NULL ) {
++deposit; /* Skip the delimiter */
/* Process deposit */
}
In C++ with the string class, you would do the same thing in a different way:
string::size_type index = line.find_last_of ( ' ' );
if ( index != string::npos ) {
string deposit = line.substr ( index + 1 );
// Process deposit
}
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.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
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 John US 1234.00 "
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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.
#include <ctype.h>
void trim(char* buf)
{
char* s = buf + strlen(buf)-1;
while( s >= buf)
{
if( !isspace(*s))
{
*(s+1) = 0;
break;
}
s--;
}
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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 keyboardinput 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.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
>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.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401