473,508 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read a binary file

I used to program in matlab
An read an image file with :
fread(a,[256,256],0)
which gave me a matrix of 256*256 full of numbers which were the pidxel
values from 0 to 256.
How do i do the same in c++

i read with fin.read((char *) &pl sizeof pl)
which brings out the text in the file .
But the matrix of numbers just appears as nothing.

Could comeone please offer suggestions / tell me how to do it .
Max
Jul 23 '05 #1
5 8258
maxwell wrote:
I used to program in matlab
An read an image file with :
fread(a,[256,256],0)
which gave me a matrix of 256*256 full of numbers which were the pidxel
values from 0 to 256.
How do i do the same in c++

i read with fin.read((char *) &pl sizeof pl)
which brings out the text in the file .
But the matrix of numbers just appears as nothing.


What matrix? Show more code.

Jul 23 '05 #2
maxwell wrote:
I used to program in matlab
An read an image file with :
fread(a,[256,256],0)
which gave me a matrix of 256*256 full of numbers which were the pidxel
values from 0 to 256.
How do i do the same in c++

i read with fin.read((char *) &pl sizeof pl)
which brings out the text in the file .
But the matrix of numbers just appears as nothing.

Could comeone please offer suggestions / tell me how to do it .
Max


#include <iostream>
#include <fstream>
using namespace std;

int main(void)
{
const unsigned int Matrix_Rows = 256;
const unsigned int Matrix_Cols = 256;
unsigned char area_matrix[Matrix_Rows * Matrix_Cols];
unsigned char pointers_matrix[Matrix_Rows][Matrix_Cols];
ifstream data_file("filename", ios::binary);

/* Reading an area of bytes */
data_file.read(area_matrix, sizeof(area_matrix));

/* Rewinding the input file */
data_file.clear(); // just in case.
data_file.seekg(0);

/* Reading a two-dimensional array of bytes */
for (unsigned int i = 0; i < Matrix_Rows; ++i)
{
data_file.read(pointers_matrix[i],
Matrix_Cols);
}

return 0;
}

The O.P. must check for errors and validate the
reading of data as column-wise or row-wise.

A matrix can be an area of contiguous bytes as
declared by "area_matrix" above, or as an array
of arrays of char, as declared by "pointers_matrix".
I don't trust the compilers to place all the
char arrays in pointers_matrix contigously.

The structure does allow for the arrays to be
located in different areas in memory:
pointers_matrix[0]
some variables
pointers_matrix[1]
other variables
pointers_matrix[2]
pointers_matrix[3]
more variables
pointers_matrix[4]
... etc ...

In summary, read data into a matrix depends on
the definition or structure of the matrix.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Jul 23 '05 #3
Thomas Matthews wrote:
maxwell wrote:
I used to program in matlab
An read an image file with :
fread(a,[256,256],0)
which gave me a matrix of 256*256 full of numbers which were the pidxel
values from 0 to 256.
How do i do the same in c++

i read with fin.read((char *) &pl sizeof pl)
which brings out the text in the file .
But the matrix of numbers just appears as nothing.

Could comeone please offer suggestions / tell me how to do it .
Max


#include <iostream>
#include <fstream>
using namespace std;

int main(void)
{
const unsigned int Matrix_Rows = 256;
const unsigned int Matrix_Cols = 256;


Why unsigned?

Jul 23 '05 #4
Rolf Magnus schrieb:
Thomas Matthews wrote:
int main(void)
{
const unsigned int Matrix_Rows = 256;
const unsigned int Matrix_Cols = 256;

Why unsigned?


Why not?
Jul 23 '05 #5
Rolf Magnus wrote:
Thomas Matthews wrote:

maxwell wrote:
I used to program in matlab
An read an image file with :
fread(a,[256,256],0)
which gave me a matrix of 256*256 full of numbers which were the pidxel
values from 0 to 256.
How do i do the same in c++

i read with fin.read((char *) &pl sizeof pl)
which brings out the text in the file .
But the matrix of numbers just appears as nothing.

Could comeone please offer suggestions / tell me how to do it .
Max


#include <iostream>
#include <fstream>
using namespace std;

int main(void)
{
const unsigned int Matrix_Rows = 256;
const unsigned int Matrix_Cols = 256;

Why unsigned?

Because one can't have negative columns or rows in a matrix.

Although, I guess it all depends on where column or row zero
is logically placed.

Or perhaps it is because my fingers tend to type "unsigned"
a lot. ;-)

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
Jul 23 '05 #6

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

Similar topics

17
10458
by: Guyon Morée | last post by:
what is the difference? if I open a text file in binary (rb) mode, it doesn't matter... the read() output is the same.
7
5037
by: KantKwitDansin1 | last post by:
I have a file "a.dat" containing 10^4 32 bit binary numbers. I need to read in these numbers and deterimine if they are prime. The primality part I will have to figure out later, but I was...
3
18953
by: nicolasg | last post by:
Hi, I'm trying to open a file (any file) in binary mode and save it inside a new text file. After that I want to read the source from the text file and save it back to the disk with its...
1
2250
by: Quinn | last post by:
Hi all, I have some binary files in the following format: text line 1 text line 2 .... text line N end of text single in binary 1 single in binary 2 single N EOF
3
18416
by: utab | last post by:
Dear all, What are the advantages of binary files over text files? I would like to search for a specific value of a variable in an output file, I was doing this lately by the string library...
6
5780
by: =?Utf-8?B?VGhvbWFzWg==?= | last post by:
Hi, Is it possible to read a file in reverse and only get the last 100 bytes in the file without reading the whole file from the begining? I have to get info from files that are in the last 100...
6
7352
by: ericunfuk | last post by:
Hi ALL, I want to read a binary file(it's pic.tif file, I guess it's binary file?), then write it to a new file), I have several questions about this process: When I use fread() to read a...
6
32846
by: Thomas Kowalski | last post by:
Hi, currently I am reading a huge (about 10-100 MB) text-file line by line using fstreams and getline. I wonder whether there is a faster way to read a file line by line (with std::string line)....
4
3459
by: Matrixinline | last post by:
Hi All Here is my problem I am using a Unicode project and I tried to read the File like sPath = LPCTSTR; FILE* oFp = _tfopen(sPath,L"r"); while(!feof(oFp))
6
342
by: zl2k | last post by:
hi, there I have a appendable binary file of complex data structure named data.bin created by myself. It is written in the following format: number of Data, Data array Suppose I have...
0
7231
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
7132
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...
1
7063
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...
0
5640
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,...
1
5059
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4720
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1568
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 ...
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
432
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...

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.