[語法] FILE I/O
請問黃色標記處怎麼修正可以使他可以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
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章