Results 1 to 2 of 2

Thread: Loading the characters of a file into an array

  1. #1
    Guest

    Post

    Does any know fo a routine that will load all of files characters into an array?

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Yes:
    Code:
    #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;
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width