简体   繁体   English

C++ 如何将数字存储在文本文件中,其中字符、数字和逗号的内容以二维矩阵分隔

[英]C++ How to store numbers in a text file with contents of characters , numbers , and comma delimited in a 2D matrix


I am creating a C++ program that reading a text file with contents我正在创建一个 C++ 程序,用于读取包含内容的文本文件

San Jose, 2, 10 , 1<br/> Sunnyvale, 2, 5, 4<br/> Gilroy, 8, 4, 3<br/> Oakland, 5, 8, 9<br/> mountain View, 7, 5, 12<br/> Santa Clara, 6, 4, 6<br/> Fremont, 1, 2, 0<br/> Belmont, 4, 9, 5<br/>

My attempt is to store all the numbers in a matrix (8x3).我的尝试是将所有数字存储在一个矩阵 (8x3) 中。 Here is my code这是我的代码

int main () {
vector <vector <string> > data;
ifstream infile( "test.txt" );
int data_row = 0;
while (infile)
{
string s;
if (!getline( infile, s )) break;
data_row++;
istringstream ss( s );
vector <string> record;
while (ss)
{
  string s;
  if (!getline( ss, s, ',' )) break;
  record.push_back( s );
}
data_column = record.size() - 1;
data.push_back( record );
}
if (!infile.eof())
{
cerr << "Fooey!\n";
}

cout << data_row << " " << data_column << "\n";

data_type** m_value;
m_value = new  data_type*[data_row];
for(int i=0;i<data_row;i++) {
  m_value[i] = new data_type[data_column];
}
vector<string>myvector;
for (int i = 0; i < data.size(); i++) {
    for (int j = 0; j < data[i].size(); j++) {
        cout << i << "****" << j << " " << data[i][j] << "\n ";
      if ( j != 0) {
        myvector.push_back(data[i][j]);
}
} 
}
for(int i=0;i<data_row;i++) {
for(int j=0;j< data_column;j++) {
  for (int k = 0; k < myvector.size(); k++) {
    cout << k << " " << myvector[k] << "\n";
     m_value[i][j] = myvector[k];
}
}}

The program fails with getline.cpp:57:24: error: cannot convert程序失败 getline.cpp:57:24: 错误: 无法转换
'std::basic_string' to 'int' in assignment 'std::basic_string' 到 'int' 赋值
m_value[i][j] = myvector[k]; m_value[i][j] = myvector[k];

Please help me to resolve the problem请帮我解决问题
Thank you谢谢

std::stoi converts a string to an integer. std::stoi将字符串转换为整数。 If you are using decimal numbers use std::stod .如果您使用十进制数字,请使用std::stod That means string to double m_value[i][j] = std::stoi(myvector[k]);这意味着字符串要翻倍m_value[i][j] = std::stoi(myvector[k]);

Please go and eat/read a good book on C++请去吃/读一本关于 C++ 的好书

#include <cstddef>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <vector>
#include <iterator>

template<typename T, std::size_t N>
std::size_t sizeof_array(T(&)[N])
{
    return N;
}

struct record
{
    std::string name;
    int a;
    int b;
    int c;
};

std::istream& operator>>(std::istream &is, record &r)
{
    record tmp;
    std::getline(is, tmp.name, ',');

    char ch;    
    int *dst[] = { &tmp.a, &tmp.b, &tmp.c };

    for (std::size_t i{}; i < sizeof_array(dst); ++i)
    {
        if(!( is >> *dst[i] ))
            return is;
        if (i < sizeof_array(dst) - 1)
            is >> ch;
    }

    while (is.peek() != '\n' && is.peek() != EOF)
        is.get();
    is.get();

    r = tmp;

    return is;
}

std::ostream& operator<<(std::ostream &os, record const &r)
{
    os << r.name << '\t' << r.a << '\t' << r.b << '\t' << r.c;
    return os;
}

int main()
{
    constexpr char const *input_file_name{ "input.txt" };
    std::ifstream input_file{ input_file_name };

    if (!input_file) {
        std::cerr << "Failed to open \"" << input_file_name << "\" for reading!\n\n";
        return EXIT_FAILURE;
    }

    std::vector<record> data{ std::istream_iterator<record>{input_file}, std::istream_iterator<record>{} };
    for (auto const &d : data)
        std::cout << d << '\n';
}

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

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