PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

reading from csv

Hello, I'm trying to load data from a comma separated text file. I want to parse through the text, pull out the number values, and insert them into an array of integers. Stripped down, my code looks something like this:

Code:

ClassEFile ef;
ef.Open("map.csv", EFOF_READ);
int map[31];

for (int x = 0; x < 31; x++) {
char *c = new char[1];
short len = 0;
do {
ef.Read(c, 1);
shared->main->Log("[", false);
shared->main->Log(c, false);
shared->main->Log("]", false);
len++;
} while(!ClassEStd::StrEqual(c, ","));
ef.Seek(ef.Tell()-len);
char *next = new char[len];
ef.Read(next, len);
ClassEStd::StrReplace(app, ",", "", false);
map[x] = ClassEStd::StrToInt(ClassEStd::StrTrim(next), 10)
}


map.csv looks like this:
Code:

2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,4,5,3,6,7,8,9,7,8,9,7,8,9,7,8,
2,2,2,2,2,2,10,2,2,2,2,2,2,2,2,11,12,12,11,9,7,8,9,7,8,9,7,8,9,7,8,


When I open up my log file though, I see this:
Code:

[2««««««««þîþîþîþ][,««««««««þîþîþîþ][2««««««««þîþîþîþ][,««««««««þîþîþîþ][2««««««««þîþîþîþ][,««««««««þîþîþîþ]
[2««««««««þîþîþîþ][,««««««««þîþîþîþ][2««««««««þîþîþîþ][,««««««««þîþîþîþ][2««««««««þîþîþîþ][,««««««««þîþîþîþ]
[2««««««««þîþîþîþ][,««««««««þîþîþîþ][2««««««««þîþîþîþ][,««««««««þîþîþîþ][2««««««««þîþîþîþ][,««««««««þîþîþîþ]
[2««««««««þîþîþîþ][,««««««««þîþîþîþ][2««««««««þîþîþîþ][,««««««««þîþîþîþ][2««««««««þîþîþîþ][,««««««««þîþîþîþ]
[2««««««««þîþîþîþ][,««««««««þîþîþîþ][2««««««««þîþîþîþ][,««««««««þîþîþîþ][2««««««««þîþîþîþ][,««««««««þîþîþîþ]
[3««««««««þîþîþîþ][,««««««««þîþîþîþ][4««««««««þîþîþîþ][,««««««««þîþîþîþ][5««««««««þîþîþîþ][,««««««««þîþîþîþ]
[3««««««««þîþîþîþ][,««««««««þîþîþîþ][6««««««««þîþîþîþ][,««««««««þîþîþîþ][7««««««««þîþîþîþ][,««««««««þîþîþîþ]
[8««««««««þîþîþîþ][,««««««««þîþîþîþ][9««««««««þîþîþîþ][,««««««««þîþîþîþ][7««««««««þîþîþîþ][,««««««««þîþîþîþ]
[8««««««««þîþîþîþ][,««««««««þîþîþîþ][9««««««««þîþîþîþ][,««««««««þîþîþîþ][7««««««««þîþîþîþ][,««««««««þîþîþîþ]
[8««««««««þîþîþîþ][,««««««««þîþîþîþ]
[9««««««««þîþîþîþ][,««««««««þîþîþîþ][7««««««««þîþîþîþ][,««««««««þîþîþîþ][8««««««««þîþîþîþ][,««««««««þîþîþîþ][
««««««««þîþîþîþ][
««««««««þîþîþîþ][2««««««««þîþîþîþ][,««««««««þîþîþîþ][2««««««««þîþîþîþ][,««««««««þîþîþîþ][2««««««««þîþîþîþ]
[,««««««««þîþîþîþ][2««««««««þîþîþîþ]
[,««««««««þîþîþîþ][2««««««««þîþîþîþ][,««««««««þîþîþîþ][2««««««««þîþîþîþ][,««««««««þîþîþîþ][1««««««««þîþîþîþ]
[0««««««««þîþîþîþ][,««««««««þîþîþîþ][2««««««««þîþîþîþ][,««««««««þîþîþîþ][2««««««««þîþîþîþ][,««««««««þîþîþîþ]
[2««««««««þîþîþîþ][,««««««««þîþîþîþ][2««««««««þîþîþîþ][,««««««««þîþîþîþ][2««««««««þîþîþîþ][,««««««««þîþîþîþ]
[2««««««««þîþîþîþ][,««««««««þîþîþîþ][2««««««««þîþîþîþ][,««««««««þîþîþîþ][2««««««««þîþîþîþ][,««««««««þîþîþîþ]
[1««««««««þîþîþîþ][1««««««««þîþîþîþ][,««««««««þîþîþîþ][1««««««««þîþîþîþ][2««««««««þîþîþîþ][,««««««««þîþîþîþ]
[1««««««««þîþîþîþ][2««««««««þîþîþîþ][,««««««««þîþîþîþ][1««««««««þîþîþîþ][1««««««««þîþîþîþ][,««««««««þîþîþîþ]
[9««««««««þîþîþîþ][,««««««««þîþîþîþ][7««««««««þîþîþîþ][,««««««««þîþîþîþ][8««««««««þîþîþîþ][,««««««««þîþîþîþ]
[9««««««««þîþîþîþ][,««««««««þîþîþîþ][7««««««««þîþîþîþ][,««««««««þîþîþîþ][8««««««««þîþîþîþ][,««««««««þîþîþîþ]


I'm thinking this has something to do with me using my c variable incorrectly. I'm trying to read one character at a time, but since ClassEFile::Read() doesn't take a regular char as a parameter, I made c a character sequence with a length of one. Is there a better way for me to approach this?
Hi,

The problems are caused by a missing NULL character at the end of your string. Here is a sample:

Code:

char *c = new char[2];
c[1] = 0;
Thanks Edge, that was it exactly.

I've been expanding my code and its been going well, but I've run into a strange problem. I'm parsing through the the values contained in a csv file and loading them into a two dimensional array. It seems to work perfectly fine on my PC, but when I run it on either of two WM devices, or on the MS Device Emulator, zero values are inserted seemingly arbitrarily into my array. Since the array contains information for a tile based map, this results in my zero tile being displayed, and the alignment of the remaining tiles in the row to be thrown off.

Each time I load the application on a device, the zero tiles appear in the same place. However, the zeros appear in different places on each of my respective test devices. I tried installing to main and to storage memory, but the results are the same.

What's even stranger is that when I start using the Log function in my loading loop to see what's going on, the map data loads fine. Does anyone have any idea as to what might be happening here? If you'd would like to see my code, please let me know.
Hi,

Can you tell me how you declare and initialize your array? Also try to look at how you access the array and see if you're reading or writing outside of its bounds.
Thanks for the quick reply. My array's declared like this in the map class header:
Code:

short **layers[4];

The structure contains four layers. The second and third dimensions form the actual map grid, and are defined in the loading loop based on the dimensions of the map. So in the end, I access the structure like this: layers[LAYER][xTile][yTile]. My loading loop looks like this:
Code:

ClassEFile ef;
GameFile gf(wanderModule->shared);

if (ef.Open("maps.epk", EFOF_READ)) {
if (ef.PackGetFileData(gf.data, gf.size, "maranda.csv") == E_OK) {
// Read information such as map name and dimensions
char* mapname = gf.ReadLine();
EString* dim = gf.SplitLine(",");
mapsize.x = ClassEStd::StrToInt(dim[0], 10);
mapsize.y = ClassEStd::StrToInt(dim[1], 10);
gf.NextLine();

// Dynamically allocate memory for layers, based on mapsize
for (int layer = 0; layer < 4; layer++) {
layers[layer] = new short *[mapsize.x];
for (int x = 0; x < mapsize.x; x++)
layers[layer][x] = new short[mapsize.y];
}
// Read map data from file and load into layers
for (int curLayer = 0; curLayer < 4; curLayer++) {
for (int y = 0; y < mapsize.y; y++) {
EString* row = gf.SplitLine(",");
for (int x = 0; x < mapsize.x; x++)
layers[curLayer][x][y] = ClassEStd::StrToInt(row[x], 10);
}
gf.NextLine();
}
// ... cleanup

I've tried setting the layers structure statically (like layers[4][31][31]), but kept getting the same results.

I was also using the standard ClassEFile functions when I first noticed the problem, so I don't think it has to do with my GameFile class... Before I wrote my GameFile class, my original loading loop looked like this:

Code:

// Read map data from file and load into layers
for (int curLayer = 0; curLayer < 4; curLayer++) {
for (int y = 0; y < mapsize.y; y++) {
for (int x = 0; x < mapsize.x; x++) {
int len = 0;
char *c = new char[2];
c[1] = '\0';
// Parse until a delimiter is found
while(!ClassEStd::StrEqual(c, ",") && !ClassEStd::StrEqual(c, "#") && len < 8) {
ef.Read(c, 1);
len++;
}
// Go back and load the data into layers
ef.Seek(ef.Tell()-len);
char *app = new char[len+1];
app[len] = '\0';
ef.Read(app, len);
ClassEStd::StrReplace(app, ",", "", false);
layers[curLayer][x][y] = ClassEStd::StrToInt(ClassEStd::StrTrim(app), 10);
}
}
}


Thanks again for any help!
Hi,

Maybe you can try using a static array instead of dynamically allocating it, and make a test with a fixed size map.
I tried setting my array statically with a fixed size map before, but still noticed I was getting the same issue :(
That sounds odd. Perhaps another part of your code is corrupting your array?

It could be a good idea to make a small sample with an empty framework (such as the helloworld sample) and see how your array is loaded. Also try to log when you read the csv file character by character, and also log when it's done reading it. Check to see if you notice any differences.