Hello.
I have a question about dynamically allocating enough memory space to read data/text from a file no matter if the user wants to read one line or the entire file.
For example let say I have a text file with 10 lines to text:
aaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbb
cccccccccccccccccccccccccc
ddddddddddddddddddd
eeeeeeeeeeeeeeeeee
ffffffffffffffffffffffffffffffffff
gggggggggggggggg
hhhhhhhhhhhhhhh
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
jjjjjjjjjjjjjjjjjjjjjjjjjj
1) Let say I want to read the first line so I could make changes to it (in memory first of course). Is there a way to dynamically allocate enough memory so the entire line would fit in a pointer to char? I can allocate the memory space, but the problem is I do not know the size to start out.
2) Let say I want to read the entire file (everything). Is there a way to dynamically allocate enough memory so the entire file would fit in a pointer to char?
Ex:
char *ptr;
ptr = new char[????] <------- what size relative to the lengh of the line or entire file?
Thanks,
Kuphryn
I have a question about dynamically allocating enough memory space to read data/text from a file no matter if the user wants to read one line or the entire file.
For example let say I have a text file with 10 lines to text:
aaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbb
cccccccccccccccccccccccccc
ddddddddddddddddddd
eeeeeeeeeeeeeeeeee
ffffffffffffffffffffffffffffffffff
gggggggggggggggg
hhhhhhhhhhhhhhh
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
jjjjjjjjjjjjjjjjjjjjjjjjjj
1) Let say I want to read the first line so I could make changes to it (in memory first of course). Is there a way to dynamically allocate enough memory so the entire line would fit in a pointer to char? I can allocate the memory space, but the problem is I do not know the size to start out.
2) Let say I want to read the entire file (everything). Is there a way to dynamically allocate enough memory so the entire file would fit in a pointer to char?
Ex:
char *ptr;
ptr = new char[????] <------- what size relative to the lengh of the line or entire file?
Thanks,
Kuphryn