array help

  • Thread starter justin_davies_2002
  • Start date
J

justin_davies_2002

i need to read a text file into a character array, so i used an example
fom taken from a cplusplus.com tutorial " Input/ output with files"
(seebelow). The Problem i get is, it only makes a buffer array which
can hold 4 characters when the example text file i created is 30
characters long.
Any Help would be great!!

// reading binary file
#include <iostream.h>
#include <fstream.h>

const char * filename = "example.txt";

int main () {
char * buffer;
long size;
ifstream file (filename, ios::in|ios::binary|ios::ate);
size = file.tellg();
file.seekg (0, ios::beg);
buffer = new char [size];
file.read (buffer, size);
file.close();

cout << "the complete file is in a buffer";

delete[] buffer;
return 0;
}
 
V

Victor Bazarov

i need to read a text file into a character array, so i used an example
fom taken from a cplusplus.com tutorial " Input/ output with files"
(seebelow). The Problem i get is, it only makes a buffer array which
can hold 4 characters when the example text file i created is 30
characters long.

How do you know your buffer can only hold 4 characters? What's the value
of 'size' you get from calling 'file.tellg()'?
Any Help would be great!!

// reading binary file
#include <iostream.h>
#include <fstream.h>

const char * filename = "example.txt";

int main () {
char * buffer;
long size;
ifstream file (filename, ios::in|ios::binary|ios::ate);
size = file.tellg();
file.seekg (0, ios::beg);
buffer = new char [size];
file.read (buffer, size);
file.close();

cout << "the complete file is in a buffer";

delete[] buffer;
return 0;
}


V
 
J

j2002

when file.tellg(); is called it returns a value of 30, but when this is
then used to create an array, the array size is only 4. I changed
the example to buffer = new char [30];
but that would also only create an array of size 4
 
V

Victor Bazarov

j2002 said:
when file.tellg(); is called it returns a value of 30, but when this is
then used to create an array, the array size is only 4. I changed
the example to buffer = new char [30];
but that would also only create an array of size 4

I think you're confused. I am guessing here, but if you're using 'sizeof'
on a pointer, don't, it will NOT return the size of the array the pointer
points to. It will return the size of the pointer, which on your system
is _always_ 4 bytes. So, don't use 'sizeof' to try to see how many bytes
were actually allocated. Trust the system and presume that they were
allocated successfully since you asked. When allocation fails you will
get an exception.

V
 
J

j2002

to see what numbers where generated and what was in the array i changed
the program to;
#include <iostream.h>
#include <fstream.h>

const char* filename = "example.txt";
int main()
{
char * buffer;
long size;
ifstream file (filename, ios::in|ios::binary|ios::ate);
size = file.tellg();
cout << "The size of the file is " << size << "\n";
file.seekg(0, ios::beg);
buffer = new char [size];
file.read (buffer, size);
cout << "the size of the buffer is " << sizeof buffer <<"\n";
file.close();
cout << "the complete file is in the buffer\n";
for (int c =0; c < sizeof buffer; c++)
{
cout << buffer[c];
cout << "\n";
}
delete[]buffer;
return 0;
}
 
M

Malte Starostik

j2002 said:
when file.tellg(); is called it returns a value of 30, but when this is
then used to create an array, the array size is only 4. I changed
the example to buffer = new char [30];
but that would also only create an array of size 4

This looks like you're taking the size of the _pointer_ and not the
array. There is no way to retrieve the size of a dynamically allocated
array other than by remembering it. That's only one of the reasons why
std::vector (and in some cases std::string for char arrays) is both
easier and safer to use than a raw array.
Also note that iostream.h and fstream.h are outdated. Use iostream and
fstream instead (note: no .h) and read about namespace std.
I've only briefly skimmed over the tutorial on cplusplus.com, but it
definately fails in that regard, those headers have been obsoleted by
the 1998(!) C++ Standard.

Cheers,
Malte
 
R

Ron Natalie

Victor said:
How do you know your buffer can only hold 4 characters? What's the value
of 'size' you get from calling 'file.tellg()'?

In additions to your confusion over sizeof, tellg() doesn't necessarily
return a count of characters. It's a magic value that's really only
useful for passing back to seekpos. The position may have extra information
in it in the case of multibyte encodings.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,701
Latest member
XavierQ83

Latest Threads

Top