Hey guys,

I am writing a C++ program with the help of dynamic allocation of a character array. The size of the array depends on the size of file read. However, when I checked the size of allocated array, it is always 4! So, is there any limit on this kind of allocations? Or is there any problem about my program? Thanks a lot for your help!

ps. I need to read in JPG or BMP files (max. 400KB), is this algorithm appropriate indeed?

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main() {
  int bufflen;
  char* buffer;

..............

  ifstream filein ("buffer.txt", ios::in); // open a file to read in
  if (filein.is_open() != TRUE){
    cout << "ERROR: Fail to open data\n";
    exit(1);
  }
  filein.seekg (0, ios::end); // get length of file
  bufflen = filein.tellg(); // int, value should be over 30000
  buffer = new char [bufflen]; // allocate character array dynamically
  filein.seekg (0, ios::beg);
  filein.get (buffer, sizeof(buffer)); // read data into my allocated array

..............

delete[] buffer;
return 0;
}

Recommended Answers

All 8 Replies

The sizeof() operator is giving you the size of the pointer named "buffer", which is 32 bits (4 bytes).

Just use bufflen for the size of the buffer in the get() call.

Thanks jonsca. The problem of array size is solved.

But there is another problem: I tried to transfer the file using a TCP socket, but the received file seems corrupted. I guess there should be no problem for the socket codes, since it works fine with TXT files and STATIC memory allocation. Is there any problem with my algorithm of reading and buffering file data? Or do I have to set the dynamic array size to be "bufflen + 1"?

Please help, thanks a lot!

If you had a fixed-sized array, you can use sizeof:

char a[5];
cout<<sizeof(a); //5

but if you were to pass it into a function as a pointer, that information is lost (as the array is reduced to a pointer).

void myfunc(char a[])
{
   cout<<sizeof(a);   //4
}

int main()
{
  char b[5];
  cout<<sizeof(b); //5
  myfunc(b);

}

But there is another problem: I tried to transfer the file using a TCP socket, but the received file seems corrupted

You could have put that in the question, too. lol. No worries. Try changing your second seekg call on line 20 to a rewind http://www.cplusplus.com/reference/clibrary/cstdio/rewind/. That will bring you back to the beginning and reset the error flag (just seeking won't help the fact that the stream is invalid due to reading to the end of the file on the first run).

You could have put that in the question, too. lol. No worries. Try changing your second seekg call on line 20 to a rewind http://www.cplusplus.com/reference/clibrary/cstdio/rewind/. That will bring you back to the beginning and reset the error flag (just seeking won't help the fact that the stream is invalid due to reading to the end of the file on the first run).

Sorry that I didn't include it because I discovered this after setting the dynamic allocation right.
Anyway I was declaring a file input stream instead of a file object in my program. I guess they should not be compactable, right? BTW I just think of using gcount() instead of 2 seekg(). Any advice on this? Thanks a lot!

You can use the ifstream as you are doing, it's a more C++ way of doing it than calling fopen is.

gcount() might work to tell you the number of characters you are going to send, as that will tell you how many characters you actually read in. It wouldn't buy you much with the seekg, as you'd have to "unget" all of the characters you just got.

I was suggesting rewind because it takes care of everything for you.

I just found out why the image is corrupted. LOL
Originally I used ifstream filein(char filename, ios::in) and ofstream fileout(char filename, ios::out), but now I changed them to ifstream filein(char filename, ios::binary) and ofstream fileout(char filename, ios::binary), and it now works with JPG files.
Anyway thanks a lot for your help, jonsca! :-)

You're welcome. You didn't say you were working with jpegs, I assumed you were sending text. Anyway, glad it's working.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.