[語法] FILE I/O

看板C_and_CPP (C/C++)作者 (god-like)時間16年前 (2009/06/20 16:22), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/1
請問黃色標記處怎麼修正可以使他可以RUN 因為我想每一個write, read 都用function 來寫,讓版面比較整潔 #include <iostream> #include <fstream> #include <string> using namespace std; struct node { int key; int leaf; }; int main() { node node; ifstream infile; ofstream outfile; node.leaf = 5; node.key = 10; string abc = "gg"; outfile.open(abc.c_str(), ios::binary); outfile.seekp(0*sizeof(node), ios::beg); outfile.write((char*) &node, sizeof(node)); outfile.close(); cout << node.leaf << " " << node.key << endl; node.leaf = 50; node.key = 100; infile.open(abc.c_str(), ios::binary); infile.seekg(0*sizeof(node), ios::beg); infile.read((char*) &node, sizeof(node)); infile.close(); cout << node.leaf << " " << node.key << endl; node.leaf = 50; node.key = 100; outfile.open(abc.c_str(), ios::binary); outfile.seekp(0*sizeof(node), ios::beg); outfile.write((char*) &node, sizeof(node)); outfile.close(); outfile.open(abc.c_str(), ios::binary); outfile.seekp(1*sizeof(node), ios::beg); outfile.write((char*) &node, sizeof(node)); outfile.close(); node.leaf = 5; node.key = 10; infile.open(abc.c_str(), ios::binary); infile.seekg(0*sizeof(node), ios::beg); infile.read((char*) &node, sizeof(node)); infile.close(); cout << node.leaf << " " << node.key << endl; node.leaf = 5; node.key = 10; infile.open(abc.c_str(), ios::binary); infile.seekg(1*sizeof(node), ios::beg); infile.read((char*) &node, sizeof(node)); infile.close(); cout << node.leaf << " " << node.key << endl; return 0; } -----------------------------問題----------------------- 我使用infile.good(); 都是正確的 如果把infile.open, close拿掉,就可以成功執行 可是這樣就達不到我每一個write都能寫一個open close 的功能 有辦法讓他可以work嗎?? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 163.25.118.131
文章代碼(AID): #1AF9nEIO (C_and_CPP)
文章代碼(AID): #1AF9nEIO (C_and_CPP)