Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Okay, the problem that I'm having is, I can read the name and first two pairs of numbers. Beyond that it seems that the file pointer seems to go bonkers. I think it is probably how I'm using the structures, but I hope one of you can help put me on the right track. n.n
Here be the code of doom:
// Room walk demo.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>// Just in case. =p
#define BUFFER 256struct rdata {
char *name;
char *desc;
int exits[12][2];
};
struct adata {
char *name;
int vrange[6];
};int main(void) {
// Our lovely variables and functions.
void resize_room(int, struct rooms);
struct rdata *rooms;
struct adata *areas;
int numrooms, numareas, player;
numrooms = numareas = 2;
player = 1;
int x;
char buffer[BUFFER];
FILE *fp;
// Do initial malloc'ing.
rooms = (struct rdata *) malloc(sizeof(struct rdata));
areas = (struct adata *) malloc(sizeof(struct adata));
// Make sure they were successful.
if (areas == (struct adata *) NULL || rooms == (struct rdata *) NULL) {
printf("\nERROR! Malloc failed for rooms and areas!\n");
exit(1);
}
// Open file and process
if ((fp = fopen("test.txt","rb")) == NULL) {
printf("\nERROR! File not found!\n");
exit(1);
}
// Start of processing.
fscanf(fp,"#%s ",buffer);
if (strcmp(buffer,"AREA") != 0) {
printf("\nERROR! File not of proper format!\n");
exit(1);
}
fgets(buffer,BUFFER,fp);
buffer[(strlen(buffer)-1)] = '\0';
if ((areas[numareas-1].name = (char *) malloc(strlen(buffer))) == NULL) {
printf("ERROR! Unable to allocate space for room name!\n");
exit(1);
}
strcpy(areas[numareas-1].name,buffer);
// Screw-up zone.
fgets(buffer,BUFFER,fp);
sscanf(buffer,"#RVNUMS%d%d\n",&areas[numareas-1].vrange[0],&areas[numareas-1].vrange[1]);
fgets(buffer,BUFFER,fp);
sscanf(buffer,"#OVNUMS%d%d\n",&areas[numareas-1].vrange[2],&areas[numareas-1].vrange[3]);
fgets(buffer,BUFFER,fp);
fscanf(fp,"MVNUMSd%d\n",&areas[numareas-1].vrange[4],&areas[numareas-1].vrange[5]);// House cleaning.
fclose(fp);
free(areas);
}
// Our semi destructive resizer. Pity it destroys part of the first element.
void resize_room(int newsize, struct rdata *arr) {
if ((arr = (struct rdata *) realloc(arr,newsize*sizeof(struct rdata))) == NULL) {
printf("\nERROR! Unable to expand room array!\n");
exit(1);
}
}And sample file content:
#AREA Test Zone
#RVNUMS 21 4
#OVNUMS 2 91
#MVNUMS 3 172
#RNUM 1

Oi, bad paste.
These two go where the last fscanf is.
fscanf(fp,"MVNUMS%d%d\n",&areas[numareas-1].vrange[4],&areas[numareas-1].vrange[5]);
printf("%s %d %d %d %d %d %d\n",areas[numareas-1].name,areas[numareas-1].vrange[0],areas[numareas-1].vrange[1],areas[numareas-1].vrange[2],areas[numareas-1].vrange[3],areas[numareas-1].vrange[4],areas[numareas-1].vrange[5]);

I see two possible problems:
For RVNUMS and OVNUMS, you fgets the record into a buffer and then use sscanf to read the data into the structure. This works fine although I'm not sure why you don't just fscanf into the structure.
But on MVNUMS, you fgets and then fscanf (rather than sscanf). The fgets is going to pull the MVNUMS line out of the file and into buffer. But fscanf is expecting the MVNUMS line to be in the file, not buffer.
Also, the fscanf is looking for "MVNUMS" rather than "#MVNUMS". I don't remember how fscanf/sscanf will handle the missing #.

Well, that's another goof. Mebbe I shouldn't code to the wee hours. @.@ I used fgets to place check printf's, so I could be sure what line is being read. And finally it works as I want it to. Thank you for your assistance. =)

![]() |
Store Procedure in Sql .....
|
Windows Sockets
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |