Created
March 8, 2016 18:47
-
-
Save asadw1/59261331fd2b96643242 to your computer and use it in GitHub Desktop.
Reading a field out of a data file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Data File format looks like this: | |
FirstName|LastName|Address|City|Zipcode|Occupation|Birth | |
*/ | |
ifstream myFile; | |
myFile.open(filepath); | |
string s_line, field; //field is the text between the pipes | |
while (!myFile.eof()) { //while not at the end of the file... | |
int pipeCount = 0; | |
getline(myFile, s_line); // grab the entire line from the file | |
stringstream line(s_line); //build a stringstream instance of it | |
while (line.good()) { //while not at the end of line... | |
getline(line, field, '|'); //grab each piece of text between the delimeter | |
pipeCount++; //keep track of how many pipes you've traversed, or which column you're in | |
if (pipeCount == 4) { // figure out how many columns over your zip code is | |
int zip_code = stoi(field, field.length()); //convert the string of ##### into an int | |
myVectorOfZipCodes.push_back(zip_code); // save all the zip codes in a vector or whatever you want | |
break; // exit local while loop and proceed to next line. | |
} | |
field.clear(); | |
} | |
} | |
myFile.close(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment