Solved

C++ Language question

Posted on 1997-07-09
4
184 Views
Last Modified: 2008-03-06
I have been since recently in C++-programming after
several years of turbo pascal. Now I've encountered a
problem that I just can't seem to solve.
I used in tp the function blockread to read several bytes
at once in a memory block assigned by memalloc.

the class I use in c++ is that of ifstream to open the
file.
reserve x number of 16-bytes paragraphs with memalloc.

The thing I want to do is read n bytes in one time out of
the file opened by ifstream and place them in the buffer
assigned by memalloc.

someone knows how to solve this problem ?

0
Comment
Question by:berox
  • 2
4 Comments
 
LVL 4

Expert Comment

by:AVaulin
ID: 1164599
Use FILE structure and fopen, fread and fclose functions instead ifstream class. This way is the easiest.
0
 

Author Comment

by:berox
ID: 1164600
still remains the question how can I put them in a buffer allocated by memalloc and (I forgot to put it in my former question)is there a function to read the memory locations reserved by memalloc (something like Mem[Seg:ofs] in TP), or is it just "pointer-work"
0
 
LVL 4

Expert Comment

by:AVaulin
ID: 1164601
Example:

struct YourStuct
{
 int int1;
 floaf float1;
 char str[20];
}

void YourFunction( void )
{
 YourStuct* s;
 FILE* fp;

 s = new YourStuct [20];
 fp = fopen( "yourfile", "rb" );
 fread( s, sizeof(YourStuct), 20, fp );
 fclose( fp );

// do wht you want with s

 delete s;
}
0
 
LVL 1

Accepted Solution

by:
shappir earned 50 total points
ID: 1164602
Hi,

You can read a block directly from an ifstream.
First you probably want to open it in binary mode (using the ios::binary flag) as at appears from your question that you want to read the data as bytes not as text.
Use the get() member function to perform the acctual read operation:

char buffer[SIZE];
ifstream if("data.dat", ios::in | ios::binary);
if.get(buffer, sizeof(buffer));

Note:
1. If didn't check that the file was opened OK to make the code simpler.
2. No need to explicitly close the file -- the stream's destructor does that for you.
 

0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now