Skip to content

Instantly share code, notes, and snippets.

@blippy
Created March 11, 2016 14:52
Show Gist options
  • Save blippy/c469714259da25317d84 to your computer and use it in GitHub Desktop.
Save blippy/c469714259da25317d84 to your computer and use it in GitHub Desktop.
Example of reading in a file in C++
#include "mcstats.h"
#include <assert.h>
#include <stdlib.h>
//#include <stdio>
#include <iostream>
#include <fstream>
#include <string>
using std::cout;
using std::endl;
int main()
{
std::ifstream ifs;
ifs.open("../maff/xlsm/sort.txt");
std::string str;
std::getline(ifs, str);
cout << str << endl;
int n = stoi(str);
std::vector<double> ds;
assert(n>0);
for(int i=0; i<n; i++){
std::getline(ifs, str);
double d = stod(str);
ds.push_back(d);
}
ifs.close();
sortd(ds);
for(int i=0; i<n; i++){
cout << ds[i] << endl;
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment