fscanf doesn't work

I don't understand this, I am trying to read data from a file, It just does not work!!
1
2
3
4
5
6
7
8
while( true ) {
		int vals_read = fscanf(fileHandle,"%[^;]%s",&name,&pass);
		if( vals_read == 2 ) {
			// process the line
			table.Insert(pass,name);
		} else if( feof( fileHandle ) )
			break;
	}

I have also tried using "%s %s","%s:%s", "%[^;];%s"

My file will be consisting of a Name and a Password, both will be strings.
This is how my file looks now: Andy;TEST

While debugging, I find the pass variable is a single "T" and the name has nothing in it.

Also while debugging, if I check the status of fileHandle(my file) it has this "value" : fileHandle = 0x1035e4f8 {_ptr=0x003d7390 "Andy;TESTÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ...


Please help
The information you gave is insufficient. I'm trying to figure out what you omitted, but I don't like guessing and this is definitely an impediment in giving you a clear solution. So I suppose that you have:

1
2
3
4
FILE *fileHandle;
std::string name, pass;
int vals_read;
std::vector<???> table;


spill out more details and I'll try to debug it.
closed account (z05DSL3A)
dAND3h,

As userulluipeste said, there is not enough info to determine what is going on. Sometimes it is worth creating a small full program to demonstrate the issue you are having, in doing so you may solve your own problem.
eg:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>

int main()
{
    char name_str [80];
    char password_str [80];
    
    FILE * pFile;

    // creat a test file
    pFile = fopen ("myfile.txt","w+");
    fprintf (pFile, "%s;%s", "Andy", "Test");
    rewind (pFile);

    // try reading it...
    while(true)
    {
        int vals_read = fscanf (pFile, "%[^;]%s", name_str, password_str);
        if( vals_read == 2 ) 
        {
            printf ("I have read: %s and %s \n",name_str, password_str);
        }
        else if( feof( pFile ) )
			break;
    }

    fclose (pFile);

    return 0;
}

I have read: Andy and ;Test
Last edited on
Hey, thanks for the replies, it turns out I was trying to pass a std::string instead of a c-style string, so it was messing up.

But, I now Have a new problem, This is how my code now looks:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
std::string tmpname = "";
	std::string tmppass = "";
	const char* name = tmpname.c_str();
	const char* pass = tmppass.c_str();
	int health = -1;
	int armour = -1;
	int lives = -1;

	while( true ) {
		Player user;
		int vals_read = fscanf(fileHandle,"%s %s %d %d %d",name,pass,health,armour,lives);
		if( vals_read == 5 ) {
			// process the line
			user.setName(name);
			user.setPass(pass);
			user.setHealth(health);
			user.setArmour(armour);
			user.setLives(lives);

			table.Insert(user.getName(),user);
		} else if( feof( fileHandle ) )
			break;
	}
	fclose(fileHandle);

As you can see, I read in 2 sets of cstrings and then 3 numbers,all separated by a space. Now, because %s reads until a newline/space (according to what I read on one of the tutorials here), I thought this code should work. But, it doesn't.

My text file now looks like this:
Jack testpass 100 50 4

only 1 line. I found it actually loops twice, the first time it loops, name and pass are correct, but health,armour and lives are still all default -1. The second time it loops, name and pass are equal to 100 and 50 respectively. Thanks for your help :P
Lol nevermind, I wasn't using a reference to the numbers..........If anyone runs into this problem. Don't forget to either use a pointer or a reference do store the values in.

e.g.
fscanf(fileHandle,"%s %s %d:%d:%d",name,pass,&health,&armour,&lives);

name and pass are already char*. :D
Topic archived. No new replies allowed.