C++: Reading from and writing to a file

Me being the idiot that I am, I am not satisfied that I have just recently finally really gotten my random wallpaper script working with Python. Nooo, now I feel like a sell-out since I’d already written it in C++ once and unfortunately lost that somehow.
So now that it works in Python, of course I just had to decide to write it in C++ again too.
I found, though, that I am severely lacking in C++ knowledge. All the tutorials I followed almost a year ago now (I think it was at least 3) have all been next to lost from my mind.

Fortunately I had decided on completing a very simple task for today, since it was already 10pm when I started on this: Read all lines from a file and be able to write a line to a file, the rest will come later.

I started out with creating a header file that looked like this:

#include <string>

class FileRW {
void add(std::string filename);
void read();

Declaring a class called FileRW and 2 public functions: add, read

Now for the simplest of implementations of these functions we’re going to read a file with read() and add a line with write()

first we have to include

#include <iostream>
#include <fstream>
#include <string>
#include "filerw.h"

That last one is, of course, to include the header file I wrote before, which I called filerw.h

to keep it simple we’ll also be

using namespace std;

otherwise we’d be writing std:: everywhere, which is just not so much fun really…

then the functions:

void PaperConfig::read() {
string line; // string where a line will be stored

// Open the file for reading
ifstream myfile("test.txt");
// If the file was opened
if (myfile.is_open()) {
// Until it reaches the end of the file
while (!myfile.eof()) {
// Get the next line
getline(myfile, line);

// If it's not an empty line, print it
if (line != "")
cout << line << endl;

// Close the file
// If the file was not opened
cout << "Unable to open file";

We use ifstream (in file stream?) to open the file for reading.
We use the line variable as a temporary variable to store each line we read in so that we can write it to the console (or do something else with it). We use myfile.is_open() to check if the file could be opened, if not then it’ll tell us and we use myfile.eof() to check if we have anything left to work with.
If the currently read line does not equal “” (an empty string).
When we’re done we close the file.

void PaperConfig::add(string filename) {
// Open file for writing and set the append flag
ofstream myfile("test.txt", ios::app);

// Write filename to file
myfile << filename << endl;

// Close the file

Here we use the ofstream (out file stream?) to open the file for writing and the ios::app flag to tell the stream to append whatever we write to the end of the file.
Then we write our parameter string to the myfile object like we write a lot of other things to cout and we add an endline character (endl)
Finally we close the file, this way we don’t keep things in memory we don’t need to.

Now I hope I have explained enough in this post, I don’t often write things like this (in case you don’t get it this is my version of a tutorial, but even I can’t really call it that because it is writting in about 30 minutes) so if you see room for improvement please let me know. I will try to write more things like this in the future and hope to get better at writing, C++ and software development in general.


One response to “C++: Reading from and writing to a file

  1. I realised I forgot to mention and talk about some stuff, so I posted some more explanations: hereI probably forgot more, so let me know if you want to know anything.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s