Syntax Question for Tile-Based Engine
Yeah, I agree. I think my biggest problems are with the file i/o part. I have a book on file i/o, and seems that what I''m doing is okay. I''m not sure what I''m doing wrong.
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi
Muzzafarath, I think your code might be slightly off:
FILE *fp = NULL;
fp = fopen("map10-9.map", "rt");
if(fp == NULL) exit(1); //File didn''t open correctly
for (int j=0; j<=21; j++) { fscanf(fp, "%s", world [j]; } //Read one line from the text file
Which causes a problem in the reading in...
the reason being that world[j] is a ptr, but it is initially pointing to a constant string...
what would be safer is:
char world[21][31];
instead of
char *world[21] = { "34343...", ..., "....334" };
or
char *world[21];
since the latter two will pass in a read-only location of memory.
fscanf is writing the string to the buffer pointed to by
world, but if there is no allocated buffer than it will be a run-time error.
[if this is not clear let me know and I''ll repost to clarify... I use C++ a lot and have forgotten most of this non-stream, FILE stuff...]
(also, if you guys are so interested in an I/O article, post to the ''request for posts'' forum...)
Good luck wit it...
Dark Lord Pi
FILE *fp = NULL;
fp = fopen("map10-9.map", "rt");
if(fp == NULL) exit(1); //File didn''t open correctly
for (int j=0; j<=21; j++) { fscanf(fp, "%s", world [j]; } //Read one line from the text file
Which causes a problem in the reading in...
the reason being that world[j] is a ptr, but it is initially pointing to a constant string...
what would be safer is:
char world[21][31];
instead of
char *world[21] = { "34343...", ..., "....334" };
or
char *world[21];
since the latter two will pass in a read-only location of memory.
fscanf is writing the string to the buffer pointed to by
world, but if there is no allocated buffer than it will be a run-time error.
[if this is not clear let me know and I''ll repost to clarify... I use C++ a lot and have forgotten most of this non-stream, FILE stuff...]
(also, if you guys are so interested in an I/O article, post to the ''request for posts'' forum...)
Good luck wit it...
Dark Lord Pi
Dark Lord Pi
Download this and see for yourselves that my code works I''ve even compiled it for you (with DJGPP it it matters). Sometimes it doesn''t work if you doubleclick on it from Windows, so you have to start it from the DOS prompt.
But it DOES work (at least for me).
/. Muzzafarath
But it DOES work (at least for me).
/. Muzzafarath
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
I downloaded the code, and again, Muzzafarath, it made sense. I tried to do the same thing in my game and it does not work. I am positive that it is crashing at the point of opening the file. I remarked out the part where it reads in world[j] with the for loop, and left the opening of the file and the hard-coded world declaration
(where I set
world[0]=11111"
world[1]=11111"
and so on)
It just bombed out when it got to opening. Are there any special things to consider when doing file i/o in VC++? I imagine there is not. I am giving it the exact path to map.txt, and still to no avail.
Thanks again for all of your help
(where I set
world[0]=11111"
world[1]=11111"
and so on)
It just bombed out when it got to opening. Are there any special things to consider when doing file i/o in VC++? I imagine there is not. I am giving it the exact path to map.txt, and still to no avail.
Thanks again for all of your help
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi
Very odd. Could you post the code you're using?
When you declare *world[] are you doing it the way you showed in the first post?
/. Muzzafarath
Edited by - Muzzafarath on 4/18/00 11:12:41 AM
When you declare *world[] are you doing it the way you showed in the first post?
/. Muzzafarath
Edited by - Muzzafarath on 4/18/00 11:12:41 AM
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
yes I declare world like so...
char *world[21]=
{
"111111111111111111111111111111",
"100000000000000000000000000001",
"100002222220000000000000077701",
"100002222223333333333000077701",
"100002222227777777773000070001",
"100002222227777777773000070001",
"100000000377777777773000070001",
"107777700377777777773000070001",
"177777770377777777773000770001",
"107777700377777777773007700001",
"100777770377777777773777000001",
"100000707377777777773000000001",
"100007777377777777773000000001",
"100000000302222777773000000001",
"100000000332222777773000000001",
"100000000002222333333000000001",
"100000666666666666666666600001",
"100000800000000000000000800001",
"100000800000000000000000800001",
"100000000000000000000000000001",
"111111111111111111111111111111",
};
I have 2 variables called place_x and place_y that tracks the current map...they both start off with a value of 10.
If you move off the map up then place_y is decreased by one and is then a 9.
So, I then call this function...
void Change_Map(void)
{
if (place_x==10 && place_y==9)
{
FILE *fp = NULL;
fp = fopen("c:\ddrive\c++\newc\map.txt", "rt");
if(fp == NULL) exit(1);
for (int j=0; j<=21; j++)
{ fscanf(fp, "%s", world [j]); }
fclose(fp);
} // end if
} // end Change_Map()
//////////////////
As I said before, I've even tried remarking the whole for loop, and using the hard coded version of change the world variable (and leaving the opening of that file). It seems that it just does not want to open that file
Just out of curiosity, what does the "rt" do in fscanf?
My, I have one hell of a post going on here on such a simple thing
Edited by - Nazrix on 4/18/00 12:33:43 PM
char *world[21]=
{
"111111111111111111111111111111",
"100000000000000000000000000001",
"100002222220000000000000077701",
"100002222223333333333000077701",
"100002222227777777773000070001",
"100002222227777777773000070001",
"100000000377777777773000070001",
"107777700377777777773000070001",
"177777770377777777773000770001",
"107777700377777777773007700001",
"100777770377777777773777000001",
"100000707377777777773000000001",
"100007777377777777773000000001",
"100000000302222777773000000001",
"100000000332222777773000000001",
"100000000002222333333000000001",
"100000666666666666666666600001",
"100000800000000000000000800001",
"100000800000000000000000800001",
"100000000000000000000000000001",
"111111111111111111111111111111",
};
I have 2 variables called place_x and place_y that tracks the current map...they both start off with a value of 10.
If you move off the map up then place_y is decreased by one and is then a 9.
So, I then call this function...
void Change_Map(void)
{
if (place_x==10 && place_y==9)
{
FILE *fp = NULL;
fp = fopen("c:\ddrive\c++\newc\map.txt", "rt");
if(fp == NULL) exit(1);
for (int j=0; j<=21; j++)
{ fscanf(fp, "%s", world [j]); }
fclose(fp);
} // end if
} // end Change_Map()
//////////////////
As I said before, I've even tried remarking the whole for loop, and using the hard coded version of change the world variable (and leaving the opening of that file). It seems that it just does not want to open that file
Just out of curiosity, what does the "rt" do in fscanf?
My, I have one hell of a post going on here on such a simple thing
Edited by - Nazrix on 4/18/00 12:33:43 PM
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi
Hmmm...
Don't ask me why but I believe it's opening the file now.
I removed the direct path to the file, and just left the file in the same directory as the source code is in. Now if I have the for loop in, it bombs...If I remark the for loop it doesn't crash on me...
So, I'm doing this now:
fp = fopen("map.txt", "rt");
----------------------------------------
I think that's progress anyway
Lord Pi, the reason I was using *world[21] is basically because that's what Andre LaMothe was using in Tricks of the Windows Gurus book. Perhaps your way of doing it may be better...I'll mess around with that idea perhaps...or might it be easier if I declare world with an int?
int world[21][31];
and then perhaps read in each integer from the file into the appropriate place in the array? I am just talking off the top of my head now though.
Edited by - Nazrix on 4/18/00 1:46:41 PM
Don't ask me why but I believe it's opening the file now.
I removed the direct path to the file, and just left the file in the same directory as the source code is in. Now if I have the for loop in, it bombs...If I remark the for loop it doesn't crash on me...
So, I'm doing this now:
fp = fopen("map.txt", "rt");
----------------------------------------
I think that's progress anyway
Lord Pi, the reason I was using *world[21] is basically because that's what Andre LaMothe was using in Tricks of the Windows Gurus book. Perhaps your way of doing it may be better...I'll mess around with that idea perhaps...or might it be easier if I declare world with an int?
int world[21][31];
and then perhaps read in each integer from the file into the appropriate place in the array? I am just talking off the top of my head now though.
Edited by - Nazrix on 4/18/00 1:46:41 PM
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi
When writing absolute paths in C/C++ you have to think of that you can't just use \. You have to use \\. This is wrong:
fp = fopen("c:\ddrive\c++\newc\map.txt", "rt");
This is right:
fp = fopen("c:\\ddrive\\c++\\newc\\map.txt", "rt");
Try changing:
for (int j=0; j<=21; j++)
to:
for (int j=0; j<=20; j++)
I you use "j<=21" then that will be done 22 times, but if you use "j<=20" it will be done 21 times (I suppose this is what you want...).
There is no "rt" in fscanf(), but there is in fopen() (that was probably just a typo by you). Anyway, it stands for "read text", and describes what you want to do with the file. Here are some examples:
"rb" = read (from) binary
"wb" = write (to) binary (this will truncate the file if it exists)
"ab" = append binary (write to the end)
"rt" = read text
"wt" = write text (will truncate it)
"at" = append text
"r+b" = read or write to/from binary. file must exist.
"w+b" = read or write to/from binary. if it exists it will be truncated.
"r+t" = read or write to/from text file. file must exist.
/. Muzzafarath
Edited by - Muzzafarath on 4/18/00 2:55:36 PM
fp = fopen("c:\ddrive\c++\newc\map.txt", "rt");
This is right:
fp = fopen("c:\\ddrive\\c++\\newc\\map.txt", "rt");
Try changing:
for (int j=0; j<=21; j++)
to:
for (int j=0; j<=20; j++)
I you use "j<=21" then that will be done 22 times, but if you use "j<=20" it will be done 21 times (I suppose this is what you want...).
There is no "rt" in fscanf(), but there is in fopen() (that was probably just a typo by you). Anyway, it stands for "read text", and describes what you want to do with the file. Here are some examples:
"rb" = read (from) binary
"wb" = write (to) binary (this will truncate the file if it exists)
"ab" = append binary (write to the end)
"rt" = read text
"wt" = write text (will truncate it)
"at" = append text
"r+b" = read or write to/from binary. file must exist.
"w+b" = read or write to/from binary. if it exists it will be truncated.
"r+t" = read or write to/from text file. file must exist.
/. Muzzafarath
Edited by - Muzzafarath on 4/18/00 2:55:36 PM
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
You might want to try making your function like this:
I need to ask a really stupid question just to make sure... The vars "*world[], place_x and place_y" are global aren't they? I'm positive that VC would let you know if they weren't but you can never be too sure. Compilers can be stupid sometimes. DJGPP C++ didn't catch printf("Hello") (notice how I forgot the semicolon at the end) the first time I tried to compile a program with it.
/. Muzzafarath
Edited by - Muzzafarath on 4/18/00 3:02:18 PM
void Change_Map(void)
{
FILE *fp = NULL;
int j;
if (place_x==10 && place_y==9)
{
fp = fopen("c:\ddrive\c++\newc\map.txt", "rt");
if(fp == NULL) exit(1);
for (j=0; j<=21; j++)
{ fscanf(fp, "%s", world[j]); }
fclose(fp);
} // end if
} // end Change_Map()
I need to ask a really stupid question just to make sure... The vars "*world[], place_x and place_y" are global aren't they? I'm positive that VC would let you know if they weren't but you can never be too sure. Compilers can be stupid sometimes. DJGPP C++ didn't catch printf("Hello") (notice how I forgot the semicolon at the end) the first time I tried to compile a program with it.
/. Muzzafarath
Edited by - Muzzafarath on 4/18/00 3:02:18 PM
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
Muzzafarath: I''m sorry about the format of my last message. Your code had nothing wrong with it, I was referring to the way memory was being managed as a potential problem. (and I quoted your code since the fscanf was something that had me worried)
I never read LaMothe''s book, so who am I to say what works or not. I was just referring to having memory preallocated before reading into it, since that was something I was taught to do.
So just ignore my suggestion and I''ll go back to smoking my crack cocaine.... Sorry once again.
Dark Lord Pi
I never read LaMothe''s book, so who am I to say what works or not. I was just referring to having memory preallocated before reading into it, since that was something I was taught to do.
So just ignore my suggestion and I''ll go back to smoking my crack cocaine.... Sorry once again.
Dark Lord Pi
Dark Lord Pi
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement