1

I want to write a function which does this: given a file and a name (string), append to the file if the last line is not 'name' (create the file if the file is not there). Otherwise just do nothing. I can certainly create a function like this:

appendIfLastLineNotEqual(file fd, string name) { ... }

However, this will be hard to write unit test as it dumps to file. Is there a cleaner way to do this, as well as easy to unit test? Also I can do file parsing backward etc to implement the function. Any smart way instead of reading the file, compare with the last line etc? Thanks!

WhatABeautifulWorld
  • 3,198
  • 3
  • 22
  • 30

1 Answers1

1

If you can can use streams as input parameter to your function, your code will work with whatever stream you provide.

You can use std::fstream as input in your production code, and instead use std::sstream or even create your own mockup of a stream in your unit test code.

otama
  • 46
  • 3