Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,968 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,378 people online right now. Registration is fast and FREE... Join Now!




buffer problem

 
Reply to this topicStart new topic

buffer problem

tootypegs
8 Jan, 2008 - 10:17 AM
Post #1

D.I.C Head
**

Joined: 9 Oct, 2007
Posts: 177


My Contributions
Hi i have the following piece of code which should just write from a file into a buffer however for some reason i keep getting errors returned from the declaration of my buffer but i cnt figure out why. My errors are:

1) Constant expression require in function main()
2) Cannot convert unsigned char to const char
3) type mismatch

Can anyone see where ive gone wrong, im guessing it will be something stoopid

thanks

CODE


f.seekp (start_byte,std::ios::beg);

char buffer[Size_of_the_file];
    
f.write (&buffer,sizeof (buffer));



User is offlineProfile CardPM
+Quote Post

Tom9729
RE: Buffer Problem
8 Jan, 2008 - 12:30 PM
Post #2

Debian guru
Group Icon

Joined: 30 Dec, 2007
Posts: 1,591



Thanked: 12 times
Dream Kudos: 325
My Contributions
I think we'll need to see more than that to help you. smile.gif
User is online!Profile CardPM
+Quote Post

tootypegs
RE: Buffer Problem
8 Jan, 2008 - 12:46 PM
Post #3

D.I.C Head
**

Joined: 9 Oct, 2007
Posts: 177


My Contributions
QUOTE(Tom9729 @ 8 Jan, 2008 - 01:30 PM) *

I think we'll need to see more than that to help you. smile.gif




Hi in my attempt to make something work ive changedmy code but yet i still have the same problem with my buffer. 'Size_of_the_file' is just a straight forward int that i declard a value of earlier in my file.
I now get the error

Constant expression required in function main.

CODE


char buffer[Size_of_the_file];

ifstream myFile ("C:\\bollock\\usbimage2.txt", ios::in | ios::binary);
myFile.seekg (start_byte,std::ios::beg);
    myFile.read (buffer, 100);

  fstream myFile2 ("new.txt", ios::out | ios::binary);
    myFile2.write (buffer, 100);



User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Buffer Problem
8 Jan, 2008 - 12:59 PM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Here is a nice basic example that should give you some guidance. Pay attention to how I have defined an array of char pointers and used that for the buffer to read into. You can't use a standard variable name when defining a char string.

CODE

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

int main() {

    // Oopen the file as an ifstream
    ifstream f("C:\\bollock\\usbimage2.txt", ios::in | ios::binary);

    // Get the size
    ifstream::pos_type x = f.tellg();

    // Notice we make a pointer of certain char size.
    // You can't use variables as subscript unless it is done this way.
    char *buffer = new char[x];

    // Read into the buffer (notice no ampersand, this was the pointer problem)
    f.read(buffer,sizeof(buffer));
    f.close();

    // Use an output stream and write out, again no ampersand
    ofstream g("C:\\bollock\\new.txt", ios::out | ios::binary);
    g.write(buffer, sizeof(buffer));
    g.close();


    return 0;
}


Hope this is useful. Enjoy!

"At DIC we be buffer reading/writing code ninjas!" decap.gif
User is offlineProfile CardPM
+Quote Post

tootypegs
RE: Buffer Problem
8 Jan, 2008 - 01:21 PM
Post #5

D.I.C Head
**

Joined: 9 Oct, 2007
Posts: 177


My Contributions
QUOTE


Hope this is useful. Enjoy!

"At DIC we be buffer reading/writing code ninjas!" decap.gif


haha, thank you very much for your advice

can i just check that if i want to declare buffer sizes with a predefined int then it should go something like........where Size_of_the_file is the predefined int.

CODE


char *buffer = new char[Size_of_the_file];



.................can i just ask one more question, i feel bad because u's have given me soo much help so far but at least il learn from my rubbishness.

Ive implimented everything as you have sed and it streams to the output file, however i know Size_of_the_file has a value of 2000 however when i run the program the buffer only prints 4 characters to the output file?

This post has been edited by tootypegs: 8 Jan, 2008 - 01:34 PM
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Buffer Problem
8 Jan, 2008 - 02:28 PM
Post #6

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
The sizeof operator, when put to use on a dynamically allocated array like this, only returns the size of the pointer that points to the memory allocated to the array. In this case, the amount of memory required to store a char* is 4-bytes, so you're only outputting sizeof(char*)=4 bytes from the buffer.

If you want to output the full 2000 bytes of data, you need to use Size_of_the_file as the argument for the number of bytes you output. So it would be something like g.write(buffer, Size_of_the_file*sizeof(char)); to write the whole buffer to the file.

* edit - oops - sizeof(char) was a little redundant - should always be 1. But I like redundancy and repeating myself and saying the same thing more often than is necessary.

This post has been edited by jjhaag: 8 Jan, 2008 - 02:32 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Buffer Problem
8 Jan, 2008 - 02:38 PM
Post #7

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
jjhaag beat me to the punch, but here is my program updated to read the size of the file and then write it. I use seekg to move to the end of the file, grab the position, clear the pointer (because it is in an error state) restart at the beginning and then go about setting up the rest of the read write. Notice I also remove any mention of sizeof and just use the size read in.... aka the file size.

CODE


int main() {

    // Oopen the file as an ifstream
    ifstream f("C:\\bollock\\usbimage2.txt", ios::in);

    // Get the size
    f.seekg(0,ios::end);
    long x = f.tellg();
    f.clear();
    f.seekg(0,ios::beg);


    // Notice we make a pointer of certain char size.
    // You can't use variables as subscript unless it is done this way.
    char *buffer = new char[x];

    // Read into the buffer (notice no ampersand, this was the pointer problem)
    f.read(buffer,x);
    f.close();

    // Use an output stream and write out, again no ampersand
    ofstream g("C:\\bollock\\new.txt", ios::out);
    g.write(buffer, x);
    g.close();

    return 0;
}


My bad for not taking this further. smile.gif

This post has been edited by Martyr2: 8 Jan, 2008 - 02:39 PM
User is offlineProfile CardPM
+Quote Post

tootypegs
RE: Buffer Problem
8 Jan, 2008 - 02:53 PM
Post #8

D.I.C Head
**

Joined: 9 Oct, 2007
Posts: 177


My Contributions
Thanks for your help everyone it has been MUCH appreciated!!!!!!!! This forum is a great place!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 06:30PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month