相关疑难解决方法(0)

将整个ASCII文件读入C++ std :: string

我需要将整个文件读入内存并将其放在C++中std::string.

如果我把它读成a char[],答案很简单:

std::ifstream t;
int length;
t.open("file.txt");      // open input file
t.seekg(0, std::ios::end);    // go to the end
length = t.tellg();           // report location (this is the length)
t.seekg(0, std::ios::beg);    // go back to the beginning
buffer = new char[length];    // allocate memory for a buffer of appropriate dimension
t.read(buffer, length);       // read the whole file into the buffer
t.close();                    // close file handle

// ... Do stuff with buffer here ...
Run Code Online (Sandbox Code Playgroud)

现在,我想做同样的事情,但是使用a std::string而不是a char[] …

c++ string file-io caching standard-library

559
推荐指数
5
解决办法
49万
查看次数

该声明在C++中没有存储类或类型说明符

我的课程中有多个课程.

A)当我在另一个类中创建一个类的对象时,我没有错误但是当我使用该对象来调用一个函数时,我得到了上面的错误.

B)另外如果我创建另一个类的对象并在我的类的构造函数中使用它调用一个函数,那么我没有得到这样的错误.

C)Cout函数在类的主体中不起作用,除非我把它放到任何函数中

D)主类能够完成所有这些并且我没有收到任何错误.

很快就能收到回复.先感谢您 .

以下是代码:这是我的cpp中的两个类.除了在创建对象后使用对象,我没有遇到任何问题.代码太大了太过贴了.一切都可以在主要但不在其他课程中完成,为什么?

 #include <iostream>
 #include <fstream>
 #include <iomanip>
 #include <string>
 #include <cstdlib> 
 #include <vector>
 #include <map>
 using namespace std;
 class Message
  {

    public:
    void check(string side)
       {
    if(side!="B"&&side!="S")
          {
    cout<<"Side should be either Buy (B) or Sell (S)"<<endl;;
          }
       }
  };

    class Orderbook
  {
    public:
      string side;
      Orderbook()  //No Error if I define inside constructor
      Message m;       //No Error while declaring
      m.check(side);   //Error when I write m. or m->
  };
Run Code Online (Sandbox Code Playgroud)

c++

12
推荐指数
2
解决办法
10万
查看次数

标签 统计

c++ ×2

caching ×1

file-io ×1

standard-library ×1

string ×1