Read text file into char Array. C++ ifstream
Asked Answered
T

5

6

Im trying to read the whole file.txt into a char array. But having some issues, suggestions please =]

ifstream infile;
infile.open("file.txt");

char getdata[10000]
while (!infile.eof()){
  infile.getline(getdata,sizeof(infile));
  // if i cout here it looks fine
  //cout << getdata << endl;
}

 //but this outputs the last half of the file + trash
 for (int i=0; i<10000; i++){
   cout << getdata[i]
 }
Tree answered 7/12, 2010 at 3:10 Comment(3)
Or maybe someone can suggest a better way to store a text file into a char array.Tree
If you do this in anything but a toy app ensure you put protections against unlimited memory allocation.Tombouctou
You seem to be missing some semicolons.Informal
U
1

Every time you read a new line you overwrite the old one. Keep an index variable i and use infile.read(getdata+i,1) then increment i.

Unbelief answered 7/12, 2010 at 3:19 Comment(2)
read(..., 1) reads one character at a time... very inefficient.Ketron
infile.seekg(0,ios::end);int len = infile.peekg();infile.seekg(0,ios::beg);infile.read(getdata,len);Unbelief
A
7
std::ifstream infile;
infile.open("Textfile.txt", std::ios::binary);
infile.seekg(0, std::ios::end);
size_t file_size_in_byte = infile.tellg();
std::vector<char> data; // used to store text data
data.resize(file_size_in_byte);
infile.seekg(0, std::ios::beg);
infile.read(&data[0], file_size_in_byte);
Ankylose answered 5/2, 2013 at 22:47 Comment(0)
O
3

Use std::string:

std::string contents;

contents.assign(std::istreambuf_iterator<char>(infile),
                std::istreambuf_iterator<char>());
Oboe answered 7/11, 2013 at 2:3 Comment(1)
...always had troubles memorizing this spell. Not quite intuitive. PS. Since OP requested a char array, contents.c_str() can be of use.Accident
K
2

You don't need to read line by line if you're planning to suck the entire file into a buffer.

char getdata[10000];
infile.read(getdata, sizeof getdata);
if (infile.eof())
{
    // got the whole file...
    size_t bytes_really_read = infile.gcount();

}
else if (infile.fail())
{
    // some other error...
}
else
{
    // getdata must be full, but the file is larger...

}
Ketron answered 7/12, 2010 at 3:52 Comment(2)
What if the file is bigger than 10000 chars?Worldbeater
.... and what you're going to do there? declare another char array of bigger size and read again? wrong, isn't it?Worldbeater
U
1

Every time you read a new line you overwrite the old one. Keep an index variable i and use infile.read(getdata+i,1) then increment i.

Unbelief answered 7/12, 2010 at 3:19 Comment(2)
read(..., 1) reads one character at a time... very inefficient.Ketron
infile.seekg(0,ios::end);int len = infile.peekg();infile.seekg(0,ios::beg);infile.read(getdata,len);Unbelief
K
0

You could use Tony Delroy's answer and incorporate a little function to determine the size of the file, and then create the char array of that size, like this:

//Code from Andro in the following question: https://mcmap.net/q/143152/-how-can-i-get-a-file-39-s-size-in-c-duplicate

int getFileSize(std::string filename) { // path to file
    FILE *p_file = NULL;
    p_file = fopen(filename.c_str(),"rb");
    fseek(p_file,0,SEEK_END);
    int size = ftell(p_file);
    fclose(p_file);
    return size;
}

Then you can do this:

//Edited Code From Tony Delroy's Answer
char getdata[getFileSize("file.txt")];
infile.read(getdata, sizeof getdata);

if (infile.eof()) {
    // got the whole file...
    size_t bytes_really_read = infile.gcount();
}
else if (infile.fail()) {
    // some other error...
}
Kevenkeverian answered 14/3, 2019 at 1:5 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.