473,387 Members | 1,321 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
5 5650
blazedaces
284 100+
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
9,208 Expert Mod 8TB
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
runsun
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
9,208 Expert Mod 8TB
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
1,251 Expert 1GB
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

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

Similar topics

3
by: dave | last post by:
Hello there, I am at my wit's end ! I have used the following script succesfully to upload an image to my web space. But what I really want to be able to do is to update an existing record in a...
4
by: pisscot | last post by:
Lind Apr 13, 6:32 am show options Newsgroups: comp.lang.c From: piss...@gmail.com (Lind) - Find messages by this author Date: 13 Apr 2005 06:32:15 -0700 Local: Wed,Apr 13 2005 6:32 am...
10
by: Tibby | last post by:
I need to read/write not only text files, but binary as well. It seems like on binary files, it doesn't right the last 10% of the file. -- Thanks --- Outgoing mail is certified Virus...
1
by: tim | last post by:
I have a multi-dimensional byte array, private someArray(,,) as byte and it's huge redim someArray(100,640,480,3) after populating the array, I write it to a text file to save it. (so I don't...
35
by: RyanS09 | last post by:
Hello- I am trying to write a snippet which will open a text file with an integer on each line. I would like to read the last integer in the file. I am currently using: file = fopen("f.txt",...
23
by: ShaneO | last post by:
Hello, I wish to extract embedded string data from a file using a Binary Read method. The following code sample is used in VB.NET and similar code is used in VB6 - (Assume variable...
2
by: devnew | last post by:
hi i am new to python and PIL and was trying to write a class to read and write RGB color model jpeg images.. i came up with this class below..i want to know if this is the way to read/write the...
9
by: vineeth | last post by:
Hello all, I have come across a weird problem, I need to determine the amount of bytes read from a file, but couldn't figure it out , My program does this : __ file = open("somefile") data =...
5
by: dm3281 | last post by:
Hello, I have a text report from a mainframe that I need to parse. The report has about a 2580 byte header that contains binary information (garbage for the most part); although there are a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.