Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;读取文件 什么是读取文件并将其内容分配给C++中的字符串的最小代码? 我确实读了很多有用的教程,但它们在某种程度上都是不同的,所以我想知道为什么,如果你能提供一些解释性的评论,那就太好了_C++ - Fatal编程技术网

C++;读取文件 什么是读取文件并将其内容分配给C++中的字符串的最小代码? 我确实读了很多有用的教程,但它们在某种程度上都是不同的,所以我想知道为什么,如果你能提供一些解释性的评论,那就太好了

C++;读取文件 什么是读取文件并将其内容分配给C++中的字符串的最小代码? 我确实读了很多有用的教程,但它们在某种程度上都是不同的,所以我想知道为什么,如果你能提供一些解释性的评论,那就太好了,c++,C++,相关:我每行读一个单词 #include<fstream> #include<string> using namespace std; int main(int argc, char **argv) { fstream inFile; string str; while(!inFile.eof()) { inFile.open("file.txt"); infile>&g

相关:

我每行读一个单词

    #include<fstream>
    #include<string>
    using namespace std;    
    int main(int argc, char **argv)
    {
    fstream inFile;
    string str;
    while(!inFile.eof())
    {
    inFile.open("file.txt");
    infile>>str;
    }
    inFile.close();
    return 0;
    }
#包括
#包括
使用名称空间std;
int main(int argc,字符**argv)
{
河道充填;
字符串str;
而(!infle.eof())
{
infle.open(“file.txt”);
infle>>str;
}
infle.close();
返回0;
}
#包括
#包括
int main()
{
std::ifstream文件(“myfile.txt”);//打开该文件
std::字符串行,整个_文件;
//从“文件”中一次读取一行并存储结果
//在名为“line”的字符串中。
while(std::getline(文件,行))
{
//将每一行附加在一起,以使整个文件
//一脉相承。
整_文件+=行;
整个_文件+='\n';
}
返回0;
//当对象超出范围时,“文件”将自动关闭。
}
这里有几件事需要注意
getline()
返回对流对象的引用,如果发生任何错误或到达文件末尾,则while测试将失败。此外,字符串中不包括尾随的换行符,因此您必须手动追加它。

最短代码:(无效)

#包括
#包括
#包括
#包括
#包括
int main()
{
标准:IFF流(“plop”);
字符串缓冲区;
std::copy(std::istreambuf_迭代器(f),
std::istreambuf_迭代器(),
std::反向插入器(缓冲区);
}
我可能会怎么做:

#include <iostream>
#include <string>    
#include <vector>
#include <algorithm>
#include <iterator>
#include <fstream>



int main()
{
    // Find the size of the file
    std::ifstream       file("Plop");
    file.seekg(0,std::ios_base::end);

    std::streampos      size    = file.tellg();

    // Read the file in one go.
    file.seekg(0);
    std::vector<char>   buffer(size); // pre-szie the vector.
    file.read(&buffer[0],size);

    // or

    // Until the next version of the standard I don't think string gurantees contigious storage.
    // But all the current versions I know do use continious storage so it should workd.
    file.seekg(0);
    std::string         buffer1(size);
    file.read(&buffer1[0],size);
}
#包括
#包括
#包括
#包括
#包括
#包括
int main()
{
//查找文件的大小
std::ifstream文件(“Plop”);
seekg(0,std::ios\u base::end);
std::streampos size=file.tellg();
//一次读取文件。
文件.seekg(0);
std::vector buffer(size);//预处理向量。
读取(&缓冲区[0],大小);
//或
//直到下一版本的标准,我不认为字符串保证连续存储。
//但我所知道的所有当前版本都使用连续存储,因此应该可以正常工作。
文件.seekg(0);
std::字符串缓冲区1(大小);
文件读取(&buffer1[0],大小);
}

这比简短的解决方案要长,但可能会稍微更有效,因为它的复制要少一点-不过我没有做任何时间比较:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;;

unsigned int FileRead( istream & is, vector <char> & buff ) {
    is.read( &buff[0], buff.size() );
    return is.gcount();
}

int main() {
    ifstream ifs( "afile.dat", ios::binary );
    const unsigned int BUFSIZE = 64 * 1024; 
    std::vector <char> buffer( BUFSIZE );
    unsigned int n;
    string s;
    while( n = FileRead( ifs, buffer ) ) {
        s.append( &buffer[0], n );
    }

    cout <<  s;
}
#包括
#包括
#包括
#包括
使用名称空间std;;
无符号整数文件读取(istream&is、vector&buff){
is.read(&buff[0],buff.size());
返回值为.gcount();
}
int main(){
ifstream-ifs(“afile.dat”,ios::binary);
常量unsigned int BUFSIZE=64*1024;
std::向量缓冲区(BUFSIZE);
无符号整数n;
字符串s;
while(n=FileRead(ifs,buffer)){
s、 追加(&缓冲区[0],n);
}

cout我没有看到那么多:

#include <fstream>
#include <sstream>
#include <string>
using namespace std;

int main() {
    ifstream ifs("filename");
    stringstream ss;
    ss << ifs.rdbuf();
    string s = ss.str();
}
#包括
#包括
#包括
使用名称空间std;
int main(){
ifs流ifs(“文件名”);
细流ss;
如果您知道您的文件包含文本,则可以使用的
平台stl::memory\u mapped\u file

platformstl::memory_mapped_file file("your-file-name");
std::string contents(static_cast<char const*>(file.memory()), file.size());
如果要将其加载到行集合中,请使用
platformstl::read_lines()

platformstl::基本文件行(“您的文件名”);
size_t n=行。size();
std::string line3=行[3];

在C++中,main必须返回int。您似乎也已经忘记了STD::命名空间。您忘了包含了。是的,我忘记了包括字符串头和标准库。但是我使用的是无效main函数,它不返回任何值。如果使用int main,则需要返回值。@ Syed Tayyab Ali:不,空格是不允许的。标准。也是main()是一种特殊情况,如果您不指定返回值,则将为您返回0。@马丁·约克:显然,这是一种共识。我认为如果文件没有以换行符终止,则会给出错误的答案。将适当的if测试添加到此类条件的陷阱中作为操作的练习。;-)只提到这一点,因为Eclipse是要求最低代码:C++标准将承担一个“返回0”;如果main函数的末尾已经达到。对于main(),如果“IFS”处于一个坏的状态,RDBUFF()是否做任何不安全的事情(例如,如果文件不存在),OOP,我意外地编辑出“您也想要一些错误检查”,这是一个特殊的情况。实验上,在我的实现中,你会得到一个空字符串,所以它不会做任何坏事,但是你不会发现上面代码的错误。
#include <fstream>
#include <sstream>
#include <string>
using namespace std;

int main() {
    ifstream ifs("filename");
    stringstream ss;
    ss << ifs.rdbuf();
    string s = ss.str();
}
platformstl::memory_mapped_file file("your-file-name");
std::string contents(static_cast<char const*>(file.memory()), file.size());
platformstl::memory_mapped_file file("your-file-name");
std::wstring contents(static_cast<wchar_t const*>(file.memory()), 
          file.size() / sizeof(wchar_t));
std::string contents;
winstl::load_text_file("your-file-name", contents);
platformstl::basic_file_lines<char> lines("your-file-name");
size_t n = lines.size();
std::string line3 = lines[3];