Skip to content

Instantly share code, notes, and snippets.

@fndari
Last active December 17, 2015 10:41
Show Gist options
  • Save fndari/3f16496e5035a8a06ff8 to your computer and use it in GitHub Desktop.
Save fndari/3f16496e5035a8a06ff8 to your computer and use it in GitHub Desktop.
Basic read and write with ASCII files with C++
#include <fstream>
#include <iostream>
struct Parameters
{
float a;
float b;
float c;
};
void readParams(std::string fname)
{
float x, y, z;
std::ifstream file(fname.c_str());
while (file >> x >> y >> z)
{
// do something with parameters
std::cout << x << std::endl
<< y << std::endl
<< z << std::endl;
}
}
void writeParams(std::string fname, Parameters params)
{
std::ofstream outfile;
outfile.open(fname.c_str());
outfile << params.a << ' '
<< params.b << ' '
<< params.c << ' ';
outfile.close();
}
int main()
{
Parameters myparams;
myparams.a = 2.543;
myparams.b = 3545;
myparams.c = 4.42;
writeParams("file.txt", myparams);
readParams("file.txt");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment