Binary files and arrays - please help 
Author Message
 Binary files and arrays - please help

I want to load information from a binary file into a multidimensional
array.Then later save info back to file.

What is the best way to do this?

Here is my prog:

#include <stdio.h>
#include <conio.h>
#define X_AXIS 50
#define Y_AXIS 50
#define Z_AXIS 10
#define FILE_NAME "map.bin"
// Adventure v.1 using DJGPP

main()
{
FILE *fp;
char ch;
int x,y,z;
int location[X_AXIS][Y_AXIS][Z_AXIS];
clrscr();
printf("Loading Data...\n");
fp= fopen (FILE_NAME , "rb+");
while ((ch=getc(fp)) !=EOF){
  printf("%c",ch);

Quote:
}

fclose(fp);
for(x=1;x<X_AXIS;x++){
  for(y=1;y<Y_AXIS;y++){
    for(z=0;z<Z_AXIS;z++){
    // Read data from binary "map.bin" into multidimensional array
    }
  }

Quote:
}
return(0);
}

I'm using DJGPP which uses C and I believe can use C++ too.
I'm very new to C, so any help would be greatly appreciated :)
Thanks.
--



Mon, 03 Mar 2003 03:00:00 GMT  
 Binary files and arrays - please help

Quote:

> #include <stdio.h>
> #include <conio.h>

This is a system-specific header, not part of the ANSI C
standard.

Quote:
> #define X_AXIS 50
> #define Y_AXIS 50
> #define Z_AXIS 10
> #define FILE_NAME "map.bin"
> // Adventure v.1 using DJGPP

> main()
> {
> FILE *fp;
> char ch;

Should be declared as an `int' since that's what getc() returns.

Quote:
> int x,y,z;
> int location[X_AXIS][Y_AXIS][Z_AXIS];
> clrscr();

Not a standard C function.

Quote:
> printf("Loading Data...\n");
> fp= fopen (FILE_NAME , "rb+");
> while ((ch=getc(fp)) !=EOF){
>   printf("%c",ch);
> }
> fclose(fp);

You just printed everything in "map.bin" to stdout.  Why?

Why are you printing a binary file to stdout in a presumably
interactive program?

Why did you close fp if you need to read from it later?

Why did you open fp for read and writing ("+") if you're not
going to write to it?

Quote:
> for(x=1;x<X_AXIS;x++){
>   for(y=1;y<Y_AXIS;y++){

x and y should probably start at 0.

Quote:
>     for(z=0;z<Z_AXIS;z++){
>     // Read data from binary "map.bin" into multidimensional array

So, what format is the data in?  If it's in the same format as it
should be in memory then you can drop the loops and just do
        fread (location, sizeof location, 1, fp);
(Add error checking.)

Otherwise, you must write code here to read something into location[x][y][z].

Quote:
>     }
>   }
> }
> return(0);
> }

--



Tue, 04 Mar 2003 03:00:00 GMT  
 Binary files and arrays - please help

: #define FILE_NAME "map.bin"
: // Adventure v.1 using DJGPP
:
: main()
: {
: FILE *fp;
: char ch;
: int x,y,z;
: int location[X_AXIS][Y_AXIS][Z_AXIS];
: clrscr();
: printf("Loading Data...\n");
: fp= fopen (FILE_NAME , "rb+");
: while ((ch=getc(fp)) !=EOF){
:   printf("%c",ch);
: }
: fclose(fp);

2) if you actually have a binary file, the only way to read the data is
EXACTLY as it was written.

B.
--

   Brian P. Hampson                  ASL Analytical Service Laboratories Ltd
   System Administrator,             Vancouver, BC (604)253-4188
      ----------------- http://www.ASL.CA/ ----------------------------  

I'm not speaking for the company <- They made me say that.
--



Tue, 04 Mar 2003 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. how to access binary files in c..please help

2. how to access binary files in C.please help

3. Help getting size of binary file for allocating pointer array

4. Write Binary File - Please Help !!

5. Binary file copy - help please

6. NEED HELP WITH PRITING AN ARRAY, PLEASE PLEASE HELP

7. Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!

8. HELP: Binary Large Objects (need to store binary file w/in database)

9. HELP: Binary Large Objects (need to store binary file w/in database)

10. Please help me store a file of ints into 3 seperate arrays

11. HELP Please Please (malloc on an array)

12. Please help!!!!Please help!!!!Please help!!!!

 

 
Powered by phpBB® Forum Software