Reading from file to 2 dimensional array

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 5871 Views - Last Post: 16 December 2011 - 09:30 AM Rate Topic: -----

#1 faen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 30-April 10

Reading from file to 2 dimensional array

Posted 15 December 2011 - 03:05 PM

Hi I've been trying to figure out how to do this, but after some reasearch on google and the lecture notes I'm still not sure if i did it right. So my question is if the code below in the code field will read the file correctly and store the numbers in the 2 dimensional array. The file would look for example like this (24 numbers wide and high):

1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1




FILE *fp;

int current[24][24];

fread(current, sizeof(int), 24*24, fp);





Thanks in advance for help :)

This post has been edited by faen: 15 December 2011 - 03:06 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Reading from file to 2 dimensional array

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4220
  • View blog
  • Posts: 13,227
  • Joined: 25-December 09

Re: Reading from file to 2 dimensional array

Posted 15 December 2011 - 03:20 PM

Quote

So my question is if the code below in the code field will read the file correctly and store the numbers in the 2 dimensional array


No, fread is really designed to read binary files where there is no separation between the variables. The best way to read this file would be using loops to extract each number into your array elements.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Reading from file to 2 dimensional array

Posted 15 December 2011 - 03:24 PM

It depends on the file. If the file "looks like" that, then no, because that would be a text file which contains characters (in other words, bytes) including newline chars and possibly also 'carriage return' characters depending on which os created it.

If the file is a binary file containing only numeric data and nothing else, then your code should work.
Was This Post Helpful? 1
  • +
  • -

#4 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 449
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: Reading from file to 2 dimensional array

Posted 15 December 2011 - 03:27 PM

Reading the explanation of fread I'm afraid this is not correct.

size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );

For each element the the function will read size (4) bytes from the file and interpret this literally as the internal presentation of the type.

This post has been edited by Karel-Lodewijk: 15 December 2011 - 03:28 PM

Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • Joined: 09-June 09

Re: Reading from file to 2 dimensional array

Posted 15 December 2011 - 03:28 PM

fread is for binary IO, i'm assuming that the file above, although are 1's and 0's, is not a binary file. If you have a binary file with 24 * 24 integers you could do something like this.

File *file = fread("your file", "rb"); //open for read binary
int *buff = malloc(sizeof(int) * 24 * 24);
fread(buff, sizeof(int), 24 * 24, file);


Was This Post Helpful? 0
  • +
  • -

#6 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 449
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: Reading from file to 2 dimensional array

Posted 15 December 2011 - 03:30 PM

That's 4 people giving the same answer, anyone else ? :)

This post has been edited by Karel-Lodewijk: 15 December 2011 - 03:32 PM

Was This Post Helpful? 0
  • +
  • -

#7 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Reading from file to 2 dimensional array

Posted 15 December 2011 - 03:32 PM

Not enough questions? -.-
Was This Post Helpful? 0
  • +
  • -

#8 faen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 30-April 10

Re: Reading from file to 2 dimensional array

Posted 15 December 2011 - 03:44 PM

Thanks a lot for help everyone! :)

It wouldn't be a problem to modify the file to contain only integers without any spaces. Does it then matter that the 2 dimensional array which i store the numbers into is not a pointer? Will it store the numbers in the correct order as well? Such that e. g. current[x][y] where x and y are any random index would be the number x steps to the right and y would be y steps down in the file?
Was This Post Helpful? 0
  • +
  • -

#9 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • Joined: 09-June 09

Re: Reading from file to 2 dimensional array

Posted 15 December 2011 - 03:58 PM

No, the fread is a binary read operation, therefore you cannot use it to read a ascii text file.

Quote

2 dimensional array which i store the numbers into is not a pointer

A array is essentially a pointer to a block of memory.


Read this, http://www.cs.bu.edu.../file-io/intro/

This post has been edited by Loundy: 15 December 2011 - 03:59 PM

Was This Post Helpful? 0
  • +
  • -

#10 faen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 30-April 10

Re: Reading from file to 2 dimensional array

Posted 15 December 2011 - 04:03 PM

I modified the file and tried the following solution:


int a;
File *file = fread("your file", "rb"); //open for read binary
                     int *buff = malloc(sizeof(int) * 24 * 24);
                     
                     fread(buff, sizeof(int), 24 * 24, file);
                     
                     
                        a=0;
                         
                         for(i=0;i<24;i++){
                      
                             for(j=0;j<24;j++){
                                               
                                               current[i][j]=buff[a];
                                               
                                               a++;
                                               
                                                                                            
                                               
                                  }
                             }



Will this work if it reads from a file like this?

110000000000000000000000
110000000000000000000000
000000000000000000000000
000000000000000000000000
000000000000000000000000
000000000000010000000000
000000000000000000000000
000000000000000000000000
000000000000000000000000
000000000000000000000000
000000000000000000000000
000000000000000000000000
000000000000000000000000
000000000000000000000000
000000000000000000000000
000000000000000000000000
000000000000000000000000
000000000000000000000000
000000000000000000000000
000000000000000000000000
000000000000000000000000
000000000000000000000000
000000000000000000000000
000000000000000000000001
Was This Post Helpful? 0
  • +
  • -

#11 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Reading from file to 2 dimensional array

Posted 15 December 2011 - 04:07 PM

Still, it depends on how the data was put into the file. It's not just that there be 'no spaces'. The file must have been opened to write in binary mode, and the int data written to it with, for example, fwrite.

If you used fprintf, or fputc, etc., it won't work.
Was This Post Helpful? 0
  • +
  • -

#12 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • Joined: 09-June 09

Re: Reading from file to 2 dimensional array

Posted 15 December 2011 - 04:08 PM

Do you think that is a binary file?
Was This Post Helpful? 0
  • +
  • -

#13 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Reading from file to 2 dimensional array

Posted 15 December 2011 - 04:12 PM

You could use fscanf for formatting ascii data read from a file.

Prototype
int fscanf(FILE *stream, const char *format, ...);


e.g.
// read integer from file.
int num;
fscanf(fp, "%d", &num);

This post has been edited by Aphex19: 15 December 2011 - 04:15 PM

Was This Post Helpful? 0
  • +
  • -

#14 faen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 30-April 10

Re: Reading from file to 2 dimensional array

Posted 15 December 2011 - 04:29 PM

In the task it said that it should be able to read the input from a file. The input is only 1's and 0's. It didn't specify which type of file. So if i deliver the code as above, do you think it would be accepted? Since we could assume that the file it reads is a binary one? Is the code otherwise correct? Thanks again for your help :)
Was This Post Helpful? 0
  • +
  • -

#15 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • Joined: 09-June 09

Re: Reading from file to 2 dimensional array

Posted 15 December 2011 - 04:33 PM

Lets assume your file is binary and that the size of an int is 16 bits, or 2 bytes. This would imply that you would need 9216 ones and zeros in your binary file; However you only have 576 ones and zeros. This implys the file you will be reading is not binary, its text.

This post has been edited by Loundy: 15 December 2011 - 04:38 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2