473,803 Members | 3,422 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

17 New Member
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 5690
blazedaces
284 Contributor
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 Recognized Expert Moderator Expert
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 New Member
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 Recognized Expert Moderator Expert
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 Recognized Expert Top Contributor
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
11768
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 table in MySQL with the path & filename to the image. I have successfully uploaded and performed an update query on the database, but the problem I have is I cannot retain the primary key field in a variable which is then used in a SQL update...
4
1864
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 Subject: how to read it out using c++ Reply | Reply to Author | Forward | Print | Individual Message | Show original | Remove | Report Abuse
10
2548
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 Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.538 / Virus Database: 333 - Release Date: 11/10/2003
1
2713
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 run out of memory). I will retrieve from the file later. What I do is use streamwriter.write and streamreader.read
35
11506
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", "r+"); fseek(file, -2, SEEK_END); fscanf(file, "%d", &c); this works fine if the integer is only a single character. When I get into larger numbers though (e.g. 502) it only reads in the 2. Is there
23
2840
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 declarations etc.) FileOpen(iFileIn, sInputFile, OpenMode.Binary, OpenAccess.Read)
2
2264
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 RGB color model jpeg..if anyone can give feedback (esp on the methods writeImage1( ) and writeImage2( ) it wd help my studies .. TIA dn
9
3852
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 = file.read() print "bytes read ", len(data) ---
5
11281
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 couple areas that have ASCII text that I need to extract. At the end of the 2580 bytes, I can read the report like a standard text file. It should have CR/LF at the end of each line. What is the best way for me to read this report using C#. It is...
0
10548
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10295
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9125
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.