Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.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++_File_Variables_Io_Assign - Fatal编程技术网

从文件到变量? 我比较喜欢C++,我想问一下如何从文件中生成变量。 我想写一个程序来读取文件,它使用符号作为标记, 就像一种语言

从文件到变量? 我比较喜欢C++,我想问一下如何从文件中生成变量。 我想写一个程序来读取文件,它使用符号作为标记, 就像一种语言,c++,file,variables,io,assign,C++,File,Variables,Io,Assign,我希望它能给出这样的频率: !frequency:time,frequency:time;//!=start/;=end 我是这样理解你的问题的。您有一个文件test.txt: time freq 0.001 12.3 0.002 12.5 0.003 12.7 0.004 13.4 然后,您需要读取此文件,以便一个容器中有time,另一个容器中有freq,以便进一步处理。如果是这样,那么您的程序如下所示: #include<iostrea

我希望它能给出这样的频率:

!frequency:time,frequency:time;//!=start/;=end

我是这样理解你的问题的。您有一个文件
test.txt

time      freq
0.001     12.3
0.002     12.5 
0.003     12.7
0.004     13.4 
然后,您需要读取此文件,以便一个容器中有
time
,另一个容器中有
freq
,以便进一步处理。如果是这样,那么您的程序如下所示:

#include<iostream>
using namespace std;

int main()
{
    ifstream in_file("test.txt");

    string label1, label2;
    float val;

    in_file >> label1;  //"time"
    in_file >> label2;   // "freq"

    vector<float> time;
    vector<float> freq;

    while (in_file >> val)
    {   
            time.pushback(val);
            in_file >> val;        
            freq.pushback(val);
    }   
 }
#包括
使用名称空间std;
int main()
{
_文件(“test.txt”)中的ifstream;
字符串label1,label2;
浮动增值税;
在\u文件>>标签1;/“时间”中
在\u文件>>label2;/“freq”中
矢量时间;
矢量频率;
while(在文件中>>val)
{   
回推时间(val);
在_文件>>val;
频率回推(val);
}   
}

对于我在评论中提到的问题,有一个更普遍的解决方案:

#include <iostream>
#include <sstream>

int main()
{
    std::map<std::string, std::vector<double> > values_from_file;

    std::ifstream in_file("test.txt");


    std::string firstLine;
    std::getline(in_file, firstLine);
    std::istringstream firstLineInput(firstLine);

    // read all the labels (symbols)
    do
    {
        std::string label;
        firstLineInput >> label;
        values_from_file[label] = std::vector<double>();
    } while(firstLineInput);

    // Read all the values column wise
    typedef std::map<std::string, std::vector<double> >::iterator It;

    while(in_file)
    {
        for(It it = std::begin(values_from_file);
            it != std::end(values_from_file);
            ++it)
        {
            double val;
            if(in_file >> val)
            {   
                it->second.push_back(val);
            }   
        }
    }
}
#包括
#包括
int main()
{
std::映射来自_文件的值;
std::ifstream在_文件(“test.txt”)中;
std::字符串第一行;
std::getline(在文件中,第一行);
std::istringstream firstLineInput(第一行);
//阅读所有标签(符号)
做
{
字符串标签;
firstLineInput>>标签;
_文件[label]=std::vector()中的值_;
}while(firstLineInput);
//按列读取所有值
typedef std::map::迭代器;
while(在_文件中)
{
for(It=std::begin(来自_文件的值);
it!=std::end(值来自\u文件);
++(it)
{
双val;
如果(在文件中>>val)
{   
它->秒。推回(val);
}   
}
}
}

你能提供一个输入文件的例子,以及你希望从中得到什么吗?你是否尝试过在谷歌上搜索,首先看一下可能是一个
std::map
使用
std::string
键作为“变量符号”,而一种“variant”类型的值容器会做你想做的事情。你应该用一个示例输入和一些(伪)代码详细说明(编辑)你的问题,你打算如何使用它。我希望它给出这样的频率:!频率:时间,频率:时间;/=开始=你有一个我可以照顾的方面吗?@ LXPOXX:是一个关于C++的输入/输出教程,如果这就是你的意思。