Last active
December 17, 2015 10:41
-
-
Save fndari/3f16496e5035a8a06ff8 to your computer and use it in GitHub Desktop.
Basic read and write with ASCII files with C++
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
#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