What I want to do is read a file called "test.txt", and then have the contents of the file be a type const char *. How would one do this?
Asked
Active
Viewed 1.7k times
2
-
Are you sure you need a `const char *`? If you need something to pass into a function that takes `const char *`, then you can just pass in a `char *` if that's more convenient for you. – Steve Jessop Oct 24 '10 at 20:18
-
possible duplicate of [What is the best way to slurp a file into a std::string in c++?](http://stackoverflow.com/questions/116038/what-is-the-best-way-to-slurp-a-file-into-a-stdstring-in-c) – Konrad Rudolph Oct 24 '10 at 20:19
3 Answers
7
#include <string>
#include <fstream>
int main()
{
std::string line,text;
std::ifstream in("test.txt");
while(std::getline(in, line))
{
text += line + "\n";
}
const char* data = text.c_str();
}
Be careful not to explicitly call delete on data

Armen Tsirunyan
- 130,161
- 59
- 324
- 434
-
would `std::string text; std::getline(in, text, EOF); const char* data = text.c_str();` work faster? – Mooing Duck Jul 05 '12 at 23:11
-
I was planning to add another solution that uses sstream instead of string but I found the solution by eq-, which I strongly recommend over this one as it is much more efficient as you here allocate and free the memory with each line. – Mostafa Hassan Jul 04 '17 at 16:16
6
It's highly unlikely you really want to do that. The contents of the file (which may be either text, or binary data) are unlikely to represent a (valid) pointer to a char on your architecture, so it is not really meaningful to represent it [the content] as a const char *
.
What you may instead want is to load the contents of the file in memory, and then store a pointer (of type const char*) to the beginning of the given block. </pedantry> One way of achieving that:
#include <sstream>
#include <fstream>
// ...
{
std::ostringstream sstream;
std::ifstream fs("test.txt");
sstream << fs.rdbuf();
const std::string str(sstream.str());
const char* ptr = str.c_str();
// ptr is the pointer we wanted - do note that it's only valid
// while str is valid (i.e. not after str goes out of scope)
}

eq-
- 9,986
- 36
- 38
-
Yeah. I mess this up every time, which is odd since I usually learn from my mistakes. (I use stringstreams rarely, and "every time" I refer to the wrong header.) Fixed. – eq- Jul 05 '12 at 23:06
-
`do note that it's only valid while str is valid (i.e. not after str goes out of scope)` -- +1 for this. I had a similar solution but I was returning a `const char* ptr` from a function and couldn't figure out why things wasn't working as expected. – Norman Breau Oct 29 '21 at 00:33
1
You need to:
- create a function returning a const char*
- open an
fstream
on the file - seek to its end
- determine the file length by looking at the file position (
tell
) - seek back to the beginning
- create a char* to contain the file contents
- read the file contents into the char*
- return the char* pointer, with the function return adding the const
- the file is closed automatically by the fstream going out of scope

Will
- 73,905
- 40
- 169
- 246