Reading data from file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Krzysztof Kolago

    Reading data from file

    Hello!

    I wrote program that should read binary file, but it read only a part of
    it (13690 of 64KB). Maybe somebody can help me, and will tell me, how to
    modificate program source. I need to read all bytes of the file!

    My source:
    #include <string.h>
    #include <stdio.h>
    #include <fcntl.h>
    #include <io.h>

    main() {

    signed int Plik = open("tomo.raw" , O_RDONLY);
    unsigned char bufor[64*1024];
    int Tablica[64*1024];
    int dlugosc;

    if(Plik < 0) return(1);
    dlugosc = read(Plik,bufor ,sizeof(bufor)) ;
    for(int i = 0; i < dlugosc; i++) {Tablica[i] = bufor[i];}
    close(Plik);
    for(int i = 0; i < dlugosc; i++){printf("%i ", Tablica[i]);}
    printf("--- %i", dlugosc);
    }

    +Chris-


  • Karl Heinz Buchegger

    #2
    Re: Reading data from file



    Krzysztof Kolago wrote:[color=blue]
    >
    > Hello!
    >
    > I wrote program that should read binary file, but it read only a part of
    > it (13690 of 64KB). Maybe somebody can help me, and will tell me, how to
    > modificate program source. I need to read all bytes of the file!
    >[/color]

    Then read them!

    read() may read fewer bytes then requested. But that's not a problem,
    read() tells you how much could be read in one rush. Just subtract
    what read() has already read from twhat was requested and do another
    read() until you have read everything (read() returns 0 if end of file
    was reached or -1 if there was an error)

    ToRead = some_number;

    while( ( HaveRead = read( Plik, bufor, ToRead ) ) > 0 ) {
    // do something with the read bytes.
    ToRead -= some_number;
    }

    if( HaveRead == -1 )
    printf( "There was an error during read\n" );


    --
    Karl Heinz Buchegger
    kbuchegg@gascad .at

    Comment

    • Krzysztof Kolago

      #3
      Re: Reading data from file

      Uzytkownik "Karl Heinz Buchegger" <kbuchegg@gasca d.at> napisal w wiadomosci
      news:3FB4F500.D 6F63D65@gascad. at...[color=blue]
      >
      >
      > Krzysztof Kolago wrote:[color=green]
      > >
      > > Hello!
      > >
      > > I wrote program that should read binary file, but it read only a[/color][/color]
      part of[color=blue][color=green]
      > > it (13690 of 64KB). Maybe somebody can help me, and will tell me, how to
      > > modificate program source. I need to read all bytes of the file!
      > >[/color]
      >
      > Then read them!
      >
      > read() may read fewer bytes then requested. But that's not a problem,
      > read() tells you how much could be read in one rush. Just subtract
      > what read() has already read from twhat was requested and do another
      > read() until you have read everything (read() returns 0 if end of file
      > was reached or -1 if there was an error)
      >
      > ToRead = some_number;
      >
      > while( ( HaveRead = read( Plik, bufor, ToRead ) ) > 0 ) {
      > // do something with the read bytes.
      > ToRead -= some_number;
      > }
      >
      > if( HaveRead == -1 )
      > printf( "There was an error during read\n" );
      >
      >
      > --
      > Karl Heinz Buchegger
      > kbuchegg@gascad .at[/color]

      Thanks! It really works!

      Chris


      Comment

      Working...