Solved

C++ Language question

Posted on 1997-07-09
4
189 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

861 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