diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..3d7b5c1 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +TEACH_T08_TextFileIO \ No newline at end of file diff --git a/.idea/TEACH-T08-TextFileIO.iml b/.idea/TEACH-T08-TextFileIO.iml new file mode 100644 index 0000000..f08604b --- /dev/null +++ b/.idea/TEACH-T08-TextFileIO.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..79b3c94 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..71c69f2 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..40ed42a --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.23) +project(TEACH_T08_TextFileIO) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(TEACH_T08_TextFileIO main.cpp) diff --git a/input.txt b/input.txt new file mode 100644 index 0000000..ee2f974 --- /dev/null +++ b/input.txt @@ -0,0 +1,4 @@ +this is text +from inside +input.txt +hello text file input! \ No newline at end of file diff --git a/inputdata.txt b/inputdata.txt new file mode 100644 index 0000000..9aa1376 --- /dev/null +++ b/inputdata.txt @@ -0,0 +1,10 @@ +0 5.2 +2 1.3 +4 13.4 +6 1.5 +8 5.6 +10 6.7 +12 7.7 +14 8.8 +16 9.9 +18 10.1 \ No newline at end of file diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..d67633f --- /dev/null +++ b/main.cpp @@ -0,0 +1,159 @@ + +// Text file input and output +// Chris Bass +// Updated October 2019 + +// note: requires C++17 for the filesystem library +// in VS2019 project > C/C++ > properties > language > C++ language standard > C++17 or above +// in CLion > Edit Run Configurations... > Working Directory > Insert Macros... > $ProjectFileDir$ + +#include +#include +#include +#include +#include + +using namespace std; + +int main(int argc, char *argv[]) { + cout << "being exectued from:" << endl; + cout << argv[0] << endl; + + std::filesystem::path cwd = std::filesystem::current_path() / "filename.txt"; // C++17 + cout << "but the input.txt and output.txt files should be be in the following directory:" << endl; + cout << cwd.string() << endl << endl; + + // writing to a text file using an output file stream object + { + cout << "--writing to output.txt started--" << endl; + + ofstream outputFileStream; + + outputFileStream.open("output.txt"); + outputFileStream << "Hello text file!" << endl; + outputFileStream << "This will be inside output.txt" << endl; + outputFileStream.close(); + + cout << "--writing to output.txt finished--" << endl << endl; + } + + // reading from a text file using an input file stream object + { + cout << "--reading from input.txt started--" << endl; + + ifstream inputFileStream; + + inputFileStream.open("input.txt"); + + if (inputFileStream.good()) { + cout << "Input file is good start processing" << endl; + + char inputChar = inputFileStream.get(); + + while (!inputFileStream.fail()) { + cout << inputChar; + inputChar = inputFileStream.get(); + } + + cout << endl; + + if (inputFileStream.eof()) + cout << "Reached the end of file marker" << endl; + else + cout << "Error in input file" << endl; + } else { + cout << "Error opening input file, "; + cout << "check 'input.txt' exists in correct directory" << endl; + } + + + inputFileStream.close(); + + cout << "--reading from input.txt finished--" << endl << endl; + } + + // reading from a text file using an input file stream object + // this uses std::string + { + cout << "--reading from input.txt started--" << endl; + + ifstream inputFileStream; + + inputFileStream.open("input.txt"); + + if (inputFileStream.good()) { + cout << "Input file is good start processing" << endl; + + string inputString; + + inputFileStream >> inputString; + + while (!inputFileStream.fail()) { + cout << inputString << ";"; + inputFileStream >> inputString; + } + + cout << endl; + + if (inputFileStream.eof()) + cout << "Reached the end of file marker" << endl; + else + cout << "Error in input file" << endl; + } else { + cout << "Error opening input file, "; + cout << "check 'input.txt' exists in correct directory" << endl; + } + + inputFileStream.close(); + + cout << "--reading from input.txt finished--" << endl << endl; + } + + // reading from a text file using an input file stream object + // this assumes the data in the text file is in the format of: + // int double int double int double... + { + cout << "--reading from inputdata.txt started--" << endl; + + ifstream inputFileStream; + + inputFileStream.open("inputdata.txt"); + + if (inputFileStream.good()) { + int rowCounter = 1; + int var1; + double var2; + + inputFileStream >> var1; + + while (!inputFileStream.fail()) { + cout << "row" << setw(3) << rowCounter; + cout << " var1 = " << setw(3) << var1; + rowCounter++; + + inputFileStream >> var2; + if (!inputFileStream.fail()) { + cout << " var2 = " << setw(5) << var2; + inputFileStream >> var1; + } + cout << endl; + } + + if (inputFileStream.eof()) + cout << "Reached the end of file marker" << endl; + else + cout << "Error whilst reading input file" << endl; + } else { + cout << "Error opening input file, "; + cout << "check 'inputdata.txt' exists in correct directory" << endl; + } + + inputFileStream.close(); + + cout << "--reading from inputdata.txt finished--" << endl << endl; + } + + cout << "End of program" << endl; + + return 0; +} diff --git a/output.txt b/output.txt new file mode 100644 index 0000000..a865302 --- /dev/null +++ b/output.txt @@ -0,0 +1,2 @@ +Hello text file! +This will be inside output.txt