By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

array help

P: n/a
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;
}

Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
ju****************@hotmail.com wrote:
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
Jul 23 '05 #2

P: n/a
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

Jul 23 '05 #3

P: n/a
j2002 wrote:
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
Jul 23 '05 #4

P: n/a
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;
}

Jul 23 '05 #5

P: n/a
j2002 schrieb:
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
Jul 23 '05 #6

P: n/a
thanks the problem is sloved now and the program works thanks

Jul 23 '05 #7

P: n/a
Victor Bazarov wrote:
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.
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.