PDA

Click to See Complete Forum and Search --> : Loading the characters of a file into an array


Dec 23rd, 2000, 10:12 AM
Does any know fo a routine that will load all of files characters into an array?

parksie
Dec 23rd, 2000, 10:58 AM
Yes:

#include <iostream>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>

using namespace std;

void reader() {
struct _stat buf;
_stat("file.ext", &buf);

long lLen = buf.st_size;
char *pcBuf = new char[lLen+1];

ifstream inFile("file.ext");

if(inFile.good()) {
inFile.read(pcBuf, lLen);
}

pcBuf[lLen] = 0; // Null-terminate, just in case

// Use array

delete[] pcBuf;
}