简体   繁体   English

将文本从 a.txt 文件导入二维字符串数组

[英]Importing text from a .txt file into a 2D array of strings

I've been trying to import text from a .txt file into a 2D array of string, but it doesn't seem to be working.我一直在尝试将.txt文件中的文本导入二维字符串数组,但它似乎不起作用。 Each row in the .txt file has three values/elements separated that I need to copy. .txt文件中的每一行都有我需要复制的三个分隔的值/元素。

This is the code:这是代码:

// i am only allowed to use these libraries.
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;

int main()
{
    const int rows = 38;
    const int columns = 3;

    string companies[rows][columns];

    // Inputting file contents
    ifstream file;
    
    file.open("companies.txt");

    while(!file.eof())
    {
        for(int i = 0; i < 38; i++)
        {
            for(int j = 0; j < 3; j++)
            {
                getline(file, companies[i][j], ',');
            }
        }
    }
    
    file.close();   
    
    cout << endl << endl;
    
    // displaying file contents using for loop

    for(int i = 0; i < 38; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            cout << companies[i][j] << endl << endl;
        }
    }
    
    cout << endl << endl;

    return 0;               
}

This is the data that I want to import:这是我要导入的数据:

Symbol,Company Name,Stock Price
ATRL,Attock Refinery Ltd.,171.54
AVN,Avanceon Ltd. Consolidated,78.1
BAHL,Bank AL-Habib Ltd.,54.97
CHCC,Cherat Cement Company Ltd.,126.26

One of the problems with your code is that you are only looking for , as a delimiter and not handling line breaks between the rows at all.您的代码存在的问题之一是您只是在寻找,作为分隔符,根本不处理行之间的换行符。 Normally, I would suggest reading each row into a std::istringstream and then use std::getline(',') to parse each stream, but you say that you are not allowed to use <sstream> , so you will just have to parse each row manually using std::string::find() and std::string::substr() instead.通常,我建议将每一行读入std::istringstream ,然后使用std::getline(',')解析每个 stream,但是您说您不允许使用<sstream> ,所以您将拥有改为使用std::string::find()std::string::substr()手动解析每一行。

Also, using while(.file.eof()) is just plain wrong .此外,使用while(.file.eof())完全错误的。 Not just because it is the wrong way to use eof() , but also because your for loops are handling all of the data, so there is really nothing for the while loop to do.不仅因为使用eof()的方法不对,还因为您的for循环正在处理所有数据,因此while循环实际上无事可做。

Try something more like this instead:尝试更像这样的东西:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    const int max_rows = 38;
    const int max_columns = 3;

    string companies[max_rows][max_columns];

    string line;
    string::size_type start, end;

    // Inputting file contents
    ifstream file("companies.txt");

    int rows = 0;
    while (rows < max_rows && getline(file, line))
    {
        start = 0;
        for(int j = 0; j < max_columns; ++j)
        {
            end = line.find(',', start);
            companies[rows][j] = line.substr(start, end-start);
            start = end + 1;
        }

        ++rows;
    }
    
    file.close();   
    
    cout << endl << endl;
    
    // displaying file contents using for loop

    for(int i = 0; i < rows; ++i)
    {
        for(int j = 0; j < max_columns; ++j)
        {
            cout << companies[i][j] << endl << endl;
        }
    }
    
    cout << endl << endl;

    return 0;               
}

Online Demo在线演示

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

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