Skip to main content
GameDev.net gamedev.net
🔒 Locked

Problems with arrays

Started by Lucas Daltro Oct 6, 2010 at 1:04 PM 2 replies 850+ views
Original Post
Lucas Daltro
Lucas Daltro
I've a question,how to make an array read a text file?
For example:

int Map[15][20] = {
3,3,3,3,4,5,5,5,5,6,2,2,1,10,10,8,9,9,9,9,
3,3,3,3,4,5,5,5,5,6,2,2,1,7,7,7,9,9,9,9,
3,3,3,3,4,5,5,5,5,6,2,2,1,7,7,7,9,9,9,9,
3,3,3,3,4,5,5,5,5,6,2,2,1,7,7,8,9,9,9,9,
3,3,3,3,4,5,5,5,5,6,2,2,1,7,7,8,8,8,8,8,
3,3,3,3,4,5,5,5,5,6,2,2,1,7,7,7,7,7,7,7,
3,3,3,3,4,5,5,5,5,6,2,2,1,7,7,7,7,7,7,7,
3,3,3,3,4,5,5,5,5,6,2,2,1,7,0,10,7,7,10,0,
3,3,3,3,4,5,5,5,5,6,2,2,1,7,10,0,7,7,10,0,
3,3,3,3,3,4,5,5,5,6,2,2,1,7,0,10,7,7,10,0,
3,3,3,3,3,3,4,5,5,6,2,2,1,7,10,0,7,7,10,0,
3,3,3,3,3,3,4,5,5,6,2,2,1,7,0,10,7,7,10,0,
3,3,3,3,3,3,4,5,5,6,2,2,1,7,10,0,7,7,10,0,
3,3,3,3,3,3,4,5,5,6,2,2,1,7,0,10,7,7,10,0,
3,3,3,3,3,3,4,5,5,6,2,2,1,7,10,0,7,7,10,0};

I'm tryning to pass the vavule of this array for a text file,but when I do this:

int Mapa[15][20] =fopen("map.txt","w");
the code gives error(obivious).Can somebody help me?
"Call me crazy in a world where genius create bombs" .: -=|J.Ð.|Gä(/)ëÐë§¡gÑ||=- :.
the_edd
the_edd
I think you'd be wise to learn a little more about handling files in C from a good book or tutorial before going much further.

With that said, fopen() returns a FILE* (on success), which is kind of handle through which you may read and/or write data to a file.

Once you have a FILE*, there are a number of functions you can use to read the data. The function(s) you choose will depend on how the data is formatted. fread(), fgetc() and fscanf() are some likely candidates.

Also, if you're just starting, might I suggest you consider an easier language e.g. Python?

EDIT: typos.

[Edited by - the_edd on October 6, 2010 5:32:44 PM]
blubberbert
blubberbert
are you using C or C++? (dont use C in C++ programs unless you really have to)
for C++ check out fstreams instead
for C use:
int x, y;FILE *f = fopen("map.txt", "r");for(x = 0; x < 15; x++)   for(y = 0; y < 20; y++)      fread(⤅[x][y], sizeof(int), 1, f);

or something like that... i dont use FILE very often, i like fstream more :P
------------------------------
Join the revolution and get a free donut!
Lucas Daltro
Lucas Daltro
Thak so much guys:)!I followed your advice, bought the book c complete reference"C completo e total" here in Brazil(Bye, bye my allowance:/)
"Call me crazy in a world where genius create bombs" .: -=|J.Ð.|Gä(/)ëÐë§¡gÑ||=- :.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.