Skip to content

Instantly share code, notes, and snippets.

@krysseltillada
Created July 18, 2015 09:43
Show Gist options
  • Save krysseltillada/f76b36e80ec8616a3589 to your computer and use it in GitHub Desktop.
Save krysseltillada/f76b36e80ec8616a3589 to your computer and use it in GitHub Desktop.
standard library file i/o
#include <iostream> /// std::cout; std::endl;
#include <fstream> /// std::ifstream; std::ofstream;
#include <vector> /// std::vector;
int main ()
{
std::vector <std::string> vec_data; /// declares and define vector name vec_data that stores strings
std::string temp_store; /// declares and define temp_store to store a temporary data
std::ifstream read("data.txt"); /// declares and define read that opens the file name data.txt
if (!read) { /// if the stream is in error state or failbit then execute this code
std::cout << "failed to open the file " << std::endl;
goto EXIT; /// jumps to statement EXIT:
}
while (std::getline(read, temp_store)) { /// reads the file and stores to temp_store ends until the end of file (eofbit)
vec_data.push_back (temp_store); /// stores each data in a vector vec_data
}
for (auto vd : vec_data) /// displays the data stored in a vector vec_data
std::cout << vd << std::endl;
EXIT:
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment