Archived

This topic is now archived and is closed to further replies.

Integer

How to load values from a *.txt file int may Array: int Array[50][50]

Recommended Posts

Integer    122
I have *.txt file called data.txt. In it, there are 50*50 values like this: ....1918278247859743485768747563074027670257867... What I want to do is to load these values in my programm into my array with [50][50] How can I do that? If you dont want to post you can also send me an email at integer82@web.de

Share this post


Link to post
Share on other sites
Quantum    122
Off the top of my head, untested:
(This is if it''s in binary, I don''t know how you''d know where one number ends and the other begins if it''s plain text.)
  
FILE *File = fopen(filename, "rb");
int Array[50][50];

for(int i = 0; i < 50; i++)
for(int j = 0; j < 50; j++)
fread(Array[i][j], sizeof(int), 1, File);


Something like that anyway.

Share this post


Link to post
Share on other sites
mars_888    122
I don't think that would work. If you read them like that you would put the ascii representation in the array instead of the acctuall numbers.
I think you should try something like this:

  
FILE * file = fopen(fileName, "rb");
char buffer[2]; // Used to store the Ascii code.

buffer[1] = 0; // Must be 0 terminated string.

int array[50][50];

for(int i = 0; i < 50; i++){
for(int j = 0; j < 50; j++){
// Read 1 value from file and store it in buffer:

fread(&buffer, sizeof(char), 1, file);

// Convert value in buffer and put it in the array:

sscanf(buffer, "%i", &array[i][j]);
}
}


You might also have to put in some code to skip new line characters and such. If you have spaces in the file you might also use fscanf(....); to convert directely from the file.

Hope this helps.

Edited by - mars_888 on March 16, 2001 8:46:25 PM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
I''m a newbie so you experts don''t kill me if I''m wrong but I used C++ code to load my values out of a .txt file and loaded it in binary mode like this.

///Top
#include "fstream.h"
#include "The Others Stuff"

///Cause I have 24 lines and 5 numbers across (array starts at 0)

float Array[23][4];

char filename[12]= "Polygons.txt";
int mode = (ios::in || ios::binary);

///Then I have a function like this
void LoadFile()
{

int layer=0;
int loop=0;

fstream fin(filename,mode);

while (fin >> Array[layer][loop])
{
loop++;
if (loop > 4) {layer++;loop=0;}
}

fin.close();

}

***************************************************************
This code loads the data from my file which is organised like this

1st 3 are vertex coords, 2nd 2 are tex coords
24 Lines down and 5 numbers across (Array[23][4])
-30.0 10.0 -180.0 1.0 0.0
-30.0 -5.0 -180.0 0.0 0.0
30.0 -5.0 -180.0 0.0 1.0
30.0 10.0 -180.0 1.0 1.0

30.0 10.0 50.0 1.0 0.0
30.0 -5.0 50.0 0.0 0.0
-30.0 -5.0 50.0 0.0 1.0
-30.0 10.0 50.0 1.0 1.0

-29.9 10.0 50.0 1.0 0.0
-29.9 -5.0 50.0 0.0 0.0
-29.9 -5.0 -150.0 0.0 1.0
-29.9 10.0 -150.0 1.0 1.0

30.0 10.0 -180.0 1.0 0.0
30.0 -5.0 -180.0 0.0 0.0
30.0 -5.0 50.0 0.0 1.0
30.0 10.0 50.0 1.0 1.0

-30.0 10.0 50.0 0.0 0.0
-30.0 10.0 -180.0 1.0 0.0
30.0 10.0 -180.0 1.0 1.0
30.0 10.0 50.0 0.0 1.0

-30.0 -5.0 -180.0 1.0 0.0
-30.0 -5.0 50.0 0.0 0.0
30.0 -5.0 50.0 0.0 1.0
30.0 -5.0 -180.0 1.0 1.0


Sorry if this didn''t help but this is how I load my numbers into my arrays. But you would have to orgranise your data sorta like mine or alter the code if this was to work.

Share this post


Link to post
Share on other sites
Clouds3000    122
I''m a newbie so you experts don''t kill me if I''m wrong but I used C++ code to load my values out of a .txt file and loaded it in binary mode like this.

///Top
#include "fstream.h"
#include "The Others Stuff"

///Cause I have 24 lines and 5 numbers across (array starts at 0)

float Array[23][4];

char filename[12]= "Polygons.txt";
int mode = (ios::in || ios::binary);

///Then I have a function like this
void LoadFile()
{

int layer=0;
int loop=0;

fstream fin(filename,mode);

while (fin >> Array[layer][loop])
{
loop++;
if (loop > 4) {layer++;loop=0;}
}

fin.close();

}

***************************************************************
This code loads the data from my file which is organised like this

1st 3 are vertex coords, 2nd 2 are tex coords
24 Lines down and 5 numbers across (Array[23][4])
-30.0 10.0 -180.0 1.0 0.0
-30.0 -5.0 -180.0 0.0 0.0
30.0 -5.0 -180.0 0.0 1.0
30.0 10.0 -180.0 1.0 1.0

30.0 10.0 50.0 1.0 0.0
30.0 -5.0 50.0 0.0 0.0
-30.0 -5.0 50.0 0.0 1.0
-30.0 10.0 50.0 1.0 1.0

-29.9 10.0 50.0 1.0 0.0
-29.9 -5.0 50.0 0.0 0.0
-29.9 -5.0 -150.0 0.0 1.0
-29.9 10.0 -150.0 1.0 1.0

30.0 10.0 -180.0 1.0 0.0
30.0 -5.0 -180.0 0.0 0.0
30.0 -5.0 50.0 0.0 1.0
30.0 10.0 50.0 1.0 1.0

-30.0 10.0 50.0 0.0 0.0
-30.0 10.0 -180.0 1.0 0.0
30.0 10.0 -180.0 1.0 1.0
30.0 10.0 50.0 0.0 1.0

-30.0 -5.0 -180.0 1.0 0.0
-30.0 -5.0 50.0 0.0 0.0
30.0 -5.0 50.0 0.0 1.0
30.0 -5.0 -180.0 1.0 1.0


Sorry if this didn''t help but this is how I load my numbers into my arrays. But you would have to orgranise your data sorta like mine or alter the code if this was to work.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
I won´t read all of your code,Clouds3000 BUT:

.-In C arrays DO start at 0,BUT you DECLARE its SIZE,not their max value.

:This is a 24 integers array: int Array[24];//NOT 23
:This is how to loop it: for(int i=0;i<24;i++) Array....
///This loops from 0 to 23.


.-This is a VERY TYPICAL newbie error(I made it MANY times, as i learnt TurboPascal before.).

.-Another be careful trick: Multidimensional arrays are interpreted "backwards" in C: int[Y][X] will be (X,Y).



What the hells!

Share this post


Link to post
Share on other sites
Clouds3000    122
Thank goodness I tried to help here because I do believe that you advice has just helped me locate my memory leak (thats what its called, yea? When you define an array and write more to it than you declared)
Thanks

Share this post


Link to post
Share on other sites
Redeemer    122
Clouds3000:
just i little thing i noticed in your code, every time you make comments you are useing "///" for example:

glShadeModel(GL_SMOOTH); /// Smooth shading enabled

when you can just use "//" for example:

glShadeModel(GL_SMOOTH); // Smooth shading enabled

just thourght i would point it out in case you didn''t know :p

-=Redeemer=-

Share this post


Link to post
Share on other sites