| | |
Structures, external files, and functions
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2007
Posts: 6
Reputation:
Solved Threads: 0
Hey folks --
DaniWeb has helped me a lot with various other programming projects, but I've run into a snag that I just can't get myself to understand. Hooray for vague compiler errors. Hopefully someone here can show me the error of my ways.
I'm making a little game, based on an external map file. You can traverse from room to room using various directional commands (i.e. north south east west). The "maps.c." file contains eight lines for each room, in this format:
room number
room name
room description
number of connections to other rooms (max: 2)
direction 1
connection1
direction 2
connection 2
In my program, I need to declare an array of structures, one structure for each room, each structure having a place for each piece of information about the room.
Then, I need a function to read the information for a particular room, print that information, and then wait for the user to input where to go next.
I've re-worked this code enough times that my brain is about to explode. The following is an (obviously) incomplete version of how I'm starting things off. I suspect some gross conceptual error, but I can't figure it out. At this point all I want to do is get the program to get the room name and room number, and print 'em.
Thanks a lot for any help.
DaniWeb has helped me a lot with various other programming projects, but I've run into a snag that I just can't get myself to understand. Hooray for vague compiler errors. Hopefully someone here can show me the error of my ways.
I'm making a little game, based on an external map file. You can traverse from room to room using various directional commands (i.e. north south east west). The "maps.c." file contains eight lines for each room, in this format:
room number
room name
room description
number of connections to other rooms (max: 2)
direction 1
connection1
direction 2
connection 2
In my program, I need to declare an array of structures, one structure for each room, each structure having a place for each piece of information about the room.
Then, I need a function to read the information for a particular room, print that information, and then wait for the user to input where to go next.
I've re-worked this code enough times that my brain is about to explode. The following is an (obviously) incomplete version of how I'm starting things off. I suspect some gross conceptual error, but I can't figure it out. At this point all I want to do is get the program to get the room name and room number, and print 'em.
Thanks a lot for any help.
C Syntax (Toggle Plain Text)
#include<stdio.h> #include<stdlib.h> #include<string.h> void readRoom (rooms); int main (void) { typedef struct { int room_num; char room_name[80]; char desc[1000]; int num_links; char direction1[6]; int dest1; char direction2[6]; int dest2; } roomStruct; roomStruct rooms[100]; readRoom(rooms); return 0; } void readRoom (rooms[]) { FILE *fin; int i = 0; fin = fopen("maps.c", "r"); fscanf(fin, "%d", &rooms[i].room_num); fgets(rooms[i].room_name, 80, fin); printf("Room number: %d ... Room name: %s\n", rooms[i].room_num, rooms[i].room_name); return; }
A quick re-org to help:
Hope this helps.
C Syntax (Toggle Plain Text)
#include<stdio.h> #include<stdlib.h> #include<string.h> // typedefs don't belong inside functions (not usually, anyway...) typedef struct { int room_num; char room_name[80]; char desc[1000]; int num_links; char direction1[6]; int dest1; char direction2[6]; int dest2; } roomStruct; // Functions need complete types void readRoom ( roomStruct rooms[] ); // int main() doesn't take any void arguments. int main () { roomStruct rooms[100]; readRoom(rooms); return 0; } // And the type must be the same as the prototype's void readRoom ( roomStruct rooms[] ) { FILE *fin; int i = 0; fin = fopen("maps.c", "r"); fscanf(fin, "%d", &rooms[i].room_num); fgets(rooms[i].room_name, 80, fin); printf("Room number: %d ... Room name: %s\n", rooms[i].room_num, rooms[i].room_name); // no need to "return" from a void function }
Hope this helps.
Last edited by Duoas; Nov 8th, 2007 at 11:24 pm.
•
•
Join Date: Nov 2007
Posts: 6
Reputation:
Solved Threads: 0
Thanks for the re-organization...I should probably learn to comment a little more.
I tried the code you gave me, and it compiled wonderfully! Yay.
Then I ran it. It read in the number correctly and displayed it. The name, however, didn't come up at all.
The output was:
Room number: 0 ... Room name:
What the heck is going on? Is fgets() not reading the next line in the map.c file?
I tried the code you gave me, and it compiled wonderfully! Yay.
Then I ran it. It read in the number correctly and displayed it. The name, however, didn't come up at all.
The output was:
Room number: 0 ... Room name:
What the heck is going on? Is fgets() not reading the next line in the map.c file?
•
•
•
•
I've re-worked this code enough times that my brain is about to explode. The following is an (obviously) incomplete version of how I'm starting things off. I suspect some gross conceptual error, but I can't figure it out. At this point all I want to do is get the program to get the room name and room number, and print 'em.
Kinda like "hey Mr. Mechanic, I want my car to start when I turn the key. Fix it.." What's likely to be his first question?
Also, knowing what's being read might be important...
Last edited by WaltP; Nov 8th, 2007 at 11:53 pm.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Nov 2007
Posts: 6
Reputation:
Solved Threads: 0
Sorry; I'm a bit of a coding forum noobie. I'll be more specific.
// maps.c //
The idea is that you start in room 0. You read the description. If you type north, south, or east, it leaves you in room 0, but if you type west, it takes you to room 1, displays the description, and awaits input again. Zork flashbacks, I know.
The "none" is just the second link specifier if there is not a second room connection, so anything but the correct direction will just leave you in the same room.
The re-organized code Duoas gave me pleased the compiler, but didn't correctly print rooms[i].room_name. It didn't print anything, in fact.
Thanks for the help guys; I got really excited about this project and it was kind of a buzzkill to run into problems
// maps.c //
C Syntax (Toggle Plain Text)
0 Logan and Eliseo's Room You are in Logan and Eliseo's room. It's full of people and it's really really loud. 1 west 1 none 0 1 Jake and Garrett's Room You are in Jake and Garrett's room. There is a big fancy TV, and Garrett and his buddies are playing Splinter Cell. 2 east 0 west 2 2 Will and Ryan's Room You are in Will and Ryan's room. There is a piano, and there's junk strewn all over the place. 1 east 1 none 2
The idea is that you start in room 0. You read the description. If you type north, south, or east, it leaves you in room 0, but if you type west, it takes you to room 1, displays the description, and awaits input again. Zork flashbacks, I know.
The "none" is just the second link specifier if there is not a second room connection, so anything but the correct direction will just leave you in the same room.
The re-organized code Duoas gave me pleased the compiler, but didn't correctly print rooms[i].room_name. It didn't print anything, in fact.
Thanks for the help guys; I got really excited about this project and it was kind of a buzzkill to run into problems
C Syntax (Toggle Plain Text)
void readRoom (rooms[]) { FILE *fin; int i = 0; fin = fopen("maps.c", "r"); if ( fin == NULL ) { /* some kind of error here */ } while ( fscanf(fin, "%s%d", rooms[i].room_name,&rooms[i].room_num) > 1 ) { printf("Room number: %d ... Room name: %s\n", rooms[i].room_num, rooms[i].room_name); } return; }
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
I haven't played with scanf() in a long while, but what is probably happening is that you are reading the room number, but not the newline. Thereafter, you scan for a room name and get the newline immediately, which terminates the string.
Try getting the room number like this instead:
This gets an int, skips spaces and tabs (if any) and reads a newline, leaving the fp at the beginning of the line containing the room name. Then, when you read the room name it works.
I hope this is correct.
[EDIT] Aia, that completely ignores his input format requirements.
Try getting the room number like this instead:
fscanf( fin, "%d%*[ \t]\n", &rooms[i].room_number );This gets an int, skips spaces and tabs (if any) and reads a newline, leaving the fp at the beginning of the line containing the room name. Then, when you read the room name it works.
I hope this is correct.
[EDIT] Aia, that completely ignores his input format requirements.
Last edited by Duoas; Nov 9th, 2007 at 12:12 am.
•
•
Join Date: Nov 2007
Posts: 6
Reputation:
Solved Threads: 0
Okay, thanks.
This code is working 3/4 of the way:
But it only prints out "Logan" as rooms[i].room_name
I tried fscanf() again, this time putting "%s" into rooms[i].desc, and then printing all three, and that prints
Room number: 0 ... Room name: Logan ... Room desc: and
So it's only getting the first word. Alas, I need the whole line.
Would fgets() be better?
This code is working 3/4 of the way:
c Syntax (Toggle Plain Text)
void readRoom (roomStruct rooms[]) { FILE *fin; int i = 0; fin = fopen("map.c", "r"); fscanf(fin, "%d*[ \t]\n", &rooms[i].room_num); fscanf(fin, "%s", &rooms[i].room_name);
But it only prints out "Logan" as rooms[i].room_name
I tried fscanf() again, this time putting "%s" into rooms[i].desc, and then printing all three, and that prints
Room number: 0 ... Room name: Logan ... Room desc: and
So it's only getting the first word. Alas, I need the whole line.
Would fgets() be better?
Last edited by deadswitch; Nov 9th, 2007 at 12:30 am.
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
![]() |
Similar Threads
- Calling external files (C++)
- Problems Linking C++ Files (C++)
- Can't open any xml file (PHP)
- Help: need feedback on my Java assignment about thread sleep. It's already coded. (Java)
- reading external files into java (Java)
- Using data i read from *.* files (C++)
- unresolved external symbol when using a hashtable class (C++)
- Zend PHP Certification (PHP)
Other Threads in the C Forum
- Previous Thread: Controlling wheter postfix expression is valid
- Next Thread: quick sort
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays asterisks binarysearch calculate changingto char character cm command copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic execv feet fgets file fork forloop framework function functions givemetehcodez grade graphics gtkwinlinux hacking histogram homework inches include incrementoperators input intmain() iso kernel keyboard km lazy license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft mqqueue number oddnumber odf opendocumentformat opensource overwrite owf pdf performance pointer posix problem probleminc process program programming radix recursion recv recvblocked research reversing scanf scripting segmentationfault sequential socket spoonfeeding standard string student systemcall testing threads turboc unix user variable wab whythiscodecausesegmentationfault windowsapi






