How to read a whole document content into a string one-time

Source: Internet
Author: User
If we want read the content into a buffer, we can use the Function Getline ()

For example, the following code usesGetline ()Function to display
First 100 characters from each line of a text file:

Int max_length = 100;
Char line [max_length];
While (Fin. Getline (line, max_length ))
{
Cout <"Read line:" <line <Endl;
}
and, if we want to read the content into a string, we also use the global function getline()
Syntax: # Include <string>
Istream & Getline (istream & Fin, string & S, char delimiter = '\ n ');

For example, the following code reads a line of text from
StdinAnd displays itStdout:

String S;
Getline (FIN, S );
Cout <"you entered" <S <Endl;

However, more often we need to read a whole document content into a string, and, consider to efficiency, we want do the operater with minimal times. We can use the FunctionRead ()
Syntax:

# Include <fstream>
Istream & read (char * buffer, streamsize num );

The FunctionRead ()Is used with input streams, and readsNumBytes
From the stream before placing them inBuffer. IfEOF
Is encountered,Read ()Stops, leaving however bytes it put
BufferAs they are.

First, we shocould get the size of the file to be read. Then, we resize the string to the size of the file. And, we can read the file content into the string buffer.
We can use the STD: iostream ::Seekg ()

Fin. seekg (0, IOS: End );
Streamsize = fin. tellg ()
Syntax: # Include <fstream>
Istream & seekg (off_type offset, IOS: seekdir origin );
The function seekg()  repositions the "get" pointer for the current stream to offset bytes away from origin. 

We repositions it to the end of the documentstream. Then, we useTellg ()Got the current
"Get" position of the pointer in the stream. And, aha! We get the filesize: idocumentsize.

we shou move the "get" pointer to the origin position:
Fin. seekg (0 );
we shoule risize the string  so there is enough space for the DocumentStream. 
S. Resize (streamsize );

So there is enough space for the documentstream. the pointer to the space is "document.Data ()". And now, we can read:

Fin. Read (char *) document. Data (), streamsize );

The whole code as follow: String GetFile (string & strfilename)
{
String S;
Ifstream fin;
Long streamsize;
Fin. Open (strfilename. c_str (), IOS: In );
If (! Fin)
{
Cerr <"Err: Open" <strfilename. c_str () <"failed" <Endl;
Exit (-1 );
}
Fin. seekg (0, IOS: End );
Streamsize = fin. tellg ();
S. Resize (streamsize );
Fin. seekg (0 );
Fin. Read (char *) S. Data (), streamsize );
Fin. Close ();
Return S;
}

Visual c ++ 2005 provide the function readtoend () to read a text file into a string:
Syntax: System. String readtoend ()
Member of system. Io. streamreader

Summary:
Reads the stream from the current position to the end of the stream.

The follow Code read the file c: \ test.txt into a string and print it out. String ^ sfile = "";
Streamreader ^ objreader = gcnew streamreader ("C: \ test.txt ");
Sfile = objreader-> readtoend ();
Objreader-> close ();
Console: writeline (sfile );
Console: Readline ();

If you need, you can very easily convert string * To wchar_t * using ptrtostringchars function.

Syntax: # Include <vcclr. h>
_ Const_char_ptr ptrtostringchars (_ const_string_handle S );

The Code as follow: # using <mscorlib. dll>
# Include <iostream>
# Include <vcclr. h>

Using namespace system;
Sing namespace system: IO;
Using namespace system: collections;
Using namespace STD;

Int wmain (void)
{
Streamreader ^ objreader = gcnew streamreader ("C: \ test.txt ");
Pin_ptr <const wchar_t> wch = ptrtostringchars (objreader-> readtoend ());
Objreader-> close ();
Wstring mm (wch );
Console: Readline ();
Return 0;
}


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.