A
Angus
Hello
I am using ifstream to load the contents of a text file into a char*
variable. I am allocating using new with the file size as amount to
allocate. My problem is that the variable contains a load of misc
extraneous data at the end of the variable.
Here is the code.
int main(int argc, char* argv[])
{
std::ifstream myfile;
std::string line;
long begin,end;
char szFile[256];
char* memblock;
std::ifstream:os_type size;
std::cout << "Enter filename to open: ";
std::cin.getline(szFile, 256);
std::cout << "You selected file: " << szFile << std::endl;
myfile.open( szFile );
if (myfile.is_open())
{
begin = myfile.tellg();
myfile.seekg (0, std::ios::end);
end = myfile.tellg();
size = end-begin;
memblock = new char [size];
myfile.seekg (0, std::ios::beg);
myfile.read (memblock, size);
myfile.close();
std::cout << memblock << std::endl;
delete [] memblock;
}
return 0;
}
if I myfile.read just size then why do I get all the unallocated characters
at the end of the memblock variable?
Angus
I am using ifstream to load the contents of a text file into a char*
variable. I am allocating using new with the file size as amount to
allocate. My problem is that the variable contains a load of misc
extraneous data at the end of the variable.
Here is the code.
int main(int argc, char* argv[])
{
std::ifstream myfile;
std::string line;
long begin,end;
char szFile[256];
char* memblock;
std::ifstream:os_type size;
std::cout << "Enter filename to open: ";
std::cin.getline(szFile, 256);
std::cout << "You selected file: " << szFile << std::endl;
myfile.open( szFile );
if (myfile.is_open())
{
begin = myfile.tellg();
myfile.seekg (0, std::ios::end);
end = myfile.tellg();
size = end-begin;
memblock = new char [size];
myfile.seekg (0, std::ios::beg);
myfile.read (memblock, size);
myfile.close();
std::cout << memblock << std::endl;
delete [] memblock;
}
return 0;
}
if I myfile.read just size then why do I get all the unallocated characters
at the end of the memblock variable?
Angus