File Input

I've got a simple reader here to read out some data from a text file that stores a key/tag for an image and the directory where to load the image from.

Now the problem is I dont like how my text file looks - So i want to change it so my text file can technically have tags in it. This allowing me to make it so the text file is readable.

This is currently how I have it working. Not my actual code though I just quickly wrote this. (Sorry if theres an error but you get the idea).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
std::string fileName = "file.txt";
FILE* file = fopen(fileName.c_str(), "r");

if(file)
{
	while(!feof(file))
	{
		char name[128];
		char directory[128];
		fscanf(file, "%s\n%s\n", &name, &directory);
		//Dump those values into a container of structs etc
	}
}
fclose(file);
[file.txt]
Jumping
Data\Textures\jumping.png
Bouncing
Data\Textures\bouncing.png


I want to change it so it can load in a text file with tags like <IMAGE><NAME><DIRECTORY>. Then once it finds an <IMAGE> tag it gets all the data in from underneath it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
std::string fileName = "file.txt";
FILE* file = fopen(fileName.c_str(), "r");

if(file)
{
	while(!feof(file))
	{
		// Somehow do a search here for a keyword in the file <IMAGE>
		// if its found get the <NAME> and <DIRECTORY>.
		char name[128];
		char directory[128];
		fscanf(file, "%s\n%s\n", &name, &directory);
		// Dump those values into a container of structs etc
		// Then move onto the next <IMAGE> tag
	}
}
fclose(file);
[file.txt]
<IMAGE>
	<NAME>Jumping
	<DIRECTORY>Data\Textures\jumping.png
<IMAGE>
	[NAME]Bouncing
	<DIRECTORY>Data\Textures\bouncing.png


Hopefully this makes sense.

Thanks :)
No end tags (like </IMAGE>)?

You need to parse the file. Without end tags or some convention denoting end, you can't tell if you've encountered an conrrectly or incorrectly formatted file.
So how does one go about parsing the file then? - I just threw up an idea and I'm trying to see the best way I could do this. Without using another library to help out.

Thanks for the tip though - I'll take a look at parsing now. But if anyone has a couple of line way to do this - do tell :)

Thanks.
In your example, you have a level 1 tag, <IMAGE>. Then within that, you have two tags <NAME>, <DIRECTORY>.

Your code needs to read tags and recognise IMAGE. The IMAGE handler should expect to find NAME and DIRECTORY and fill in some record like struct Image { std::string name, directory };
Thats fine - already have that - but I don't know how to actually scan for a tag and the end of the tag. Then get the data between the two. Thats the issue I have.
Does anyone know what function or something I can use to search for a tag/token of some sort?
Topic archived. No new replies allowed.