Skip to content

Instantly share code, notes, and snippets.

@robnolen
Created January 25, 2012 05:23
Show Gist options
  • Save robnolen/1674893 to your computer and use it in GitHub Desktop.
Save robnolen/1674893 to your computer and use it in GitHub Desktop.
//
// main.cpp
// StringStream
//
// Created by Robert Nolen on 11/20/11.
// Copyright 2011 Greencrayon.org. All rights reserved.
//
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
using namespace std;
int main (int argc, const char * argv[])
{
ifstream myfile ("/Users/rob/Documents/FinalProject/Biology.txt", ios::in);
char line[20];
int lineCount = 1;
int temp;
vector<int> myvec1;
vector<int> myvec2;
vector<int> myvec3;
stringstream ss;
while (myfile.getline(line, 20, '\n'))
{
ss << line;
switch (lineCount)
{
case 1:
while (ss >> temp)
{
myvec1.push_back(temp);
}
break;
case 2:
while (ss >> temp)
{
myvec2.push_back(temp);
}
break;
case 3:
while (ss >> temp)
{
myvec3.push_back(temp);
}
break;
default:
break;
}
ss.clear();
lineCount++;
}
cout << "Vector 1 values: ";
for(vector<int>::iterator n = myvec1.begin();
n != myvec1.end();
++n)
{
cout << *n << " ";
}
cout << endl;
cout << "Vector 2 values: ";
for(vector<int>::iterator o = myvec2.begin();
o != myvec2.end();
++o)
{
cout << *o << " ";
}
cout << endl;
cout << "Vector 3 values: ";
for(vector<int>::iterator p = myvec3.begin();
p != myvec3.end();
++p)
{
cout << *p << " ";
}
cout << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment