getline()以行结尾和下一行开头

时间:2018-05-05 15:23:44

标签: c++ getline

我的任务之一有问题。我有一个Excel文件,如下所示:

File I take information from

问题在于getline()在找到','之前会占用字符串,但在该行的末尾没有','。有太多的数据我无法将它们放在任何地方。

我的问题是,如何将','放在我的代码的行尾?

这是我的代码。

#include <string>
#include <iostream>
#include <fstream>
using namespace std;
void names(string value);
void names1(string value);
int main()
{
    ifstream file ("15mil.csv"); // declare file stream: 
    string value;

    for(int i=0; i<100; i++) {
        getline (file, value, ',');
        names(value);
    }

}

void names(string value) {
    if(value.find('.') != std::string::npos) {
        cout << "float: " << value << endl;
    }
    if(value.find('/') != std::string::npos) {
        cout << "data: " << value << endl;
    }
    if(value.length() == 1) {
        cout << "char: " << value << endl;
    }

    if(value.length() > 1) {
        if(value.find('1') == std::string::npos && value.find('2') == std::string::npos && value.find('3') == std::string::npos && value.find('4') == std::string::npos && value.find('5') == std::string::npos && value.find('6') == std::string::npos && value.find('7') == std::string::npos && value.find('8') == std::string::npos && value.find('9') == std::string::npos && value.find('0') == std::string::npos) {
            cout << "string: " << value << endl;
        }

        else if(value.find('.') == std::string::npos && value.find('/') == std::string::npos && value.length() != 1) {
            cout << "int: " << value << endl;
        }
    }

    if(value.length() > 1 && value.find('S') != std::string::npos) {
        cout << "string: " << value << endl;
    }


}


void names1(string value) {
    if(value.length() > 1) {
        if(value.find('1') == std::string::npos && value.find('2') == std::string::npos && value.find('3') == std::string::npos && value.find('4') == std::string::npos && value.find('5') == std::string::npos && value.find('6') == std::string::npos && value.find('7') == std::string::npos && value.find('8') == std::string::npos && value.find('9') == std::string::npos && value.find('0') == std::string::npos) {
            cout << "string: " << value << endl;
        }
}
}

1 个答案:

答案 0 :(得分:0)

在getline之后,在value += ',';value.push_back(',');的帮助下,在此处添加:

for(int i=0; i<100; i++) {
    getline (file, value, ',');

    value += ','; // Or value.push_back(',');
    names(value);
}