Click here to Skip to main content
16,017,015 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionRe: Assertion Failed Pin
David Crow26-Mar-07 2:36
David Crow26-Mar-07 2:36 
AnswerRe: Assertion Failed Pin
crieagu26-Mar-07 3:09
crieagu26-Mar-07 3:09 
GeneralRe: Assertion Failed Pin
David Crow26-Mar-07 3:20
David Crow26-Mar-07 3:20 
GeneralRe: Assertion Failed Pin
crieagu26-Mar-07 3:37
crieagu26-Mar-07 3:37 
GeneralRe: Assertion Failed Pin
David Crow26-Mar-07 3:39
David Crow26-Mar-07 3:39 
QuestionReading csv file -> Lil modification needed..... Pin
Software_Specialist24-Mar-07 15:13
Software_Specialist24-Mar-07 15:13 
AnswerRe: Reading csv file -> Lil modification needed..... Pin
Software_Specialist25-Mar-07 3:30
Software_Specialist25-Mar-07 3:30 
GeneralRe: Reading csv file -> Lil modification needed..... Pin
PJ Arends25-Mar-07 13:20
professionalPJ Arends25-Mar-07 13:20 
For a software specialist you sure have a lot to learnD'Oh! | :doh:

There are many ways to read a line of text from a file. I find the easiest way is to use CStdioFile::ReadString. If you do not or can not use MFC then you could use ifstream::getline although the shortfall of getline is that it requires a preallocated buffer. ifstream::operator >>(std::string &) also can be used but it breaks at any and all whitespaces.

If I am stuck using STL streams I use my own overloaded operator >>():
std::tifstream& operator >>(std::tifstream &stream, std::tstring &string)
{
    string.clear();

    TCHAR ch = 0;
    while (!stream.eof() && !stream.fail() && ch != _T('\n'))
    {
        if (0 != ch) string += ch;
        stream._Read_s(&ch, 1, 1);
    }

    return stream;
}
It might not be the most efficient, but it works as a standard C++ stream extractor and it extracts the entire string, from the start right up to the newline character.

Example usage - dumps a text file to the console output:
int _tmain(int argc, _TCHAR* argv[])
{
    std::tifstream f(argv[1]);
    std::tstring s;
    while (!f.fail() && !f.eof())
    {
        f >> s;
        std::tcout << s << std::endl;
    }

    return 0;
}



You may be right
I may be crazy
-- Billy Joel --


Within you lies the power for good, use it!!!

GeneralRe: Reading csv file -&amp;gt; Lil modification needed..... Pin
Stephen Hewitt25-Mar-07 14:32
Stephen Hewitt25-Mar-07 14:32 
GeneralRe: Reading csv file -> Lil modification needed..... Pin
PJ Arends25-Mar-07 17:00
professionalPJ Arends25-Mar-07 17:00 
AnswerRe: Reading csv file -> Lil modification needed..... Pin
Stephen Hewitt25-Mar-07 14:35
Stephen Hewitt25-Mar-07 14:35 
GeneralRe: Reading csv file -> Lil modification needed..... Pin
Software_Specialist26-Mar-07 10:21
Software_Specialist26-Mar-07 10:21 
GeneralRe: Reading csv file -&amp;gt; Lil modification needed..... Pin
Stephen Hewitt26-Mar-07 13:22
Stephen Hewitt26-Mar-07 13:22 
GeneralRe: Reading csv file -&amp;gt; Lil modification needed..... Pin
Software_Specialist26-Mar-07 14:22
Software_Specialist26-Mar-07 14:22 
GeneralRe: Reading csv file -&amp;gt; Lil modification needed..... Pin
Stephen Hewitt26-Mar-07 14:32
Stephen Hewitt26-Mar-07 14:32 
GeneralRe: Reading csv file -&amp;gt; Lil modification needed..... Pin
Software_Specialist27-Mar-07 2:34
Software_Specialist27-Mar-07 2:34 
GeneralRe: Reading csv file -&amp;amp;gt; Lil modification needed..... Pin
Stephen Hewitt27-Mar-07 4:01
Stephen Hewitt27-Mar-07 4:01 
QuestionPPC (tmpstr) Index Numbering Pin
ZapMe124-Mar-07 11:30
ZapMe124-Mar-07 11:30 
QuestionActive interfaces [modified] Pin
deeps_cute24-Mar-07 9:38
deeps_cute24-Mar-07 9:38 
QuestionRe: Active interfaces Pin
Mark Salsbery25-Mar-07 9:02
Mark Salsbery25-Mar-07 9:02 
Questionhow many lines? Pin
Lord_Vader24-Mar-07 9:19
Lord_Vader24-Mar-07 9:19 
AnswerRe: how many lines? Pin
Christian Graus24-Mar-07 10:02
protectorChristian Graus24-Mar-07 10:02 
GeneralRe: how many lines? Pin
Lord_Vader24-Mar-07 10:06
Lord_Vader24-Mar-07 10:06 
GeneralRe: how many lines? Pin
Christian Graus24-Mar-07 12:39
protectorChristian Graus24-Mar-07 12:39 
GeneralRe: how many lines? Pin
Lord_Vader24-Mar-07 13:33
Lord_Vader24-Mar-07 13:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.