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

if we want read the content into a buffer, we can use the function getline()

For example, the following code uses the getline() function to display the 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 stdin and displays it to stdout:

    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 function read()
Syntax:
    #include  < fstream >
    istream
&  read(  char *  buffer, streamsize num );

The function read() is used with input streams, and reads num bytes from the stream before placing them in buffer. If EOF is encountered, read() stops, leaving however many bytes it put into buffer as they are.

first,we should 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 use tellg() 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;
    }






 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值