473,387 Members | 1,553 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.

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-
Jul 19 '05 #1
2 6712


Krzysztof Kolago wrote:

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!


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
kb******@gascad.at
Jul 19 '05 #2
Uzytkownik "Karl Heinz Buchegger" <kb******@gascad.at> napisal w wiadomosci
news:3F***************@gascad.at...


Krzysztof Kolago wrote:

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!


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
kb******@gascad.at


Thanks! It really works!

Chris
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andy | last post by:
Hi, In the code below (not pretty I know but it's an early version :-P) I'm having problems reading the data object back in. If I move the reading code to immediately after the section where it...
1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
3
by: SB | last post by:
Hello. I have an input file which is laid out in the following manner... Name Day 1 am time 1 am time 2 appointment pm time 1 pm time 2 appointment Day 2
1
by: Magnus | last post by:
allrite folks, got some questions here... 1) LAY-OUT OF REPORTS How is it possible to fundamentaly change the lay-out/form of a report in access? I dont really know it that "difficult", but...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
4
by: Amit Maheshwari | last post by:
I need to read text file having data either comma seperated or tab seperated or any custom seperator and convert into a DataSet in C# . I tried Microsoft Text Driver and Microsoft.Jet.OLEDB.4.0...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
5
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing...
13
by: swetha | last post by:
HI Every1, I have a problem in reading a binary file. Actually i want a C program which reads in the data from a file which is in binary format and i want to update values in it. The file...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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...

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.