简体   繁体   English

将制表符分隔的值从文本文件读取到数组中

[英]Reading tab-separated values from text file into array

Hey I am quite new to C++ and I am facing a problem: 嘿,我是C ++的新手,现在遇到了一个问题:

I have a textfile which looks like this: 我有一个看起来像这样的文本文件:

500

1120

10

number1,1 number1,2   ...   number1,500

number2,1

.

.

number1120,1

So the first two values on top of the text-file describe the dimensions of the matrix. 因此,文本文件顶部的前两个值描述了矩阵的尺寸。 I now want to write a code, which reads all the files from the matrix into an array or vector of int values. 我现在想编写一个代码,该代码将矩阵中的所有文件读入int值的数组或向量中。 I can read the first three values (500, 1120,10) and write them into an integer value using getline and stringstream , but I can't figure out how to read the matrix tap separated with a loop. 我可以读取前三个值(500、1120、10),并使用getlinestringstream将它们写入整数值,但是我不知道如何读取用循环分隔的矩阵抽头。

Something like this: 像这样:

#include <iostream>
#include <sstream>

// Assume input is 12,34,56. You can use 
// getline or something to read a line from
// input file. 
std::string input = "12,34,56";

// Now convert the input line which is string
//  to string stream. String stream is stream of 
// string just like cin and cout. 
std::istringstream ss(input);
std::string token;

// Now read from stream with "," as 
// delimiter and store text in token named variable.
while(std::getline(ss, token, ',')) {
    std::cout << token << '\n';
}

You may consider to read the matrix line by line with a loop, and split the current line using a tokenizer (eg std::strtok ) or a nested loop that splits the line at your delimiters. 您可以考虑通过循环逐行读取矩阵,并使用标记器(例如std::strtok )或嵌套循环(在定界符处进行分割)来拆分当前行。 There is a thread about tokenizers . 关于标记化器有一个主题。

Thanks for al the answers I can now read all the data, tabseperated into an array, but unfortunally I run into a very weired problem. 感谢您的回答,我现在可以读取所有数据,细分为数组,但是不幸的是,我遇到了一个非常奇怪的问题。 So this is my code: 这是我的代码:

// Matrix deklarieren 
    long int** mat_dat = new long int *[m_height];
    for (long int a = 0; a < m_height; ++a)
        mat_dat[a] = new long int[m_width];

    //ganzes Feld als String einlesen


    //Daten aus Textdatei lesen
    long int i = 0;
    long int j = 0;
    long int value = 0;
    while (myfile.good())
    {
        getline(myfile, test);
        istringstream ss(test);
        string token;
        while (std::getline(ss, token, '\t'))
        {
            cout << token << '\t';
            value = atoi(token.c_str());
            mat_dat[j][i] = value;
            i++;
        }
        if (j == m_depth)
        {
            break;
        }
        j++;
        i = 0;

It safes all the data perfectly into the array, thats what I thought. 它将所有数据完美地保护到阵列中,这就是我的想法。 But when I look closly only the first 512 values of the array are correct (mat_dat[0][512] are correct) and (mat_dat[1][512] are correct. The following values in the row from mat_dat[0][512] to mat_dat[0][1120] are incorrect. Do you have any clue why it is starting to becom incorrect right at that place ? 但是当我仔细观察时,只有数组的前512个值是正确的(mat_dat [0] [512]是正确的)和(mat_dat [1] [512]是正确的。mat_dat [0] [ 512]到mat_dat [0] [1120]是不正确的,您有什么线索为什么在那个地方开始变得不正确?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM