C版本
#include <stdio.h>
typedef struct {
char a[10];
char b[11];
}stu;
int main()
{
stu test = {0};
FILE *fp;
fp = fopen("D:\\path", "w+");
if (!fp)
{
return 0;
}
fwrite(&test, sizeof(test), 1, fp);
stu test2;
fread(&test2, sizeof(test2), 1, fp);
fclose(fp);
}
C++版本
#include <iostream>
#include <fstream>
using namespace std;
struct Game
{
int num;
int year;
string location;
string first;
string second;
string third;
};
int _tmain(int argc, _TCHAR* argv[])
{
Game game = {0};
char buffer[256];
ofstream outfile("C:\\Users\\Administrator\\Desktop\\t11.txt", ios::out);
outfile << game.location.c_str() << game.num;
ifstream in_file("C:\\Users\\Administrator\\Desktop\\t11.txt", ios::trunc);
Game game2 = {0};
if (!in_file.is_open())
{
std::cout << "error" << std::endl;
return 0;
}
in_file >> buffer;
in_file.close();
outfile.close();
return 0;
}