zoukankan      html  css  js  c++  java
  • How to read a whole document content into a string onetime

    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;
        }






     




    范晨鹏
    ------------------
    软件是一种态度
    成功是一种习惯


  • 相关阅读:
    php
    nginx
    docker
    pyenv 配置python虚拟环境
    [运维笔记] Nginx编译安装
    [运维笔记] Mysql单库备份脚本
    BurpSuite Intruder 4种攻击模式
    java判断一个单向链表是否有环路
    二分查找(递归和非递归)
    反转链表算法题
  • 原文地址:https://www.cnblogs.com/diylab/p/937470.html
Copyright © 2011-2022 走看看