Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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++_Oop_File Io - Fatal编程技术网

C++ 清理此代码[文件输入]

C++ 清理此代码[文件输入],c++,oop,file-io,C++,Oop,File Io,我必须从一个文件中读取这样的行: 255 0 0 15 x l Red Yellow,Gray,Blue,Green std::string line; while(file.good()) { getline(worldfile, line); unsigned space = line.find(" "); unsigned r = atoi(line.substr(0, space).c_str()); line = line.substr(space +

我必须从一个文件中读取这样的行:

255 0 0 15 x l Red Yellow,Gray,Blue,Green
std::string line;
while(file.good()) {
    getline(worldfile, line);
    unsigned space = line.find(" ");
    unsigned r = atoi(line.substr(0, space).c_str());
    line = line.substr(space + 1);
    space = line.find(" ");
    unsigned g = atoi(line.substr(0, space).c_str());
    line = line.substr(space + 1);
    space = line.find(" ");
    unsigned b = atoi(line.substr(0, space).c_str());
    line = line.substr(space + 1);
    space = line.find(" ");
    unsigned gold = atoi(line.substr(0, space).c_str());
    line = line.substr(space + 1);
    space = line.find(" ");
    char resource = line.substr(0, space).c_str()[0];
    // do something with these values
}
假设我想读这行的前5个元素(以空格分隔)。我现在是这样做的:

255 0 0 15 x l Red Yellow,Gray,Blue,Green
std::string line;
while(file.good()) {
    getline(worldfile, line);
    unsigned space = line.find(" ");
    unsigned r = atoi(line.substr(0, space).c_str());
    line = line.substr(space + 1);
    space = line.find(" ");
    unsigned g = atoi(line.substr(0, space).c_str());
    line = line.substr(space + 1);
    space = line.find(" ");
    unsigned b = atoi(line.substr(0, space).c_str());
    line = line.substr(space + 1);
    space = line.find(" ");
    unsigned gold = atoi(line.substr(0, space).c_str());
    line = line.substr(space + 1);
    space = line.find(" ");
    char resource = line.substr(0, space).c_str()[0];
    // do something with these values
}
我不喜欢这个代码的外观。虽然它对我的文件非常有效,但我不喜欢它只是一行接一行的代码。这使得阅读变得非常困难。 我还计划在完成后将我的代码(用于游戏)作为开源发布,但我为发布这样的代码感到羞愧


我怎样才能把它清理干净呢?

像这样的东西应该可以把它们全部读出来:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main ()
{
   fstream file;
    file.open("file.txt",ios::in);
    unsigned r,g,b,gold;
    char i,f;
    string line;
    while(file >> r >> g >> b >> gold >> i >> f)
    {
        getline(file,line);
        cout << r << " " << g << " "<< b << " "<< gold << " "<< i << " "<< f << endl;
    }
    return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
流文件;
打开(“file.txt”,ios::in);
未签名的r、g、b、gold;
chari,f;
弦线;
而(文件>>r>>g>>b>>gold>>i>>f)
{
getline(文件,行);

cout作为初学者。了解流的提取运算符(例如,
std::istream&operator>>()(std::stream&,int&)
来命名一个您将在这里使用的运算符。)
std::istringstream
可能也适用于单独的行处理。这会占用空格吗?还是我必须这样做?
file>“>>g>>“”>>b>>“”>>i>>“”>>gold;
?它负责空格,测试:)我按照你的建议做了,读了这行
255 0 0 15 x
生成了这个输出:25500。我使用了这个代码:
未签名的gold;char资源;worldfile>>color->r>>color->g>>color->b>>gold>>资源;std::cout r g b,如果你不使用
li做任何事情的话ne
,那么
ignore
可能比
getline
好(但是
getline
也可以)