Ah, you're casting the return value of fgetc() to a char and then testing that against EOF. If the comparison fails then you won't break the loop on EOF and you end up with an overrun. Accept the value of fgetc() as its native type and then test it against EOF, then write to the buffer based on the result.
Just out of curiosity, why not just use fread() here?
Remember to close your FILE pointer when you're done or you'll leak handles.
When you acquire a resource (opening a file or allocating memory, etc) it's important to test for success before continuing and it's important to remember to release the resource when you're done with it.
Oh, and:
*p = '\0';
Could just be:
*p = 0;
But if you're going to write an extra byte like that then you need to increase the allocation size by 1.
Ah, I was so busy looking for the bug I didn't realize - when you do things like this you should use the properties of the destination (length or boundary positions) to control the loop rather than the properties of the source.
Logic error in my code i think
There is so much wrong in these 3 functions.
But one of the biggest issues, besides all the other ones the previous posters mentioned is in your load_map function:
map_file *tehMap;
[some weird code here]
return tehMap;
and thats it? you do nothing with this? no allocation, no assignment of your map data?
There are so many more ways to read your map data, many more EASIER ways. You can read your 100x100 grid all at once, the just use a memcpy or whatever. Why parse it in such a strange way?
You could simply read the whole map_file struct all at once if you had saved it properly.
Your comment is nice:
"// i plan on using the function to assign the values from the file to the struct"
but...you never do that!!
Please, do yourself a favour and THINK STEP BY STEP on what you do. To me, this looks like you copy and pasted fragments of map loading code you found on google into your program.
No offence mate, but please start with a smaller map! Use a grid of 5x5 tiles for example where you easily can check all contents of the array in the DEBUGGER. If your loading code works, it doesnt matter if you're using a 5x5 or 1000x1000 map.
Good luck,
- Christoph -
Acid-Chris
i do plan on adding the data to the struct and i don't take any offence im here to learn off my mistakes
i write 90% of the code my self, i do copy some minor functions from the internet i find and think will work for the purpose i need them to do
right now im working on the struct, im trying to load the map data in to the int array now but the program freezes now
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <string.h>
#include <sys/stat.h>
typedef struct {
char *map_title;
int width,height,aot,tiles[1000];
} map_file;
static int fsize(FILE *fp){
int prev=ftell(fp);
fseek(fp, 0L, SEEK_END);
int sz=ftell(fp);
fseek(fp,prev,SEEK_SET); //go back to where we were
return sz;
}
static char *readFile(char *fileName) {
FILE *file;
char *code;
int sizeFile;
file = fopen(fileName, "r");
sizeFile = fsize(file);
printf("Size of file : %i \n", sizeFile);
code = malloc(sizeFile * sizeof(char));
printf("Test for after malloc\n");
fread(code, 1, sizeFile, file);
fclose(file);
return code;
}
// load a map file in to the struct
map_file *load_map(char *location) {
printf("Loading map : %s \n", location);
map_file *tehMap;
tehMap = malloc(3*sizeof(map_file*));
char *dataToken,*map_data;
int count = 1,tile_count = 0;
map_data = readFile(location);
dataToken = strtok(map_data,",");
printf("STARTED TOKEN OUTPUT\n");
while(dataToken != NULL) {
if(count == 1) {
strcpy(tehMap->map_title,dataToken);
} else if(count == 2) {
tehMap->width = atoi(dataToken);
}else if(count == 3) {
tehMap->height = atoi(dataToken);
}else if(count > 3) {
tehMap->tiles[tile_count] = atoi(dataToken);
tile_count += 1;
}
printf("TOKEN : %s \n",dataToken);
dataToken = strtok(NULL,",");
count++;
}
tehMap->aot = count - 3;
printf("Amount of map inputs : %i \n", count);
printf("FINISHED TOKEN OUTPUT\n");
printf("tehMap struct -> Map title : %s \n", tehMap->map_title);
printf("tehMap struct -> Map width : %i px \n", tehMap->width);
printf("tehMap struct -> Map height : %i px \n", tehMap->height);
printf("tehMap struct -> Map aot : %i px \n", tehMap->aot);
/* for displaying the map data from the file to the console
for(int s=0; s < tehMap->aot; s++) {
printf("%i,",tehMap->tiles[s]);
}
*/
return tehMap;
}
int main()
{
printf("Map engine testing ground for logic\n\n");
map_file *theMap;
theMap = malloc(3*sizeof(map_file*));
theMap = load_map("map.map");
return 0;
}
any advice would be nice or modifications, thanks for the input
A couple of points:
Consider building your program in smaller chunks. First write a function that will print the contents of a file to the screen. Test this function, ensure it works and handles any edge cases (file not found) gracefully.
Next write a function to parse the file format, but continue to print it to the screen. Test it with a minimal correct file. Harden the function against unexpected input (e.g. an empty file, not enough fields, non-numeric data in numeric field, etc, etc). Test it with more advanced files.
Next, create the structure to represent the data in memory. Test this by filling the structure in code, and print it to the screen.
As the final step, try to fill the structure from the data in the file.
rip-off i changed the code around a lot but it freezes when i try to store the data from the char to the int tiles[1000] in the struct
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <string.h>
#include <sys/stat.h>
typedef struct {
char *map_title;
int width,height,aot,tiles[1000];
} map_file;
static int fsize(FILE *fp){
int prev=ftell(fp);
fseek(fp, 0L, SEEK_END);
int sz=ftell(fp);
fseek(fp,prev,SEEK_SET); //go back to where we were
return sz;
}
static char *readFile(char *fileName) {
FILE *file;
char *code;
int sizeFile;
file = fopen(fileName, "r");
sizeFile = fsize(file);
printf("Size of file : %i \n", sizeFile);
code = malloc(sizeFile * sizeof(char));
printf("Test for after malloc\n");
fread(code, 1, sizeFile, file);
fclose(file);
return code;
}
// load a map file in to the struct
map_file *load_map(char *location) {
printf("Loading map : %s \n", location);
map_file *tehMap;
tehMap = malloc(sizeof(map_file*));
char *dataToken,*map_data;
int count = 1,tile_count = 1;
map_data = readFile(location);
dataToken = strtok(map_data,",");
printf("STARTED TOKEN OUTPUT\n");
while(dataToken != NULL) {
if(count == 1) {
tehMap->map_title = dataToken;
} else if(count == 2) {
tehMap->width = atoi(dataToken);
}else if(count == 3) {
tehMap->height = atoi(dataToken);
}else if(count > 3) {
tehMap->tiles[tile_count] = atoi(dataToken); // <------------- where it freezes
tile_count += 1;
}
printf("TOKEN : %s \n",dataToken);
dataToken = strtok(NULL,",");
count++;
}
tehMap->aot = tile_count + 1;
printf("Amount of map inputs : %i \n", count);
printf("FINISHED TOKEN OUTPUT\n\n");
printf("tehMap struct -> Map title : %s \n", tehMap->map_title);
printf("tehMap struct -> Map width : %i px \n", tehMap->width);
printf("tehMap struct -> Map height : %i px \n", tehMap->height);
printf("tehMap struct -> Map amount of tile : %i \n", tehMap->aot);
return tehMap;
}
int main() {
printf("Map engine testing ground for logic\n\n");
map_file *theMap = NULL;
theMap = load_map("map.map");
return 0;
}
You haven't really addressed many of the issues I highlighted earlier.
You still are not allocating the map structure correctly. You are only allocating the sizeof a pointer. Alternatively, avoid dynamic allocation.
One option is to declare the map on the stack, and return it by value:
map_file load_map(char *location) {
map_file result;
// Load the data
return result;
}
int main() {
map_file map = load_map("map.map");
// Use the map
}
A common C idiom is to get the caller to allocate the object, and pass a pointer to it for it to be filled in. This is particularly nice because it lends itself to error reporting via the return value:
int load_map(map_file *result, char *location) {
// Try load some data
if(someError) {
return 0;
}
// Try load more data
return 1;
}
int main() {
map_file map;
if(load_map(&map, "map.map")) {
// Use the map
} else {
// Alert the user or otherwise handle the error gracefully.
}
}
I've noticed two other errors in your code, you fail to handle the case where malloc() returns NULL, and you appear to start your tile indexing at "1". Array indices start at "0" in C.
Take it easy, Chris. People come here to learn. There's a lot to take in when you're new. He'll catch up over time.