?
Solved

C++ Language question

Posted on 1997-07-09
4
Medium Priority
?
193 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

752 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