• Keine Ergebnisse gefunden

Input / Output in Files

N/A
N/A
Protected

Academic year: 2022

Aktie "Input / Output in Files"

Copied!
4
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Jetzt könnte das File erneut gelesen

werden!

Ein Spezialfall des Schreibens in / Lesens von Files wurde mit den

Befehlen cin und cout behandelt. Es lassen sich allgemeinere Ein- und Ausgabeströme öffnen, die dann ein Schreiben in Dateien erlauben.

Input / Output in Files

• Syntax

#include iostream

#include fstream

……..

ifstream inFile ( InputFileName );

ofstream outFile ( OutputFileName );

if (inFile.is_open()) {

while ( inFile >> myInteger >> myDouble ) { myInteger *= 10 ; }

inFile.clear();

inFile.seekg(0,ios::beg);

inFile.close();

}

else {

cerr << “Can not open file“ << InputFileName <<endl;

}

Öffnen eines Input und Output Stroms

Fehler behandlung Einfügen der header files

Lesen bis File Ende War das Öffnen erfolgreich

Reset ifstream, z.B. um File erneut zu lesen Setzen der Fileposition auf Filebeginn

Schliessen des Files

char InputFileName[256] = “myFile.txt“;

(2)

Mit den Ein und Ausgabeströmen sind Zustandsbeschreibungen verknüpft, die verwendet werden können.

Input / Output in Files

ifstream inFile ( InputFileName );

…...

inFile.bad();

inFile.good();

inFile.eof();

inFile.clear();

…...

while ( !inFile.eof() ){ Ausführung bis zum Ende }

……..

Die Positionierung im geöffneten File lässt sich steuern inFile.seekg(0,ios::beg);

inFile.seekg(0,ios::end);

reset der Zustandsflags, wird beim erneuten Lesen benötigt true, wenn das File Ende erreicht wurde

Positionierung relativ zum Filebeginn oder zum Fileende. Der 1. Parameter ist der Offset.

Die Dokumentation enthält weitere Funktionen.

https://en.cppreference.com/w/cpp/io/basic_istream

(3)

Mit Hilfe von Funktionen können Text Files gelesen werden.

Input / Output in Files

#include <iostream>

#include <fstream>

#include <string>

……...

string TextZeile;

ifstream inFile (“BeispielTextFile.txt“);

if (inFile.is_open()) {

while ( getline ( inFile , TextZeile ) ) { cout << TextZeile << endl;

}

inFile.close();

} else {

cerr <<“Fehler, File kann nicht geoeffnet werden “ << endl;

}

Text keine Zahlen !!

(4)

Das folgende Beispiel zeigt noch einmal die wichtigsten C++ Anweisungen, die für den lesenden und schreibenden Zugriff auf Files gebraucht werden.

#include <iostream>

#include <fstream>

int main { ….

char Filename[] = “myFile.txt“;

ofstream outFile ( Filename );

outFile << myVarA << myVarB ; ifstream inFile ( Filename );

inFile >> myVarA >> myVarB ; ….

outFile.close();

inFile.close();

} Arbeitsvorschlag:

- Schreiben Sie ein Programm, das das File myDataFile.txt liest.

Definitionen für die Input/Output Klassen

Output File Name Pointer für Output Stream Schreiben von 2 Variablen

Schliessen des Output Stream

Input / Output in Files

readFile.cc

Referenzen

ÄHNLICHE DOKUMENTE

It signals the central control or the processor (depending on the Medium Data Processing System) to au- tomatically translate EBCDIC to BCL informa- tion as it is

signal on this line indicates that the external equipment has accepted the word of information and will turn off the output data ready signal at the com- puter; this causes

Useful to make sure files are identical, DIR Sorted current or declared directory files with sizes and disk space used and remaining are displayed on console with this command..

In determining the priority of control units which operate multiple devices with different priority rules (for example, a 2821 that attaches both class 2 and class

The selector light pen feature supplies a hand-held, pen-like device that permits an operator of a display station to select fields of data from the display screen

Punching operations, speeds, internal controls, punching in data mode 2, and all other features related to punching are the same as those of the 1442 Card Read Punch Model Nl,

Das Problem ist, acht Damen auf einem Schachbrett so anzuordnen, dass keine die andere

Input port lines and output port lines are accessed at 16-pin DIP sockets on the card.. A reset line is