需要阅读文件和其他内容的理论帮助

发布于 2024-12-04 19:19:40 字数 488 浏览 0 评论 0原文

我想在理论部分请求一些帮助,所以磨那些齿轮,

我想将一个文件加载到我的程序中,它看起来像这样:

0,10,10#0,100,40...

好吧,我现在想做的是取出每个逗号分隔的数字并通过我的函数发送

void func(int, float, float);

主题标签意味着它是一个新块,因此它将像 func(0,10,10) 一样发送,之后它将发送 func(0,100,40) 等等。

我想检查每个字符,直到遇到“,”,然后将其放入向量中,然后继续直到遇到“#”。然后它会解雇我的函数(如 func(v[0],v[1],v[2]) ,然后一遍又一遍地做同样的事情,直到 EOF!

这是一个好方法吗?有更好的方法吗?想法?这些数字稍后也会变得非常大,所以我不知道我需要多少内存(因此向量)。或者我应该使用 3 个临时整数和浮点数,然后触发该函数并重新开始!

i would like to request some help on theory part, so grind those gears, here it comes

I want to load a file into my program, which looks something like this:

0,10,10#0,100,40...

Okay what i now want to do is to take out every comma separated number and send it through my function

void func( int, float, float );

The hashtag means it's a new block, so it would be sent like func(0,10,10) and after that it would send func(0,100,40) and so on.

I was thinking to check every char until i meet ',' and after that put it in a vector, and continue that until the '#' is met. Then it would fire away my function (like func(v[0],v[1],v[2]) and then just do the same thing over and over until EOF!

Is this a good way to go? Have any better ideas? Those numbers can also get very large later on, so i don't know how much memory i need (therefor the vector). Or should i just go with 3 temp ints and floats and then fire the function and start over!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

恋竹姑娘 2024-12-11 19:19:40

按照您的建议逐个字符并使用状态机是最快的方法。
然而,最简单的方法是首先用 # 分割,然后对每个结果字符串用 , 分割。
您可以使用 boost 库来执行字符串 split< /a>.

Going char by char and using a state machine like you suggested is the fastest way.
However the easiest way is first to split by the # and then for each result string split by ,.
You can use boost library to do the string split.

旧人哭 2024-12-11 19:19:40
#include <fstream>
#include <ostream>
#include <istream>
#include <stdexcept>

void func( std::vector<float> &numbers )
{}

int main() {
    std::ifstream myfile("myfile.txt");

    float number;
    char seperator;
    std::vector<float> numbers;
    while( myfile >> number) { //read number
        numbers.push_back(number); //and remember it
        if (!(myfile >> seperator) || seperator == "#") { //if # or EOF or error
            func(numbers); //run function
            numbers.clear();  //and start over
        }
    } //only gets here at EOF or malformed file
    return 0;
}

非常简单、快速、容易。

#include <fstream>
#include <ostream>
#include <istream>
#include <stdexcept>

void func( std::vector<float> &numbers )
{}

int main() {
    std::ifstream myfile("myfile.txt");

    float number;
    char seperator;
    std::vector<float> numbers;
    while( myfile >> number) { //read number
        numbers.push_back(number); //and remember it
        if (!(myfile >> seperator) || seperator == "#") { //if # or EOF or error
            func(numbers); //run function
            numbers.clear();  //and start over
        }
    } //only gets here at EOF or malformed file
    return 0;
}

Very simple, fast, and easy.

清欢 2024-12-11 19:19:40

如果您确定文件以三个一组的第一个 int 开头

ifstream fin("foo.csv");
string str;
stringstream s_str;
while(!fin.eof())
{
    int a;
    float b,c;
    getline(fin,str,',');
    s_str.str(str);
    s_str >> a;
    getline(fin,str,',');
    s_str.str(str);
    s_str >> b;
    getline(fin,str,'#');
    s_str.str(str);
    s_str >> c;
}

应该可以工作。 (我没有编译过,所以可能有错别字等)

If you're certain the file starts with the first int of a group of three

ifstream fin("foo.csv");
string str;
stringstream s_str;
while(!fin.eof())
{
    int a;
    float b,c;
    getline(fin,str,',');
    s_str.str(str);
    s_str >> a;
    getline(fin,str,',');
    s_str.str(str);
    s_str >> b;
    getline(fin,str,'#');
    s_str.str(str);
    s_str >> c;
}

should work. (I haven't compiled it so there might be typos etc)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文