|
-
June 21st, 2004, 10:15 PM
#1
Can you spot the problem?
No, this isn't a gameshow. I have some code, and what it does is get some data from a TXT and fill it into a structure. Unfortunatly fscanf() isn't filling anything, and I'm afraid it has something to do with the current position. Here's code:
Code:
/* header */
typedef struct user
{
char *username;
char *password;
char *NAME;
int wins;
int lost;
int games;
int percentofwin;
int coins;
int track[16];
} user;
/* cpp */
#include "main.h"
...
int loadUserInfo()
{
int userCount;
int i;
userstxt = fopen("c:/users.txt", "r");
if(userstxt == NULL)
return -1;
fscanf(userstxt, "USRS %d", &userCount);
// Depending how long the number is, it will have a different seek.
if(userCount <= 9)
{
if(fseek(userstxt,8L,SEEK_SET)) return -1;
}
if(userCount > 9)
{
if(fseek(userstxt,9L,SEEK_SET)) return -1;
}
if(userCount > 99)
{
if(fseek(userstxt,10L,SEEK_SET)) return -1;
}
if(userCount > 999)
{
if(fseek(userstxt,11L,SEEK_SET)) return -1;
}
for(i = userCount; i != 0; i--)
{
fscanf(userstxt,"- %s PWD %s NAME %s WON %d LST %d GMS %d PNT %d CNS %d LC %d PB %d BP %d DD %d MB %d MC %d DC %d WS %d SL %d MC2 %d DK %d WC %d DJ %d BC %d RR %d",
&users[i].username,&users[i].password,&users[i].NAME,&users[i].wins,&users[i].lost,
&users[i].games,&users[i].percentofwin,&users[i].coins,&users[i].track[0],
&users[i].track[1],&users[i].track[2],&users[i].track[3],&users[i].track[4],
&users[i].track[5],&users[i].track[6],&users[i].track[7],&users[i].track[8],
&users[i].track[9],&users[i].track[10],&users[i].track[11],&users[i].track[12],
&users[i].track[13],&users[i].track[14],&users[i].track[15],&users[i].track[16]);
}
/* the txt */
USRS 1
- Dark Cube PWD whatever NAME Dark Cube WON 0 LST 0 GMS 0 PNT 0 CNS 250 LC 0 PB 0 BP 0 DD 0 MB 0 MC 0 DC 0 WS 0 SL 0 MC2 0 DK 0 WC 0 DJ 0 BC 0 RR 0
fscanf doesn't fill the structure with anything. I am puzzled.
Need help with anything related to audio programming? I can help!
-
June 21st, 2004, 10:27 PM
#2
Post an example of the content of the txt file.
In general since you are working with arbitrary data size, one solution is to design a custom character(s) to seperate the different elements in the structure.
Kuphryn
-
June 21st, 2004, 10:58 PM
#3
Where have you allocated memory to these members
Code:
char *username;
char *password;
char *NAME;
moreover, you seemed to using pointer to a pointer here...
check these things out again.
-
June 22nd, 2004, 12:05 PM
#4
Kurphyn wrote:
Post an example of the content of the txt file.
I did.
Kurphyn wrote:
One solution is to design a custom character(s) to seperate the different elements in the structure.
How would that help me if I'm already using fscanf?
Thanks Amit, I fixed that.
The TXT contains names of users, and they all have like, a profile. What I need to do is scan all of them into a structure. The program will obviously use the USRS n to tell how many times to loop, fscanf, fill the structure, go to next line, blah blah blah.
It doesn't look like it's going to work that way, so what I'll do is scan the first three lines. USRS n, whitespace, and the first profile.
Depending on how mane USRS n there are, I can go to the next line, then fill it with a new structure by using the array, users[2000]. Just one question. How do you move to the next line?
Need help with anything related to audio programming? I can help!
-
June 22nd, 2004, 12:26 PM
#5
How do I move to a new line?
Need help with anything related to audio programming? I can help!
-
June 22nd, 2004, 12:56 PM
#6
Need help with anything related to audio programming? I can help!
-
June 22nd, 2004, 01:29 PM
#7
Please...this is really urgent!!
Need help with anything related to audio programming? I can help!
-
June 22nd, 2004, 04:50 PM
#8
I don't understand why you are using fseek, do you want to skip to a particular user and not read in the whole set?
I just gave a quick once over and think the fseek may be the problem although I would need to know more specifically why it is there.
-----------EDIT-----------------------
I looked again and I think I get it. Are you using fseek to skip over the digits of USERS? If so you don't need to because scanf already moves the read cursor beyond that data when you read in its value. I think you should be fine if you remove the fseek stuff entirely.
Last edited by imposterrific; June 22nd, 2004 at 04:53 PM.
-
June 22nd, 2004, 05:08 PM
#9
I see two problems:
1) You do fseek() (as already pointed out). The file position is moved automatically:
USRS 4
<first entry>
<second entry>
<etc etc>
After the fscanf(fp, "USRS %d", &users), the next pos in the file will be the second line (first entry).
2) Strings that contains spaces won't work very well with fscanf, like the username Dark Cube.
Last edited by j0nas; June 22nd, 2004 at 06:28 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|