Som besökare på Hamsterpaj samtycker du till användandet av s.k. cookies för att förbättra din upplevelse hos oss. Jag förstår, ta bort denna ruta!
Annons

C++ skriva och läsa från fil

Skapad av gaminggirl, 2010-05-18 13:03 i Webbutveckling & Programmering

2 138
6 inlägg
0 poäng
gaminggirl
Visningsbild
F 38 Lund Hjälte 8 084 inlägg
0
Har suttit i en vecka nu och läst och läst på internet, har testat saker som fprintstream och liknande men det vill inte dungera. Så min fråga blir kort och enkelt någon som vet hur man gör för att både kunna läsa och skriva till en fil i C++?

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

Är reklamen ivägen? Logga in eller registrera dig så försvinner den!

Phelix
Visningsbild
P 36 Linköping Hjälte 1 029 inlägg
0

Svar till gaminggirl [Gå till post]:
Låter som att du vill öppna en stream för både läsning/skrivning på samma gång? I många språk vet jag att det inte är möjligt att göra, och det är verkligen inte att rekommendera i vilket fall. Gör en sak i taget om du kan, finns stora risker för logiska fel om man håller på med båda på samma gång.

Tyvärr kan jag inte ge dig mer vägledning än så eftersom mina kunskaper i C++ sluter ungefär vid cout & cin för tillfället. :D

Ingen status


Forum » Datorer & IT » Webbutveckling & Programmering » C++ skriva och läsa från fil

Ansvariga ordningsvakter:

Användare som läser i den här tråden just nu

1 utloggad

Skriv ett nytt inlägg

Hej! Innan du skriver om ett potentiellt problem så vill vi påminna dig om att du faktiskt inte är ensam. Du är inte onormal och världen kommer inte att gå under, vi lovar! Så slappna av och gilla livet i några minuter - känns det fortfarande hemskt? Skriv gärna ner dina tankar och frågor, vi älskar att hjälpa just dig!

Den här tråden är äldre än Rojks drömtjej!

Det senaste inlägget i den här tråden skrevs för över tre månader sedan. Är du säker på att du vill återuppliva diskussionen? Har du något vettigt att tillföra eller passar din fråga i en ny tråd? Onödiga återupplivningar kommer att låsas så tänk efter en extra gång!

Hjälp

Det här är en hjälpruta

Här får du korta tips och förklaringar om forumet. Välj kapitel i rullningslisten här ovanför.

Rutan uppdateras automagiskt

När du använder funktioner i forumet så visas bra tips här.


Annons
Annons
Annons
Annons