0

I am triing to save a .txt file into an object(c++). the problem is i dont know how big it is.

in c i would do it with a evl with the malloc() function but i have no idea how to do that in c++ or how to google that issue =/

3
  • Probably you are beginning with C++. What tutorials have you tried? Commented May 5, 2011 at 15:40
  • If you can do it in C, you can do it the same way in C++. Commented May 5, 2011 at 15:41
  • 2
    What do you mean by "save a .txt file into an object"? Do you mean that you want to read its contents into some sort of object? Or you want a reference to that file in memory? We'll need more details on what you're actually trying to do. Commented May 5, 2011 at 15:43

5 Answers 5

2

Why not use std::ostringstream?

Or if you want to use an equivalent to malloc, use:

char *storage = new char[__size__];

 ....

delete[] storage;

But if your file is a binary file odds are you have a byte which is null. strlen won't work the way you expect it then.

You can also use std::string, std::vector<char> in which you can have any values and that can be converted to const char * easily.

Sign up to request clarification or add additional context in comments.

Comments

1

why won't you save it as a string in a field of type std::string? try:

myObj.someString = myFile.rdbuf();

Comments

0

You can use the new operator in C++, or better yet one of the standard library containers.

Comments

0

Try this: http://www.fredosaurus.com/notes-cpp/newdelete/50dynamalloc.html

Comments

0

Prefer using standard containers than raw dynamic allocation

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.