why fread does not work?

This is a discussion on why fread does not work? within the C Programming forums, part of the General Programming Boards category; Hi~ every one~ i have a piece of code here, But it does no work for the fread function. Can ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    7

    Question why fread does not work?

    Hi~ every one~ i have a piece of code here, But it does no work for the fread function. Can any one help me to figure it out ? thank you~(in the 50th line)
    Code:
     1 #include<stdio.h>
          2 #include<stdlib.h>
          3 #include<string.h>
          4
          5 typedef struct _HEADER {
          6
          7 int num1;
          8 int num2;
          9 int num3;
         10 int num4;
         11
         12 }HEADER;
         13
         14
         15 void readheader(HEADER *,FILE *);
         16 void writeheader(HEADER *,FILE *);
         17 short**** construct_4D(int dimension1, int dimension2, int dimension3, int dimension4);
         18
         19 int main(int argc,char *argv[])
         20 {
         21
         22
         23
         24 int i,j;
         25 HEADER header;
         26 short ****datatable;
         27
         28 datatable = (short ****)construct_4D(2,2,3,4);
         29
         30 /*if(argc  !=2){
         31 printf("Cannot open the file for read\n");
         32 }*/
         33
         34
         35 FILE *fp1=fopen(argv[1],"r");
         36 FILE *fp2=fopen("newdata.dat","w");
         37
         38  printf("The processing file is %s \n",argv[1]);
         39
         40  for (i=0;i<2;i++){
         41  for(j=0; j<2; j++){
         42  datatable[i][j] = (short **)construct_4D(2,2,3,4);
         43  }
         44  }
         45
         46
         47
         48 readheader(&header,fp1);
         49
         50 for(i=0; i<2 ;i++){
         51 for (j=0; j<4; j++)fread(&datatable[i][j][0][0],sizeof(short),6,fp1);
         52  }
         53  fclose( fp1 );
       54
         55  if ( fclose( fp1 ) == 0 )
         56
         57  {
         58  printf("Success to close input file\n");
         59  }
         60
         61 writeheader(&header,fp2);
         62
         63 printf("Can we get here?\n");
         64
         65
         66  if(ferror(fp2)) {
         67        printf("File2 Error\n");
         68        exit(1);
         69                  }
         70  if ( fclose( fp2 ) == 0 )
         71 { printf("Success to close output file\n");
         72 }
         73 return(0);
         74 }
         75
         76
         77
         78
         79 void readheader(HEADER *header,FILE *fp1)
         80 {
         81 printf("Here is the readheader function\n");
         82 fread(&(header->num1),sizeof(int),1,fp1);
         83 fread(&(header->num2),sizeof(header->num2),1,fp1);
         84 fread(&(header->num3),sizeof(int),1,fp1);
         85 fread(&(header->num4),sizeof(int),1,fp1);
         86 }
         87
         88 short**** construct_4D(int dimension1, int dimension2, int dimension3, int dimension4){
         89     short**** result;
         90     int i1, i2, i3;
         91     result = (short****)malloc(sizeof(short***)*dimension1);
         92     for (i1 = 0; i1 < dimension1; i1++){
         93         result[i1] = (short***)malloc(sizeof(short**)*dimension2);
         94         for (i2 = 0; i2 < dimension2; i2++){
         95             result[i1][i2] = (short**)malloc(sizeof(short*)*dimension3);
         96             for (i3 = 0; i3 < dimension3; i3++){
         97                 result[i1][i2][i3] = (short*)malloc(sizeof(short)*dimension4);
         98             }
         99         }
        100     }
        101     return result;
        102 }

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,172
    Do the fopen-s succeed? You never check.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    7
    yes , fopen works

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,172
    So explain not working. What happens? Crash? Bad results?
    The bad thing occurs at line 51?
    Also, this code is bad, if not altogether wrong:

    datatable[i][j] = (short **)construct_4D(2,2,3,4);

    You are allocating a 4D-array, yet truncating it to a 2D-array.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,339
    1. remove all those casts of malloc, see the FAQ.

    > datatable[i][j] = (short **)construct_4D(2,2,3,4);
    What???
    You've already built a 4-D array, why are you doing it again?

    > fread(&datatable[i][j][0][0],sizeof(short),6,fp1);
    1. This assumes that the two inner dimensions are contiguous - they aren't.
    Only the innermost array is contiguous.
    2. Where did 6 come from?

    I mean, from construct_4D(2,2,3,4); you use 2 and 4 (boogle) in the controlling for loop, so I suppose 6 comes from the remaining 2 and 3.

    Basically, the array you're reading into is completely the wrong shape.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using fread on stdin
    By nadroj in forum C Programming
    Replies: 29
    Last Post: 10-23-2008, 02:03 PM
  2. Another link from Microsoft about bug in fread
    By vart in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 05-06-2008, 11:56 AM
  3. How to load pixels of BMP into an array
    By brconner in forum Windows Programming
    Replies: 10
    Last Post: 06-02-2007, 04:30 AM
  4. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  5. fread doesnt work
    By condorx in forum C Programming
    Replies: 2
    Last Post: 06-05-2002, 02:36 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21