1
readInputRecord(ifstream &inputFile, 
string &taxID, string &firstName, string &lastName, string &phoneNumber) {      
    while (!inputFile.eof()) {
        inputFile >> firstName >> lastName >> phoneNumber >> taxID;     
    }   
}

As you can see I read in the data like you would a standard read inputfile. The trouble is the data fields can be blank such as, ", ," and include no data-between the parenthesis. I've been reading forums here and elsewhere and a common method seems to be using getline(stuff, stuff, ','), but then that will read in data stopping at the comma. What is the method to include the commas because the output file should read and then output ", ," for the variable if that is read.

Jerry Coffin
  • 437,173
  • 71
  • 570
  • 1,035

2 Answers2

1

You don't need to explicitly read the ',' to be sure there has been a ',' and std::getline(...) offers a valid solution in combination with std::stringstream

// Read the file line by line using the 
// std line terminator '\n'    

while(std::getline(fi,line)) { 
    std::stringstream ss(line);                    
    std::string cell;                              

    // Read cells withing the  line by line using  
    // ',' as "line terminator"        
    while(std::getline(fi,cell,',')) {
        // here you have a string that may be '' when you got
        // a ',,' sequence
        std::cerr << "[" << cell << "]" << std::endl; 
    } 
} 
Oncaphillis
  • 1,829
  • 10
  • 14
0

If you have boost-dev installed, then include header file <boost/algorithm/string.hpp>

void readInputRecord(std::ifstream &inputFile, std::vector<std::string>& fields) {
    std::string line;
    fields.clear();
    while (std::getline(inputFile, line)) {
            boost::split(fields, line, boost::is_any_of(","));
            for (std::vector<std::string>::iterator it = fields.begin(); it != fields.end(); ++it)
                std::cout << *it << "#";

            std::cout << std::endl;
    }
}

The all fields are contained in the vector, include the empty field. The code is not tested, but should work.

大宝剑
  • 3,564
  • 4
  • 25
  • 46