Computing.Net > Forums > Programming > Need help with a C program.

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Need help with a C program.

Reply to Message Icon

Name: X_calibur
Date: February 2, 2004 at 22:34:42 Pacific
OS: Win2000
CPU/Ram: 512MB
Comment:

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 256

struct 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



Sponsored Link
Ads by Google

Response Number 1
Name: X_calibur
Date: February 2, 2004 at 23:21:52 Pacific
Reply:

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]);


0

Response Number 2
Name: Don Arnett
Date: February 3, 2004 at 08:18:50 Pacific
Reply:

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


0

Response Number 3
Name: X_calibur
Date: February 3, 2004 at 10:08:23 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Store Procedure in Sql ..... Windows Sockets



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Need help with a C program.

Need help with C++? www.computing.net/answers/programming/need-help-with-c/4642.html

Help with my C++ program www.computing.net/answers/programming/help-with-my-c-program/1813.html

I need Help explaining a C program www.computing.net/answers/programming/i-need-help-explaining-a-c-program/17576.html