3 Replies - 879 Views - Last Post: 29 April 2012 - 01:30 PM Rate Topic: -----

#1 edgeorge92   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-April 12

Loading into 2D Array from file in C

Posted 29 April 2012 - 01:06 PM

I have a text file with 25 lines, each with 34 characters on.

In C how is it possible to load these characters and store them into a 2D array?

If the first three lines of the file are such:

bamaaaaaaaacxxxxxxxxxxbaaaaaaaamac
jzjzzzzzzzzdaaaaaaaaaaezzzzzzzzjzj
jzjzbaaczgzzzzzzzzzzzzzzgzbaaczjzj

...and so on

I require the array to be stored as if it was defined like this:

char* data[] = {
"baaaaaaaaaaaaaacxxbaaaaaaaaaaaaaac",
"jzzzzzzzzzzzzzzjxxjzzzzzzzzzzzzzzj",
"jzbaaaaaaaaaaaaexxdaaaaaaaaaaaaczj",
...and so on

Hopefully this makes some sense! It is important that the type of data is char data[][] as it is used in that format in the rest of my project and cannot be changed.

I have done the basic begining of the File IO
FILE  *infp;

printf("Opening file\n");

if((infp = fopen("map1.txt", "r"))== NULL) {
    printf("\nERROR : Unable to open input file\n");
    SetExitWithCode( 999 );
    }else{
            //code here
            }



Can anyone give me some advice?
(Attached is the file I am currently attempting to load)

Is This A Good Question/Topic? 0
  • +

Replies To: Loading into 2D Array from file in C

#2 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: Loading into 2D Array from file in C

Posted 29 April 2012 - 01:17 PM

You have to make more of an attempt to do this yourself. All you have so far is an error message when the file can't be opened.

Here's an example involving reading a single string from a file. You can figure out how to modify that to use for an array of strings (char[][]). And I'm sure you can find more info in your textbook.
Was This Post Helpful? 0
  • +
  • -

#3 edgeorge92   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-April 12

Re: Loading into 2D Array from file in C

Posted 29 April 2012 - 01:20 PM

Sorry I didn't supply any of my attempts!
I have been working on this for a while now, That said, I just cracked it!

Never mind I guess!

char data[MAX_L][MAX_W];
FILE  *infp;
char* file = "map2.txt";
//printf("\nOpening file\n");
if((infp = fopen(file, "r"))== NULL) {
	printf("\nERROR : Unable to open input file\n");
	SetExitWithCode( 999 );
}else{
	fread(data, 1, sizeof(data), infp);
	for (int i = 0; i < MAX_L; ++i) {
		data[i][MAX_W-1] = 0;
	}
	fclose(infp);
}


Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: Loading into 2D Array from file in C

Posted 29 April 2012 - 01:30 PM

That's not a good way to do this. fread (and fwrite) are not intended to be used with text files. Those functions are for unformatted (binary data) files.

Text files contain formatted ascii data with newline characters and possibly other delimiters, and the functions fgetc (for single characters) and fgets (for strings) are intended to be used with text files.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1