Skip to content

Instantly share code, notes, and snippets.

@mshafae
Last active April 16, 2024 21:12
Show Gist options
  • Save mshafae/4bde90c3c116929a03730c7841fe9561 to your computer and use it in GitHub Desktop.
Save mshafae/4bde90c3c116929a03730c7841fe9561 to your computer and use it in GitHub Desktop.
CPSC 120 Example of reading a file line by line, storing it in a std::vector, and then selecting random lines from the vector to print.
// Gist https://gist.github.com/mshafae/4d282935ea5deaf8c2e6aca83a9fcac8
// Filename cpsc120_filelines_to_vector.cc
// CompileCommand clang++ -std=c++17 cpsc120_filelines_to_vector
// Read a file line by line and place contents into a std::vector
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
int main(int argc, char const *argv[]) {
std::vector<std::string> arguments{argv, argv + argc};
if (arguments.size() < 2) {
std::cout << "Please provide a path to a file.\n";
return 1;
}
std::string input_file_name{arguments.at(1)};
// https://en.cppreference.com/w/cpp/io/basic_ifstream
std::ifstream input_file_stream{input_file_name};
if (!input_file_stream.is_open()) {
std::cout << "Could not open the file " << input_file_name << "\n";
return 1;
}
std::vector<std::string> lines;
std::string line_buffer;
while (std::getline(input_file_stream, line_buffer)) {
lines.push_back(line_buffer);
}
if (input_file_stream.eof()) {
std::cout << "End of file reached successfully!\n";
} else if (input_file_stream.bad()) {
std::cout << "I/O error while reading.\n";
return 1;
} else if (input_file_stream.fail()) {
std::cout << "Failure: Long line.\n";
return 1;
}
// When you open something, close it!
input_file_stream.close();
for (const std::string a_line : lines) {
std::cout << a_line << "\n";
}
return 0;
}
// Gist https://gist.github.com/mshafae/4bde90c3c116929a03730c7841fe9561
// Filename cpsc120_filelines_to_vector.cc
// Read a file line by line and place contents into a std::vector
#include <fstream>
#include <iostream>
#include <random>
#include <string>
#include <vector>
std::vector<std::string> ReadFileLinesToVector(std::string input_file_name) {
std::ifstream input_file_stream{input_file_name};
std::vector<std::string> lines;
if (!input_file_stream.is_open()) {
std::cout << "Could not open the file " << input_file_name << "\n";
return lines;
}
while (input_file_stream.good()) {
std::string line_buffer;
std::getline(input_file_stream, line_buffer);
lines.push_back(line_buffer);
}
return lines;
}
int main(int argc, char const *argv[]) {
std::vector<std::string> arguments{argv, argv + argc};
if (arguments.size() < 3) {
std::cout << "Please provide a path to a file and how many random lines "
"you want printed.\n";
return 1;
}
std::string input_file_name{arguments.at(1)};
int n_words{stoi(arguments.at(2))};
std::vector<std::string> lines{ReadFileLinesToVector(input_file_name)};
std::random_device rd;
std::default_random_engine engine(rd());
std::uniform_int_distribution<int> uniform_distribution(0, lines.size() - 1);
for (int count = 0; count < n_words; count++) {
int psuedo_random_number = uniform_distribution(engine);
std::cout << lines.at(psuedo_random_number) << "\n";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment