Reading data from a text file

I have to read data from a text file and sort it but I have no idea how to read the data. It was presented in geoJSON format. This is what the first entry looks like:
{
"count": 269290,
"items": [
{
"language": "en",
"created": "2013-12-02T22:01:37.000+01:00",
"geometry": {
"type": "Point",
"coordinates": [
9.24072841,
45.47024867
]
},
"timestamp": 1386018097,
"municipality": {
"name": "Milano",
"acheneID": "http://dandelion.eu/resource/2bf72cfc3857f470be5457dc4aa77e578889e32d"
},
"entities": [
"http://it.dbpedia.org/resource/Germania"
]

I was thinking about searching for specific words and then placing the data after the searched word into an array if that's even possible. Could I have used:

ifstream file("data.txt")
while(file>>parameterOne>>parameterTwo...etc){}

But "count" and "items" have to be excluded and they only appear at the start. Also would characters like "[" etc cause the method above to not work? Could someone guide me in the right direction? I'm a beginner and I'd like to figure out the code on my own.

Yes, its possible. This... I do a lot of this. I hate xml :P

I take the direct route here.

get the filesize. you can do this by opening the file and doing a .seek(eof) and .tell().

then read the whole thing into a char *..

char * wholefile = new char[filesize];
..
file.read(wholefile, filesize);

now you can happily look through it for stuff.

char * tmp;

tmp = strstr("lookforme");
if(tmp) //this is critical. if its not there, youll have a null pointer.
tmp[strlen("lookforme")] <- this is your data. You can figure out how much to grab, until the next space, maybe, or end of line, or whatever...


You can do all this stuff with strings, vectors, and the whole C++ bit, but for this specific task, I find the C functions to be more efficient and easier to write the code for.
Last edited on
Okay thanks a lot I'll give it a try.
Topic archived. No new replies allowed.