C++: why is vector implementation not yielding comma-delimited values?

时间:2018-03-25 21:10:15

标签: c++

I have a database.txt file with comma-separated values:

Name,ID,Year,Gender

I would like to extract each of these elements.

I've started with this code (I've already looked at all the other similar questions and implemented what they've suggested), but it's not printing each piece:

// reading a text file
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;

int main () {
  string line;
  ifstream myfile ("database.txt");
  if (myfile.is_open())
  {
    while ( getline (myfile,line) )
    {
      std::string str = line;
      std::vector<int> vect;

      std::stringstream ss(str);

      int i;

      while (ss >> i)
      {
        vect.push_back(i);

        if (ss.peek() == ',')
            ss.ignore();
      }

      for (i=0; i< vect.size(); i++)
        std::cout << vect.at(i)<<std::endl;
        //cout << line << '\n';
      }

    myfile.close();
  }

  else cout << "Unable to open file"; 

  return 0;
}

How can I modify it to be able to extract each value: name, ID, year, and gender? What am I doing wrong?

2 个答案:

答案 0 :(得分:1)

使用此功能分割每一行:

npm install express-generator -g

您的代码如下:

vector<string> split(const string &s, char delim) {
    stringstream ss(s);
    string item;
    vector<string> tokens;
    while (getline(ss, item, delim)) {
        tokens.push_back(item);
    }
    return tokens;
}

答案 1 :(得分:0)

借助实用程序功能和数据结构,您可以非常轻松地简化这一过程。

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

std::vector<std::string> splitString( const std::string& stringToSplit, const std::string& delimiter, const bool keepEmpty ) {
    std::vector<std::string> results;
    if ( delimiter.empty() {
        results.push_back( stringToSplit );
        return results;
    }

    std::string::const_iterator itSubStrStart = stringToSplit.begin(), itSubStrEnd;
    while( true ) {
        itSubStrEnd = std::search( itSubStrStart, stringToSplit.end(), delimiter.begin(), delimiter.end() );
        std::string temp( itSubStrStart, itSubStrEnd );

        if ( keepEmpty || !temp.empty() ) 
            results.push_back( temp );

        if ( itSubStrEnd == stringToSplit.end() )
            break;

        itSubStrStart = itSubStrEnd + delimiter.size();
    }
    return results;
}

struct DataEntry {
    std::string name;
    unsigned id;
    unsigned year;
    std::string gender;
};

int main() {
    std::string line;
    std::fstream file;

    file.open( "database.txt" );

    std::vector<DataEntry> entries;
    std::vector<std::string> elements;

    while( file >> line ) {
        elements = splitString( line, "," );
        DataEntry entry;
        entry.name = elements[0];
        entry.id   = std::stoul( elements[1] );
        entry.year = std::stoul( elements[2] );
        entry.gender = elements[3];
        entries.push_back( entry );
    }

    file.close();

    for ( auto& e : entries ) {
        std::cout << e.name << " " << e.id << " "
                 << e.year << " " << e.gender << '\n';
    }

    std::cout << "\nPress any key and enter to quit.\n";
    std::cin.get();
    return 0;
}

database.txt

John,12345,2010,M
Jane,54321,2012,F

output

John 12345 2010 M
Jane 54321 2012 F

通过首先阅读单行,这使生活变得更加容易;然后解析那行文本,并从那里做你将使用该数据;要么将它存储到结构中,打印它,操纵它等等。

Edit

你需要注意这样一个事实,即在阅读文本行时,如果在文本文件中有这样的内容,则解析它们:

John Doe,12345,2010,M

它不会给你你所期望的。我会把它留给你弄清楚。