Files
threaded_network_chat/log.cc

53 lines
1.0 KiB
C++

#include <fstream>
#include <ios>
#include "public.h"
#include "log.h"
// gets the number of lines in a file. returns -1 if there was an error.
int
linesInFile(string path)
{
ifstream file(path);
if (!file) {
return 1;
} else {
int lineNum = 0;
string line;
int num = 0;
while (getline(file, line))
num++;
return num;
}
}
// appends a line of text to the end of a given file. returns 0 if the file
// existed, 1 if it didn't work
int
writeToFile(string path, string line, bool incLineNum)
{
ofstream file;
/*
* Hopefully this fixes the program messing up where the server/client
* doesn't start if the file wasn't created by a previous session.
*/
file.open(path, ios_base::out|ios_base::app);
if (file.is_open()) {
file << line << endl;
file.close();
// this probably would help but theres too much broken stuff
// right now to be sure if (incLineNum)
//{
// linePos++;
// }
return 0;
} else {
// do something if it didn't work
return 1; // i guess that's good enough for now
}
}