Skip to content

Commit af8b41a

Browse files
committed
add readfile example
1 parent cca6ae5 commit af8b41a

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

CMakeLists.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ set(CXX_STANDARD_REQUIRED ON)
88
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
99

1010

11-
add_subdirectory(WritingFile)
11+
add_subdirectory(WritingFile)
12+
add_subdirectory(ReadingFile)

ReadingFile/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
project(ReadingFile)
2+
3+
add_executable(${PROJECT_NAME} main.cpp)

ReadingFile/main.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <fstream>
2+
#include <iostream>
3+
#include <regex>
4+
#include <string>
5+
6+
int main(int argc, char **argv) {
7+
if (argc < 2) {
8+
std::cerr << "usage: " << argv[0] << " <filename>\n";
9+
return 0x1;
10+
}
11+
12+
std::ifstream *file = new std::ifstream();
13+
14+
file->open(argv[1], std::ios::in); // open file with in mode
15+
16+
if (file->is_open()) {
17+
std::string data;
18+
19+
// read file until eof is hit
20+
while (!file->eof()) {
21+
std::getline(*file, data);
22+
std::cout << data << std::endl;
23+
}
24+
} else {
25+
std::cerr << "Unable to open file \n";
26+
}
27+
28+
file->close(); // close handle
29+
30+
delete file;
31+
file = nullptr;
32+
return 0x0;
33+
}

0 commit comments

Comments
 (0)