Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C++ Language question

Posted on 1997-07-09
4
Medium Priority
?
194 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

618 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