Skip to content

Instantly share code, notes, and snippets.

@KentaYamada
Created August 3, 2016 11:24
Show Gist options
  • Save KentaYamada/582eda89209cb9d6f5638fcb52c884a1 to your computer and use it in GitHub Desktop.
Save KentaYamada/582eda89209cb9d6f5638fcb52c884a1 to your computer and use it in GitHub Desktop.
ofsstreamのサンプルコード
#include <iostream>
#include <string>
#include <fstream>
int main()
{
std::string data = "hoge, piyo, fuga";
std::ofstream ofs;
ofs.open("./test.txt", std::ios::out | std::ios::trunc);
if(!ofs.is_open()) {
std::cout << "file open failed." << std::endl;
return -1;
}
for(int i = 0; i < 1000; i++) {
ofs << "No." << i << " " << data << std::endl;
if(ofs.fail()) {
std::cout << "write error." << std::endl;
ofs.close();
return -1;
}
}
ofs.close();
std::cout << "test.txt created." << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment