By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,404 Members | 1,037 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,404 IT Pros & Developers. It's quick & easy.

read data from file, write into 3D array - need help again.

P: 17
Thanks in advance. This program is written in C.
It needs to read all characters from a file; then write them into a 3D array (yes, 3D!).
The file is a .prn file (one of the Excel types), which separates columns with space.
To simplify the question, the file contains 3 rows and 2 columns:
abc 2,3
defg 4
h 5,6

array is array[3][2][5], where the 3rd D contains the # of rows; 2nd D has #conlumns; 1st D contains the character elements. i.e. hope:
Expand|Select|Wrap|Line Numbers
  1. array[0][0][0] = 'a', array[0][0][1] = 'b', array[0][0][2] = 'c',
  2. array[0][1][0] = '2', array[0][1][1] = '3',
  3. array[1][0][0] = 'd', array[1][0][1] = 'e', array[1][0][2] = 'f', array[1][0][3] = 'g',
  4. ...

Here is the code. Compiling did not give any error, but nothing is in the array. I read books and searched online all day but did not figure it out.
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void main( )
  5. {
  6.    char out_char;
  7.    char array[3][2][5];
  8.    int i, j, k;
  9.    FILE *pfile = NULL;
  10.    char *filename = "data.prn";
  11.    pfile = fopen(filename, "r");
  12.    if (pfile != NULL)
  13.      {
  14.         while ((out_char = fgetc(pfile))!= EOF)
  15.              {
  16.                for (i=0; i<3; i++)
  17.                 {
  18.                   for (j=0; j<2; j++)
  19.                      {
  20.                         if (out_char == '\n') continue;
  21.                         for (k=0; k<5; k++)
  22.                           {
  23.                              if (out_char == ' ') continue;
  24.                              array[i][j][k] = out_char;
  25.                           }
  26.                      }
  27.                 }
  28.              }
  29.         }
  30.    else
  31.          printf ("\n\n\t\t%s file does not exist.\n", filename);
  32.    fclose(pfile);
  33.       printf ("\n");
  34. }
Jun 8 '07 #1
Share this Question
Share on Google+
5 Replies


blazedaces
100+
P: 284
Thanks in advance. This program is written in C.
It needs to read all characters from a file; then write them into a 3D array (yes, 3D!).
The file is a .prn file (one of the Excel types), which separates columns with space.
To simplify the question, the file contains 3 rows and 2 columns:
abc 2,3
defg 4
h 5,6

array is array[3][2][5], where the 3rd D contains the # of rows; 2nd D has #conlumns; 1st D contains the character elements. i.e. hope:
array[0][0][0] = 'a', array[0][0][1] = 'b', array[0][0][2] = 'c',
array[0][1][0] = '2', array[0][1][1] = '3',
array[1][0][0] = 'd', array[1][0][1] = 'e', array[1][0][2] = 'f', array[1][0][3] = 'g',
...


Here is the code. Compiling did not give any error, but nothing is in the array. I read books and searched online all day but did not figure it out.
#include <stdio.h>
#include <stdlib.h>

void main( )
{
char out_char;
char array[3][2][5];
int i, j, k;
FILE *pfile = NULL;
char *filename = "data.prn";
pfile = fopen(filename, "r");
if (pfile != NULL)
{
while ((out_char = fgetc(pfile))!= EOF)
{
for (i=0; i<3; i++)
{
for (j=0; j<2; j++)
{
if (out_char == '\n') continue;
for (k=0; k<5; k++)
{
if (out_char == ' ') continue;
array[i][j][k] = out_char;
}
}
}
}
}
else
printf ("\n\n\t\t%s file does not exist.\n", filename);
fclose(pfile);
printf ("\n");
}
Listen, I'm not incredibly familiar so don't take my word on this, but I believe if you're making it a char[][][] array then it expects character values for all of those. There are alternatives though that I can suggest. The easiest to do exactly what you want is use vectors (harder to understand if you've never used them or made matrices with them, but they work like a charm). With vectors you can set the type of every object stored separately (again, not 100% sure on this, can someone verify?

There's always other solutions too though: remember that characters are ascii, which means you could store them as (char) row and (char) column (is this how you cast in C++, it's been a while for me...?). Then when you want the row and column to be known or used or shown, simply cast back to (int).

You could also use an array of bytes if you know that you won't be using so many characters... but I'm not sure...

Just some solutions that I can think up right now.

On a sidenote though, you'd best post what errors you're getting or what you believe is the problem/where. It would help people to help you.

Good luck,

-blazed
Jun 8 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
Are you sure this is right?
array[0][0][0] = 'a', array[0][0][1] = 'b', array[0][0][2] = 'c',
array[0][1][0] = '2', array[0][1][1] = '3',
array[1][0][0] = 'd', array[1][0][1] = 'e', array[1][0][2] = 'f', array[1][0][3] = 'g',
This is a [3][2][5] array but the above example has no values for
array[0][0][3]
array[0][0][4]
array[0][1][2]
array[0][1][3]
array[0][1][4]
array[1][0][4]

and nothing for any elements in array[2][x][x].

That means the array is niot completely initialized and will have indeterminate garbage in some elements. You need a scheme that initializes every array element.

When you have that, re-post and I'll have a look at your code.
Jun 8 '07 #3

P: 17
Are you sure this is right?


This is a [3][2][5] array but the above example has no values for
array[0][0][3]
array[0][0][4]
array[0][1][2]
array[0][1][3]
array[0][1][4]
array[1][0][4]

and nothing for any elements in array[2][x][x].

That means the array is niot completely initialized and will have indeterminate garbage in some elements. You need a scheme that initializes every array element.

When you have that, re-post and I'll have a look at your code.
I find a way to write data into the array. Now the problem is since the numbers of elements are not same in different cell, when printf %c, some strange characters may appear.
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h> 
  2. #include <stdlib.h>
  3.  
  4. void main( )
  5. {
  6.    char out_char;
  7.    char array[3][2][5];
  8.    int i, j, k;
  9.    FILE *pfile = NULL;
  10.    char *filename = "data.prn";
  11.  
  12.   pfile = fopen(filename, "r");
  13.    if (pfile == NULL)
  14.      {           
  15.         printf ("\n\n\t\t%s file does not exist.\n", filename);
  16.         exit(1);
  17.      }
  18.    for (i=0; i<3; i++)
  19.       {
  20.         for (j=0; j<2; j++)
  21.            {
  22.                     fscanf (pfile, "%s", array[i][j]);
  23.             }
  24.         }
  25.    fclose(pfile);  
  26.  
  27.    for (i=0; i<3; i++)
  28.            {  
  29.               for (j=0; j<2; j++)
  30.                  {
  31.                     for (k=0; k<5; k++) 
  32.                          {printf ("%c", array[i][j][k]);}
  33.                      printf ("\t");
  34.                  }
  35.               printf ("\n");
  36.             }
  37.         printf ("\n");
  38. }
Jun 8 '07 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
I took your code and rewrote it:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void main( )
  5. {
  6. char out_char;
  7. char array[3][2][5];
  8. char* temp;
  9. int i, j, k;
  10. FILE *pfile = NULL;
  11. char *filename = "data.prn";
  12. pfile = fopen(filename, "r");
  13. if (pfile == NULL)
  14. {
  15.     printf ("\n\n\t\t%s file does not exist.\n", filename);
  16. }
  17. temp=&array[0][0][0];
  18. i = 0;
  19. while ( i < 30)
  20. {        
  21.     out_char = fgetc(pfile);
  22.     if (out_char == EOF)
  23.     {
  24.         break;
  25.     }
  26.     if (out_char != ' ')
  27.     {
  28.         *(temp + i) = out_char;
  29.         ++i;
  30.     }
  31.  
  32. }
  33. /* display array */
  34. for (i = 0; i < 3;++i)
  35. {
  36.     for (j=0; j < 2; ++j)
  37.     {
  38.         for(k=0; k < 5; ++k)
  39.         {
  40.             printf("%c", array[i][j][k]);
  41.         }
  42.     }
  43.  
  44. }
  45. fclose(pfile);
  46. printf("\n");
  47. }
  48.  
The code operates on the priciple that there are only one-dimensional arrays in C. That is, an array of [2][2] and an array of [4] both have the same layout i memory.

So, I took the address of your array[0][0][0] as the starting address and I loaded up to 30 char, which is the size of the array.

Then I display the array as a 3D array and you can see correct results.

I hope this helps.
Jun 10 '07 #5

AdrianH
Expert 100+
P: 1,251
Thanks in advance. This program is written in C.
It needs to read all characters from a file; then write them into a 3D array (yes, 3D!).
The file is a .prn file (one of the Excel types), which separates columns with space.
To simplify the question, the file contains 3 rows and 2 columns:
abc 2,3
defg 4
h 5,6

array is array[3][2][5], where the 3rd D contains the # of rows; 2nd D has #conlumns; 1st D contains the character elements. i.e. hope:
Expand|Select|Wrap|Line Numbers
  1. array[0][0][0] = 'a', array[0][0][1] = 'b', array[0][0][2] = 'c',
  2. array[0][1][0] = '2', array[0][1][1] = '3',
  3. array[1][0][0] = 'd', array[1][0][1] = 'e', array[1][0][2] = 'f', array[1][0][3] = 'g',
  4. ...

Here is the code. Compiling did not give any error, but nothing is in the array. I read books and searched online all day but did not figure it out.
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void main( )
  5. {
  6.    char out_char;
  7.    char array[3][2][5];
  8.    int i, j, k;
  9.    FILE *pfile = NULL;
  10.    char *filename = "data.prn";
  11.    pfile = fopen(filename, "r");
  12.    if (pfile != NULL)
  13.      {
  14.         while ((out_char = fgetc(pfile))!= EOF)
  15.              {
  16.                for (i=0; i<3; i++)
  17.                 {
  18.                   for (j=0; j<2; j++)
  19.                      {
  20.                         if (out_char == '\n') continue;
  21.                         for (k=0; k<5; k++)
  22.                           {
  23.                              if (out_char == ' ') continue;
  24.                              array[i][j][k] = out_char;
  25.                           }
  26.                      }
  27.                 }
  28.              }
  29.         }
  30.    else
  31.          printf ("\n\n\t\t%s file does not exist.\n", filename);
  32.    fclose(pfile);
  33.       printf ("\n");
  34. }
I have a sneeky suspicion that what you are doing is not what you want.

What you probably want to do is use a struct, otherwise you are going to 1) waste memory, 2) confuse anyone who is going to be looking at your code, including yourself. ;)

Try an (1d) array of structs instead.


Adrian
Jun 10 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.