954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Reading a file into memory C++

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.

hannon565
Newbie Poster
8 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

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).

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

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

kim00000
Junior Poster in Training
93 posts since Oct 2009
Reputation Points: 12
Solved Threads: 11
 
It's best not to use EOF .


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

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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.

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

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

Thanks for all the help =]

hannon565
Newbie Poster
8 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

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);
hannon565
Newbie Poster
8 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

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.

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: