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()
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:
the whole code as follow:
visual c++2005 provide the function ReadToEnd() to read a text file into a String:
Syntax:
the follow code read the file c:\test.txt into a String and print it out.
If you need, You can very easily convert String* to wchar_t* using PtrToStringChars function.
Syntax:
the code as follow:
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;
}
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' );
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;
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 );
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()
streamsize = fin.tellg()
Syntax:
#include <fstream>
istream& seekg( off_type offset, ios::seekdir origin );
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;
}
{
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.
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();
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);
__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;
}
#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;
}
范晨鹏
------------------
软件是一种态度
成功是一种习惯