Skip to content
Permalink
Browse files
Updated TextFileIO activity for CLion
  • Loading branch information
aa6164 committed Sep 7, 2022
0 parents commit 8662c4bdc9869408d6bc7be49155fc9b90d484bb
Showing 10 changed files with 204 additions and 0 deletions.

Some generated files are not rendered by default. Learn more.

Some generated files are not rendered by default. Learn more.

Some generated files are not rendered by default. Learn more.

Some generated files are not rendered by default. Learn more.

Some generated files are not rendered by default. Learn more.

@@ -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)
@@ -0,0 +1,4 @@
this is text
from inside
input.txt
hello text file input!
@@ -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
159 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 <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <filesystem>

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;
}
@@ -0,0 +1,2 @@
Hello text file!
This will be inside output.txt

0 comments on commit 8662c4b

Please sign in to comment.