Search
 
SCRIPT & CODE EXAMPLE
 

CPP

how to read a comma delimited file into an array c++

#include <fstream>
#include <sstream>
#include <iostream>
#include <vector>

int main()
{
    std::ifstream inFile("registration.txt");
    if (inFile.is_open())
    {
        std::string line;
        while( std::getline(inFile,line) )
        {
            std::stringstream ss(line);

            std::string ID, fname, lname;
            std::getline(ss,ID,',');    std::cout<<"""<<ID<<""";
            std::getline(ss,fname,','); std::cout<<", ""<<fname<<""";
            std::getline(ss,lname,','); std::cout<<", ""<<lname<<""";

            std::vector<std::string> enrolled;
            std::string course;
            while( std::getline(ss,course,',') )
            {
                 enrolled.push_back(course); std::cout<<", ""<<course<<""";
            }
            std::cout<<"
";
        }
    }
    return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: check if float has decimals c++ 
Cpp :: count occurrences of character in string c++ 
Cpp :: convert long int to binary string c++ 
Cpp :: how to compare lower case character to uppercase cpp 
Cpp :: how to clear console c++ 
Cpp :: c++ measure time in microseconds 
Cpp :: maximum int c++ 
Cpp :: C++ do...while Loop 
Cpp :: cpp case 
Cpp :: c++ replace string 
Cpp :: c++ call by value vs call by reference 
Cpp :: 2d vector c++ size 
Cpp :: max value of double c++ 
Cpp :: conditional variable c++ 
Cpp :: ue4 c++ enumaeration 
Cpp :: cpp binary tree 
Cpp :: c++ init multidimensional vector 
Cpp :: c++ get type name 
Cpp :: when was c++ created 
Cpp :: string in cpp 
Cpp :: c++ vector move element to front 
Cpp :: built in factorial function in c++ 
Cpp :: c++ remove text file 
Cpp :: ue4 float to fstring 
Cpp :: C++ Volume of a Cylinder 
Cpp :: c++ initialize vector of vector with size 
Cpp :: modulo subtraction 
Cpp :: std vector random shuffle 
Cpp :: cpp func as const 
Cpp :: how to use toString method in C++ 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =