שאלה ב c++

nautilus7791

New member
שאלה ב c++

בתוכנית הבאה יש כתיבה לקובץ באמצעות אופרטור >>.כיצד אני קורא בחזרה נתונים מהקובץ?
// redirecting cout's output #include <iostream> #include <fstream> using namespace std; int main () { streambuf *psbuf; ofstream filestr; filestr.open ("test.txt"); psbuf = filestr.rdbuf(); cout.rdbuf(psbuf); cout << "This is written to the file"; filestr.close(); return 0; }​
 
זו שיטה מאוד לא טובה לכתוב לקובץ למה אתה מערב כאן את cout? יותר הגיוני פשוט
ofstream filestr; filestr.open ("test.txt"); filestr << "This is written to the file";​
ובאותה מידה אתה יכול ליצור אובייקט ifstream ולקרוא ממנו ע"י האופרטור ההפוך, <<. חפש בגוגל ofstream example או ifstream example ותמצא אינסוף דוגמאות.
 
למעלה