Share via


CStdioFile::ReadString

Reads text data into a buffer, up to a limit of nMax–1 characters, from the file associated with the CStdioFile object.

virtual LPTSTR ReadString( 
   LPTSTR lpsz, 
   UINT nMax  
); 
virtual BOOL ReadString( 
   CString& rString 
);

Parameters

  • lpsz
    Specifies a pointer to a user-supplied buffer that will receive a null-terminated string.

  • nMax
    Specifies the maximum number of characters to read, not including the terminating null character.

  • rString
    A reference to a CString object that contains the read string when the function returns.

Return Value

If you use the version that returns a LPTSTR, the method returns either a pointer to the buffer that contains the text data or NULL if end-of-file was reached without reading any data.

If you use the version that returns a BOOL, the method returns FALSE if end-of-file was reached without reading any data; otherwise, the method returns TRUE.

Remarks

Reading is stopped by the first newline character. If the method reads fewer than nMax–1 characters, it appends a newline character to the buffered string. The method appends a NUL character (\0) regardless of the number of read characters.

CFile::Read is also available for text-mode input, but it does not terminate on a carriage return–linefeed pair.

This method throws a CFileException* if it is unable to read the string from the file or if you specify NULL for the nMax parameter.

This method throws a CInvalidArgException* if you specify NULL for the lpsz parameter.

If you specify a negative value for the nMax parameter, the calling application exits unexpectedly.

Note

The version of this method that returns a BOOL removes the \n if present; the version that returns a LPTSTR does not.

Example

CStdioFile f(stdin);
TCHAR buf[100];

f.ReadString(buf, 99);

Requirements

Header: afx.h

See Also

Reference

CStdioFile Class

Hierarchy Chart

CStdioFile::WriteString

CFile::Read

Other Resources

CStdioFile Members

Change History

Date

History

Reason

July 2009

Added exception descriptions and cleaned up topic.

Information enhancement.