don't understand basic file I/O

Started by
18 comments, last by Zahlman 15 years, 11 months ago
I thought '1' was 49...
Advertisement
What happens when you need more than 10 tile types?
Quote:Original post by Oluseyi
What happens when you need more than 10 tile types?


Is there a way to read a file with just numbers, all separated by a space?

like:

1 4 4 4 5 10 11 2 2
0 5 6 2 2 10 1 1 1

etc...

[opinion]
The way you are doing this is very dangerous. You should never assume that your data is going to be valid and in the format that you are expecting. Also your way does not allow for different sized maps.

So how I would suggest doing it is, for each line of the map have it terminated with a character (/0) would be good for this. So you read each line until you reach the terminating character. You continue to read lines until you reach the EOF (end of file).

A couple simple while loops and you are set. Since this looks like standard C it means we don't have access to std::vector to store the maps. So you either need to have a maximum size allowed, or you a linked list for storing the map info. In this case I would go with a linked list.
[/opinion]

theTroll
So how would I insert the /0 thingy in my text file?

I think I kinda get what you're saying but it's a little vague. Couple details would be very much appreciated.

anyone?
Any hex editor would let you do it. The '/0' is just 0x00 in hex.

theTroll
ok ty

one last question

say I wanted to read the file number by number instead of one character at a time
and say each number is separated by a space, like so:

1 1 0 9 2 10 15 1 2 3 3
1 1 0 9 2 10 15 1 2 3 3
7 1 0 9 2 10 15 1 2 3 3
1 5 0 9 2 10 15 1 2 3 3

how would i read that into a 2 dimensional array?
like in semi psedoc-code
i dont really understand file i/o
If you know how many Numbers there are per "line" of your file, then you can use fscanf (or preferably fscanf_s if available).
http://www.cplusplus.com/reference/clibrary/cstdio/fscanf.html

Basically you'd have something like this:
#include <stdio.h>int number;int row = 0;// lets assume myFileHandle is a working handle to a file// this while loop will run until the end of file has been reachedwhile(!feof(myFileHandle)) {  for(int column=0; column<11; ++column) { // 11 numbers per line    fscanf(myFileHandle,"%d",&number);    // at this point, <number> will have the number at the <row>th row and <column>th column.    // all you need to do now, is put those values inside an array.    // Unfortunately I have no clue as to what would be an appropriate way to do that in standard C - possibly something like a linked list using structs.    // In C++ you'd simply use std::vector ...  }  ++row;}
0) Like Oluseyi said right at the beginning: Handle each file separately. Notice that, as it stands, you only check for successful opening of the first file. That doesn't imply anything about any of the others. If you do it all in one loop, you won't risk running out of file descriptors, and you also won't have to keep around that ugly array of them.

1) FFS. It's '\0', not '/0', you guys. Also, it's a bad idea to expect the user to put weird characters like that into a text file. Return characters (i.e., end-of-line) work perfectly well as delimiters.

2) Don't write comments to say what things do, when it's already perfectly obvious what they do. The person reading your code should already be expected to know what a for-loop does, and is expecting a function named "LoadMaps" to load maps. When you comment these things, it makes the reader pay attention to things that are not important. This is bad, because it plants the seed of paranoia.

Comments are used to explain how you are doing what you are doing.

BTW: The most common reader of your code, by far, is you. Don't annoy yourself.

3) Try cutting things up into helper functions to make the process more clear.

4) There is no need to assign a bit of text to a variable just to pass it to a function call. You can pass any expression as a parameter to a function (unless you are e.g. passing by non-const reference).

void LoadMap(const char* name, int* destination){	FILE* map_data = fopen(name, "r");			if (!map_data)	{		WriteToLogFile("Error: Unable to open map file.");	}		for (int m = 0; m < WORLDSIZE_X; m++)	{		for (int n = 0; n < WORLDSIZE_Y; n++)		{			fscanf(map_data, "%d", &destination[n * WORLDSIZE_X + m]);		}	}	fclose(map_data);}	void LoadMaps(){	char map_id[16]; // let's be nice and generous ;P	// BTW, what is the leading 0 for in your file names? Did you want	// all the numbers to be a specific "width", zero-padded on the left?	// In that case, you want something like "map%02d.txt".	for (int h = 0; h < MAXNUMLEVELS; ++h)	{		sprintf(map_id, "map0%d.txt", h);		LoadMap(map_id, g_levels[h].map);	}	}

This topic is closed to new replies.

Advertisement