C ++如何从文本文件中读取并将其拆分为值 [英] C++ how to read from a text file and split it into values

查看:84
本文介绍了C ++如何从文本文件中读取并将其拆分为值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



在C ++中,我一直在尝试阅读我的文本文件并将其存储到变量中



in c#i能够做到这一点,虽然我怎么用c ++做呢?



Hi Guys,

Within C++ i've been trying to read my text file and store it into variables

in c# i was able to do it, although how would i do it in c++?

string path = System.IO.Directory.GetCurrentDirectory() + @"\Destiny2Data.txt";
            var files = File.ReadLines(path);
            

            foreach (var f in files)
            {
                if (f.StartsWith("XModifier"))
                {
                    string s = f.Replace("\"", "").Split('=')[1];
                    xTextbox.Text = s;
                }

                if (f.StartsWith("YModifier"))
                {
                    string s = f.Replace("\"", "").Split('=')[1];
                    yTextbox.Text = s;
                }

                if (f.StartsWith("Left Mouse Click"))
                {
                    string s = f.Replace("\"", "").Split('=')[1];
                    LeftMouseClick.Checked = Convert.ToBoolean(s);
                }

                if (f.StartsWith("Right Mouse Click"))
                {
                    string s = f.Replace("\"", "").Split('=')[1];
                    RightMouseClick.Checked = Convert.ToBoolean(s);
                }
                if (f.StartsWith("Red Health Bar"))
                {
                    string s = f.Replace("\"", "").Split('=')[1];
                    RedHealthBar.Checked = Convert.ToBoolean(s);
                }
                if (f.StartsWith("Yellow Health Bar"))
                {
                    string s = f.Replace("\"", "").Split('=')[1];
                    YellowHealthBar.Checked = Convert.ToBoolean(s);
                }


            }





我当前的文本文档如下所示:





My current text document looks like this:

Quote:

XModifier = 1

YModifier = 1

鼠标左键单击= False

右键鼠标点击= False

红色健康栏= False

黄色健康Bar = False

XModifier = 1
YModifier = 1
Left Mouse Click = False
Right Mouse Click = False
Red Health Bar = False
Yellow Health Bar = False





我尝试过:



尝试用c#



完美的工作



试过这个c ++虽然不是我想要的东西





What I have tried:

Tried it in c#

perfectly working

tried this in c++ though wasnt what i was looking for

auto read_file(std::istream& in)
{
    using namespace std;
    map<string, vector<string>> values;
    string line;
    while(getline(in, line))
    {
        istringstream linein{ line };
        string key, value;
        if(!getline(linein, key, '#'))
            throw runtime_error{ "bad file format" };
        while(getline(linein, value, '#') && !value.empty())
            values[key].emplace_back(move(value));
    }
    return values;
}

推荐答案

使用 std :: string 类,具有compare,find和substring等功能。请参阅类,函数和示例代码的链接。 ; - )
Use the std::string class with its features like compare, find and substring. See the link for the class, functions and sample code. ;-)


这是从这样的文件中读取一些值的示例。缺少错误检查:

Here is an example of reading some values from such file. Error checks are missing:
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
#include <sstream>

int read_int(std::istream& input_stream)
{
	int n;
	input_stream >> n;
	return n;
}

bool read_bool(std::istream& input_stream)
{
	std::string value;
	bool b;
	input_stream >> value;
	std::transform(value.begin(), value.end(), value.begin(), ::tolower);
	std::istringstream{ std::move(value) } >> std::boolalpha >> b;
	return b;
}

int main()
{
	std::ifstream input_file("text.txt");
	std::string name;
	while (std::getline(input_file, name, '='))
	{
		if (name.compare(0, 3, "XMo") == 0)
			std::cout << read_int(input_file) << std::endl;
		else if (name.compare(0, 3, "YMo") == 0)
			std::cout << read_int(input_file) << std::endl;
		else if (name.compare(0, 4, "Left") == 0)
			std::cout << std::boolalpha << read_bool(input_file) << std::endl;
		else if (name.compare(0, 5, "Right") == 0)
			std::cout << std::boolalpha << read_bool(input_file) << std::endl;
		else if (name.compare(0, 3, "Red") == 0)
			std::cout << std::boolalpha << read_bool(input_file) << std::endl;

		input_file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}

	input_file.close();
	std::cin.get();
    return 0;
}


这篇关于C ++如何从文本文件中读取并将其拆分为值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆