473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

read a file into a memory block

mickey0
142 100+
Hello,
I'm trying to read a text fiel under Windows:
Expand|Select|Wrap|Line Numbers
  1. long size;
  2. ifstream file (name, ios::ate);    //open and goes at the end    
  3. size = file.tellg(); //it takes 181
  4. _buffer = new char [size];    
  5. file.seekg (0, ios::beg);    
  6. file.read (_buffer, size);
  7. size = file.gcount(); //it takes 168
  8. // _buffer[size] = '\0';
  9.  
I know that there are other way (stringstream) to do this, but to do exercise, I'd like to solve in this way. I know that Windows use CR + LF at the end of each line and the read take out (maybe) the LF; this explains the 168 vs 181; also I know that the read doesn't put the END delimiter at the end block memory;
My problem is start when I did "cout << _buffer;" and I saw at its end there were some strange characters that weren't in the file; so I thought that they were the dirty memory....
Then, how do I have to proceed, please? Do I have to set '\0'? But wich position at? Do I have to call memset() to clear to memory?
Some hints, please....
May 2 '08 #1
6 4740
RRick
463 Expert 256MB
Since your buffer is a char*, you will have to add a '\000' at the end. This is the marker that tells cout that it has reached the end of data. Cout will continue until it finds one of these markers. I suspect this caused the problem you described.

When you create the buffer you need an to add an extra character for the '\000'. What you need to do is: buffer = new char[size+1];

Another technique is use a fixed local buffer (i.e. char buffer[2000]). Since you pass the buffer size to read, 'read' will read that many characters at most and 'read' returns the number of characters actually read. You loop until read returns something less than the max buffer size. What to do with the buffer contents? Append them to a std::string. Now you don't have to seek or do the other io tricks. Nor do you need to worry about appending a '\000'.
May 2 '08 #2
mickey0
142 100+
Hello,
the problem is what I explained above about CR+LF too. So where do I have to put '\0' ? In which position? If I set it in _buffer[size+1] there are still strange characters within _buffer;

One more help?

(I know the string solution yet).
May 3 '08 #3
JosAH
11,448 Expert 8TB
You can read the file in 'binary' mode instead of 'text' mode. That way no folding
of '\r\n' to '\n' is done and you'll know exactly how large the buffer will be. Optionally
you can do the folding yourself afterwards and realloc the buffer to get rid of the
tail of the original buffer.

kind regards,

Jos
May 3 '08 #4
mickey0
142 100+
in binary mode size = file.tellg(); and size = file.gcount(); return the same number, but when I print the _buffer again there are strange characters at the end of _buffer...but doing this the stange chars doens't appear....

Expand|Select|Wrap|Line Numbers
  1.         ........................................
  2.     _buffer = new char [size+1];    
  3.     //memset( _buffer, 0, size );
  4.     file.seekg (0, ios::beg);    
  5.     file.read (_buffer, size);
  6.     _buffer[size] = '\0';
  7.     file.close();
  8.  
what does that mean???
BUT I don't have clear why, allocating one char more (size+1), I have to set '\0' on char at position 'size' (if I try _buffer[size+1] = '\0'; the program crash at runtime).

Thanks.
May 3 '08 #5
mickey0
142 100+
Hello,
I asssume that this above is the right solution (even without memset)
THanks
May 4 '08 #6
RRick
463 Expert 256MB
The '\000' character at the end of an array of characters, is standard C for describing strings. When you create a string like "Hello", you only see 5 characters, but the real storage requirements is 6 (5 chars + '\000'). That's why you want your buffer to be size + 1.

If your buffer is not large enough, you'll start overwriting other parts of your program. Sometimes you crash and burn and other times you don't.
May 5 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: picard | last post by:
I have seen in various posts that there are tricks to increasing the largest continuous memory block available to an application on a windows machine. I want to prove this is possible using a...
9
by: Alfonso Morra | last post by:
I have the following data types: typedef union { long l ; double f; char* s ; void* p ; } Value ; typedef struct {
40
by: Abby | last post by:
My .dat file will contain information like below. /////////// First 0x04 0x05 0x06 Second 0x07
5
by: Alfonso Morra | last post by:
Hi, I am writing a messaging library which will allow me to send a generic message structure with custom "payloads". In many cases, a message must store a non-linear data structure (i.e....
4
by: TT (Tom Tempelaere) | last post by:
Hi, In my project I need to use VirtualAlloc (kernel32) to allocate memory to ensure that data is 4K aligned. I need to fill the data block with file contents. I notice that there are no 'Read'...
3
by: jiing | last post by:
Now I am try to transfer a memory pointer between two threads. but there has a error mesage. _CrtIsValidHeapPointer(pUserData) in dbgheap.c I lookup google and find it's seems to be the local...
3
by: Rosario Morgan | last post by:
Hello Help is great appreciated in advance. I need to loop through a file 6000 bytes at a time. I was going to use the following but do not know how to advance through the file 6000 bytes at...
12
by: Frodo Baggins | last post by:
Hi I need to know the size of the memory block pointed to by a char* in a function receiving this pointer. Typically, this pointer points to a string. strlen() will not do the job since sometimes...
14
by: barcaroller | last post by:
I have a multi-field struct and a memory block (created using malloc) that contains structured data. If I map the struct to the memory block, am I guaranteed that the struct fields will be filled...
8
by: barcaroller | last post by:
I have a pointer to a memory block. Is there a way I can map a vector<Tto this memory block? I would like to take advantage of the powerful vector<T> member functions. Please note that I cannot...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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,...

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.