This topic is 1092 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm interested in making a level maker program. I need to be able to read specific data from that file and convert it to strings and ints.

file.txt

// string sprite, int x, int y, int z
tree1 40 30 1
tree2 50 35 2


I need to convert each string to proper variable using a space(I could change it to anything at this point).

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
string line;
ifstream myfile ("file.txt");
int linnum=0;
char c;
while (myfile.good()) {
c = myfile.get();
if (c=='\n')linnum++;
}
myfile.close();
cout<<linnum<<endl;
return 0;
}



So I want it in an array. So I made this to get the number of lines for the array.

I need an array that can hold a sting and three ints.

##### Share on other sites

How flexible do you need this? If you have a fixed case of an object class like this:

class Object
{
std::string sprite;
int x, y, z;
}


Then you can just read in the values directly:

std::vector<Object*> vObjects;
while(myfile.good())
{
Object pObject = new Object;

myfile >> pObject->name;
myfile >> pObject->x;
myfile >> pObject->y;
myfile >> pObject->z;

vObjects.push_back(pObject);
}


The >> operator will read the next value until a delim character is hit (space/newline etc...), and convert it to the proper type. So if you have a static file format where ie. there is always between 0-X sprites with the same format, then this is probably the easiest way. If you really need to store the read-out information in an intermediat array, I would just store string-per-string (>> can also store any value as string) in an std::vector<string> and convert the values on  use to their proper types. If there is any storage/performance overhead that you don't want, you can look into variant (boost::any), but there also is a certain overhead, mainly in complexity (you need to know which type the propery you are reading out is, so you most likely already have the target structure at hand and might as well just create it directly).

Edited by Juliean

##### Share on other sites

If your assets are fairly regular, I might try using JSON, rather than a custom format

##### Share on other sites

At this time I want something simple. Juliean gave something I think will work. Once I get this working I will consider something like JSON.

##### Share on other sites

Code should look like this

class Object
{

public:
std::string sprite;
int x, y, z;
};

std::vector<Object*> vObjects;
while(myfile.good())
{
Object* pObject = new Object;

myfile
>> pObject->name;
myfile >> pObject->x;
myfile >> pObject->y;
myfile >> pObject->z;

vObjects
.push_back(pObject);

}

Edited by workaround

##### Share on other sites
Hi
Google protocols buffers could help here.
They will give you serialized data

But there not good for large file.

Which you can work around by making a structured file from them.

If you give each buffer a ID of some sort and make it the first field of the buffer then the objets data you could serialize each buffer then write the serialized string to file then to retrieve you scan the file fields for the type and load it. Works well

##### Share on other sites

This is why I prefer binary files over human-readable. You read in the header, extract the data offsets and then either read in the data, offset by offset, directly into your data structures or read the entire data block from the file into a buffer and parse it there. Sure, you lose the readability of a human-readable format, but your editor should make that unnecessary anyway.

##### Share on other sites

At this time I want something simple. Juliean gave something I think will work. Once I get this working I will consider something like JSON.

I agree, at this scale and you know exactly what is in your text files, because you are generating them manually, Juliean has a great solution. But I would also leave some to-do comments for exploring JSON :) good luck!

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631782
• Total Posts
3002324
×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!