c++文件流结构体怎么读取写入操作

c++文件流结构体如何读取写入操作?

从txt每20字符读一次赋值给结构体
txt内容0   3    David       1  3   Sodim  
typedef struct ContextStu{
         int ID;
         int  ChildID;
        string name;
}Context;

vector<Context> _Context;
ifstream file;
file.open(f"D:\\Test.txt",ios::in);
然后呢。。。。。



从txt每20字符写一次结构体数据到文本中,存储4个字符存ID 4个字符存ChildID 12 个字符存name
txt内容0   3    David       1  3   Sodim  
typedef struct ContextStu{
         int ID;
         int  ChildID;
         string name;
}Context;

vector<Context> _Context;
Context Context1,Context2;
Context1.ID  =0;
Context1. ChildID = 3;
Context1.name = "David";
Context2.ID = 1;
Context2.ChildID = 3;
Context2.name = "Sodim";
_Context.push(Context1);
_Context.push(Context2);
ofstream file;
file.open("D:\\Test.txt",ios::in);
然后呢。。。。。

------解决思路----------------------
仅供参考
//NAME: essaie bla bla
//DIMENSION: 8
//DATA
//1  14  15
//2  11  10
//3  6   4
//4  7   13
//5  9   21
//6  19  3
//7  1   5
//8  8   8
//EOF
//
// 文本文件中可能还含有其他内容,但是需要用到的内容即以上

//比如data.txt:
//NAME: essaie bla bla
//其它内容
//DIMENSION: 8
//其它内容
//DATA
//其它内容
//1  14  15
//其它内容
//2  11  10
//其它内容
//3  6   4
//其它内容
//4  7   13
//其它内容
//5  9   21
//其它内容
//6  19  3
//其它内容
//7  1   5
//其它内容
//8  8   8
//其它内容
//EOF

// 目标是要获取NAME后字串,DIMENSION后数值,以及DATA以下的数值
// 其中NAME就是随便个字句,DIMENSION是城市数量,DATA以下是城市编号,X坐标,Y坐标
// 所有的这些将赋值给一个事先定义好的结构
#include <stdio.h>
#include <string.h>
#define MAXCPL   80   //每行最大字符数
#define MAXCITY  100  //每组数据中DATA最多项数,DIMENSION的最大值
#define MAXNAMEL 32   //NAME最大长度
struct S {
    char NAME[MAXNAMEL+1];
    int  DIMENSION;
    struct D {
        int NO;
        int X;
        int Y;
    } DATA[MAXCITY];
} s;
FILE *f;
int st,n,i;
char ln[MAXCPL];
int main() {
    f=fopen("data.txt","r");
    if (NULL==f) {
        printf("Can not open file data.txt!\n");
        return 1;
    }
    st=0;
    n=0;
    while (1) {
        if (NULL==fgets(ln,MAXCPL,f)) break;
        if (st==0) {
            if (1==sscanf(ln,"NAME: %32[^\n]",s.NAME)) st=1;
        } else if (st==1) {
            if (1==sscanf(ln,"DIMENSION: %d",&s.DIMENSION)) st=2;
        } else if (st==2) {
            if (0==strcmp(ln,"DATA\n")) st=3;
        } else if (st==3) {
            if (3==sscanf(ln,"%d%d%d",&s.DATA[n].NO,&s.DATA[n].X,&s.DATA[n].Y)) {
                n++;
                if (n>=MAXCITY 
------解决思路----------------------
 n>=s.DIMENSION) break;
            }
        }
    }
    fclose(f);
    printf("s.NAME=[%s]\n",s.NAME);
    printf("s.DIMENSION=%d\n",s.DIMENSION);
    for (i=0;i<n;i++) {
        printf("s.DATA[%d].NO,X,Y=%d,%d,%d\n",i,s.DATA[i].NO,s.DATA[i].X,s.DATA[i].Y);
    }
    return 0;
}
//s.NAME=[essaie bla bla]
//s.DIMENSION=8
//s.DATA[0].NO,X,Y=1,14,15
//s.DATA[1].NO,X,Y=2,11,10
//s.DATA[2].NO,X,Y=3,6,4
//s.DATA[3].NO,X,Y=4,7,13
//s.DATA[4].NO,X,Y=5,9,21
//s.DATA[5].NO,X,Y=6,19,3
//s.DATA[6].NO,X,Y=7,1,5
//s.DATA[7].NO,X,Y=8,8,8


------解决思路----------------------
哥们你这怎么循环写的数据呀?????????????
std::ofstream outfile;
    outfile.open(filePath.c_str());
    char sz2[21];
    memset(sz2, 0, 21);
    sprintf(sz2, "%-4d%-4d%-12s", Context1.ID, Context1.ChildID, Context1.name.c_str());
    outfile.write(sz2, 20);
    outfile.close();

引用:
读文件

    Context con;
    char sz[21];
    infile.open(filePath.c_str());
    while (1)
    {
        memset(sz, 0, 21);
        infile.read(sz, 20);
        if (0 == infile.gcount())
        {
            break;
        }

        sscanf(sz, "%4d%4d%12s", &con.ID, &con.ChildID, con.name.c_str());
        std::cout << con.ID << con.ChildID << con.name.c_str() << std::endl;
    }
    infile.close();


写文件

    std::ofstream outfile;
    outfile.open(filePath.c_str());
    char sz2[21];
    memset(sz2, 0, 21);
    sprintf(sz2, "%-4d%-4d%-12s", Context1.ID, Context1.ChildID, Context1.name.c_str());
    outfile.write(sz2, 20);
    outfile.close();