Mitt senste försök fick jag datorn att fatta att strömmen var öppen men den skriver inget till filen:
[code:C++]
#include "fileReader.h"
/* The FileReader constructor
* param folder The folder the file is in.
* param file The name of the file to read.
* param write True if we are to write to the file false otherwise.
*/
FileReader::FileReader(string folder, string file) {
string location = folder + "/" + file;
folderName = folder;
fileName = file;
stream.open(location.c_str(), ios::ate);
}
/* Reads the next line of the file, and returns it as an int
*
* return The line represented as an int.
* exception fileClosedException The file is not opened.
* exception endOfFileException An unexpected EOF was encountered.
*/
int FileReader::getNextLineToInt() throw(fileClosedException,endOfFileException) {
if(stream.is_open()) {
if(!stream.eof()) {
string line;
getline(stream, line);
return atoi(line.c_str());
} else {
throw endOfFileException();
}
} else {
throw fileClosedException();
}
}
/* Reads the next line of the file, and returns it as an string
*
* return The line represented as a string.
* exception fileClosedException The file is not opened.
* exception endOfFileException An unexpected EOF was encountered.
* exception illegalOperation You may not do this kind of operation on this file.
*/
string FileReader::getNextLine() throw(fileClosedException,endOfFileException) {
if(stream.is_open()) {
if(!stream.eof()) {
string line;
getline(stream, line);
return line;
} else {
throw endOfFileException();
}
} else {
throw fileClosedException();
}
}
/* Reads the rest of the file, and returns it as an string
*
* return The rest of the file represented as a string.
* exception fileClosedException The file is not opened.
* exception illegalOperation You may not do this kind of operation on this file.
*/
string FileReader::getLinesUntilEOF() throw(fileClosedException) {
string text = "";
if(stream.is_open()){
while(true) {
try {
text += getNextLine();
cout << text << endl;
text += "
";
} catch(endOfFileException) {
return text;
}
}
}
else{
cout << "stream is closed" << endl;
throw fileClosedException();
}
return 0;
}
/* Writes a line to the file
*
* param line The line to be written.
* exception fileClosedException The file is not opened.
*/
void FileReader::writeNextLine(string line) throw(fileClosedException) {
if(stream.is_open()){
stream << line << endl;
}
else{
throw fileClosedException();
}
}
/* Writes a line to the file
*
* param line The number to be written.
* exception fileClosedException The file is not opened.
*/
void FileReader::writeNextLine(int number) throw(fileClosedException) {
if(stream.is_open()){
stream << number << endl;
}
else{
throw fileClosedException();
}
}
/* Closes the connection to the stream */
void FileReader::close() {
cout << "Stänger" << endl;
stream.close();
}
/* Writes an article to the file
*
* param art The article to be written.
* exception fileClosedException The file is not opened.
*/
void FileReader::writeArticle(Article art) throw(fileClosedException) {
writeNextLine(art.getTitle());
writeNextLine(art.getAuthor());
writeNextLine(art.getText());
}
/* Read an article from the file
*
* return The article that was read from the file.
* exception fileClosedException The file is not opened.
*/
Article FileReader::readArticle() throw(fileClosedException) {
Article temp(atoi(fileName.c_str()));
string title = getNextLine();
string author = getNextLine();
string text = getLinesUntilEOF();
temp.write(title,author,text);
return temp;
}
[/code]
Om någon kan se var jag tänker fel hade dte varit underbart för jag tror jag stirrat mig blind på koden nu...
Ingen status