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

Urgent Help. Text File Reading.

I am Creating a MFC Application to Read a Text file into a Edit Box. I am implementing the following code

FILE *m_pFile ;
    CString m_strLine , m_strText;
    char line [1000] = "" ;

    m_pFile = fopen ( "C:\\SELF.txt" , "r" ) ;
        
    if ( m_pFile != NULL ) 
    {
        // read each line in the file
        while(fgets(line,sizeof(line),m_pFile) != NULL)
        {
            // convert the line to a CString
            m_strLine = line ; 

            // format the string for the edit control
            m_strLine.Replace ( '\n' , '\r\n' );    //PROBLEM HERE

            // store each line to the text string
            m_strText += m_strLine ; 
        }
    }

    // write all the text to the edit box at once
    GetDlgItem(IDC_EDIT1)->SetWindowTextW( m_strText ) ;

    // close the file
    fclose ( m_pFile );

All The text is appearing without the line feed. Please Help!!! :'(
I am Using Visual Studio 2008.

Alternate Suggestions are Welcome.

Kunal Aggarwal
Newbie Poster
13 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

You want

m_strLine.Replace("\n", "\r\n");

>> m_strLine.Replace ( '\n' ,'\r\n' );

That should give you a warning or two, are you ignoring compiler warnings?

mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
 

You want

m_strLine.Replace("\n", "\r\n");

>> m_strLine.Replace ( '\n' ,'\r\n' );

That should give you a warning or two, are you ignoring compiler warnings?

m_strLine.Replace("\n", "\r\n");
is giving a error
cannot covert from const char [2] to wchar_t

Kunal Aggarwal
Newbie Poster
13 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

Alright, then wrap the string literals with the _T() macro (from ), like so;

m_strLine.Replace(_T("\n"), _T("\r\n"));
mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
 

>>cannot covert from const char [2] to wchar_t
Hummm. If you are compiling the program for UNICODE then why aren't you getting similar errors on all the other lines in your program (such as line 5)?

You could have avoided that problem altogether had you used ifstream and getline() instead of C's FILE and associated C functions.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

>> If you are compiling the program for UNICODE then why aren't you getting similar errors on all the other lines in your program (such as line 5)?

fopen() is the non-UNICODE version, so the code is valid as such - although it is confusing. One option would be to completely stick with MFC i.e. using CStdioFile/CString for reading the file.

mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
 

Alright, then wrap the string literals with the _T() macro (from ), like so;

m_strLine.Replace(_T("\n"), _T("\r\n"));


Thanks So Much.. It worked. Thanks again.

Kunal Aggarwal
Newbie Poster
13 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

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