Bad Ptr on char pointer

Helo everybody,
I'm trying to read the contents of a file using binary mode. I am using the code from the tutorial with the add of displaying the contents of the file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// reading a complete binary file
#include <iostream>
#include <fstream>
using namespace std;
int main () {
  ifstream::pos_type size;
  char * memblock;
  ifstream file ("example.txt", ios::in|ios::binary|ios::ate);
  if (file.is_open())
  {
    size = file.tellg();
    memblock = new char [size];
    file.seekg (0, ios::beg);
    file.read (memblock, size);
    file.close();

    cout << "the complete file content is in memory\n";
	cout << memblock << endl;

    delete[] memblock;
  }
  else cout << "Unable to open file";
  return 0;
}

And the contents of example.txt are:

hello, my name is dimitris
this is just a test...
:D


when i run this I have the following output:

the complete file content is in memory
hello, my name is dimitris
this is just a test...
:D²²²²ττττττττΌ■Ό■Ό■

What are al those garbage at the end?

I tried to debug it and when it comes to the char * memblock i have the folllowing message: Bad Ptr, Error:Expression cannot be evaluated.
This seems to happen with every dynamic pointer of char type i use. Even on the simplest:
1
2
3
4
5
6
#include <iostream>
using namespace std;
int main(){
  char * temp;
  return 0;
}

Even on this i have the bad ptr message...


Can somebody explain me what's wrong with that?
Thank you in advance...
The read function does not guarantee to terminate strings with a null character.

So you should allocate enough memory to hold the contents of the file and the null character;

memblock = new char [size+1];

It would also be a good idea to zero the entire block before calling read(), just in case errors occur. You can
#include <algorithm>
and zero it with:

fill_n( memblock, size+1, '\0' );

Then read and print as usual.

Hope this helps.

[edit] fixed the stupid fill name
Last edited on
No, it doesn't work, acctually it doesn't compile at all with the fill.
Take a look at this:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;
int main(){
	char * temp;
	temp = new char[5];
	temp[0] = 'a';
	temp[1] = 's';
	temp[2] = 'd';
	temp[3] = 'f';
	temp[4] = 'g';
	cout << temp;
	system("pause");
}


You would expect a result like: "asdfg",
but the resultof that program is "asdfg²²²²ττττττττ■Ό■Ό■Ό■"
Why does it allocates 14 more chars positions?

If i manually terminate the sting then it works fine. I think i'm gonna use i like that, thanx.
Last edited on
No you wouldn't get "asdfg" with that code. operations like cout << when passed a char * keep printing characters until they find a '\0' termination character. So if you haven't put one at the end of your string, it will run off the end of your array and keep stepping through memory until it either finds a string termination or the program crashes.
you need something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;
int main(){
	char * temp;
	temp = new char[6];
	temp[0] = 'a';
	temp[1] = 's';
	temp[2] = 'd';
	temp[3] = 'f';
	temp[4] = 'g';
        temp[5] = '\0';
	cout << temp;
	system("pause");
}
Ok, i understand it know.
Thank you very much.
You know, for once I wish people would take the time to actually google the documentation for stuff they are advised if (*gasp*) it doesn't work right away.

I also wish that people would take the time to re-read the answer given them rather than assuming they got it right the first time and simply regurgitating the original question again, except maybe a little more loudly.

Sorry I misspelled the name of the routine, but whether you believe it or not, I'm telling you right now that it is a good idea to zero the entire block before calling read(), just in case errors occur.

Otherwise when things start going bump in the night you'll have no one to blame but yourself.


Now don't go and stop posting here just because I've admonished you. I make mistakes with the best of them, and I'll continue to be courteous and friendly for any future questions you may ask.
Last edited on
Yes, you are completly right that i could google it and find out that i was wrong. I'm just used in programms like Actionscript and PHP that doens't need all those staff that make c++ so difficult and the same time so much much more powerfull. That's why even something like that seems very weird to me. I'm glad you tried to help me, and everybody that helps me whenevver i have problem. I really appriciate this.
I won't stop posting when i have a probem, i will just look for the cause of it a little deeper. Thanx for your advice.
Hmm, yeah, sorry I was a little grouchy.

The real trick with C++ is to learn to read the documentation, and to know where to look. It takes everyone a while to get used to it. This site is an excellent documentation source and has a very good collection of tutorials. Another good spot to peruse is http://www.cprogramming.com/tutorial.html

For more advanced stuff with the STL
HP's SGI site: http://www.sgi.com/tech/stl/
The Function Pointer Tutorials: http://www.newty.de/fpt/index.html

We'll be here to help you out as you go, and you'll do fine.


BTW, have you seen Grey Wolf's Sudoku program challenge in the Lounge? http://www.cplusplus.com/forum/lounge/2299/
Thank you for the informations you gave me, I'll try to find as much as possible from those tutorials.

The Grey Wolf's Sudocu program challenge is a very good idea and i have thought about it in the past, but I am doing all this staff just for fun (t's my hobbie :P) and I don't think I have right know the time to think all those staff.

I was wondering if you know any easy way to connect to a server via ftp? I'm trying to do an antispam program and I don't know how to connect to the server to retrieve the mail file and then put it back. If you have any information I would be very glad.
Thank you.
Playing with sockets is not fun in C++. If I were you I'd google around to see if anyone has written a library to do it for you.

Maybe someone else here can actually give a good link...

Good luck!
The code project site is a good bet, I've found two with a quick search
http://www.codeproject.com/KB/IP/ftpclientclass.aspx
and
http://www.codeproject.com/KB/IP/TJFTP.aspx
Topic archived. No new replies allowed.