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:
// reading a complete binary file
#include <iostream>
#include <fstream>
usingnamespace 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 = newchar [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:
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
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.
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.
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
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.