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...

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.

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          "

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          "

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

#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.

#include <ctype.h>

void trim(char* buf)
{
    char* s = buf + strlen(buf)-1;
    while( s >= buf)
    {
        if( !isspace(*s))
        {
           *(s+1) = 0;
           break;
        }
        s--;
    }
}
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.

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).

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.

>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.

This article has been dead for over six months. Start a new discussion instead.