Creating file reader/level loader C++

Started by
6 comments, last by unicoder 8 years, 9 months ago

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.

Advertisement

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).

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

Eric Richards

SlimDX tutorials - http://www.richardssoftware.net/

Twitter - @EricRichards22

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.

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);

}

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

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.

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!

This topic is closed to new replies.

Advertisement