Hey am trying to read contents of a text file into memory and will be read later by a different function.


This is how i am considering doing it

I was thinking of reading the whole file into an array but thought it might be too big seeing as file has alot of lines of text in it

Channel :: Channel(char* channelName, char* channelNumber,  char* fileName)
 {
    this->channelName = channelName;
    this->channelNumber = channelNumber;
    this->fielName = fileName;
    


    fstream* file = new fstream (FileName, fstream :: in);
    
    char aSingleCharacter;
    
    aSingleCharacter = (char) file->get();

    while(aSingleCharacter != EOF)
    {
        aSingleCharacter = (char)file -> get();
    }
    
}

any help be much appericated.

It's best not to use EOF.

You probably don't have to read character by character also. Look into the >> operator and the getline() method of istream (implemented by ifstream).

Do you mean, How to read file in binary mode?
If, so you may do something like this:

file->read(reinterpret_cast<char *>(this), sizeof(Channe1));

Correct me if i did something wrong

,,,
Kimo :P

It's best not to use EOF.

EOF is fine. It's best not to us .eof() -- a big difference. :icon_wink:

commented: You're right. A false assumption on my part. Time to dust off Kelley/Pohl again! +2

You've done it again WaltP! That's my bad. Well, Dave's thread's a good read anyway. :)

I still maintain that reading in character by character (and seemingly throwing them away once a new one is read) is probably not the best approach.

Kim
I'm trying read from text file into memory, to be used by different classes to search through.

Thanks for all the help =]

Sorry quick question about getline();
Is there anyway to set it so that will read till end of line without stating the amount of characters you want read

exaxmple:

getline(line, MAX_CHAR_BUFFER_SIZE);

if you use std::strings, then yes:

std::string buffer;
ifstream in("c:/.........");
std::getline(in, buffer);
std::cout << "Read :" << buffer;

But if you insist on using char-arrays, then you need to input the number of chars to read. This is to prevent you from running out of your arays bound without even noticing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.