我如何从整数之前的文件中读取字符串

时间:2018-12-28 15:04:08

标签: c string file

我有这个文本文件:

2 6
99 100 14 15 1 4 29 43 15 15
31 24 2 0 2 0 2 0 12 12
1 5 2 6 3 50 2 0 1 100
31 24 2 0 2 0 2 0 12 12
99 100 14 15 1 4 29 43 15 15
Lucky 0 0 100 100
James 2 0 100 100
Jerry 2 4 100 100
Cristofor 0 2 100 100
Chris 2 3 100 100
Miclaus 2 1 100 100

我想读取多个结构中的所有数据。 这是我的代码:我读了前6行,我想读下6行,并将每个单词都放入变量中。我不知道如何读取该字符串。

void citireDate(){
char c;
FILE *f;
f = fopen("nume.in","r");
fscanf(f,"%d", &R);
fscanf(f,"%d", &P);
for(int i=1;i <= 2*R+1 ; i++)
    for(int j=1;j <= 2*R+1; j++){
        fscanf(f,"%d",&ghetar[i][j].inaltime);
        fscanf(f,"%d",&ghetar[i][j].manusi);
}

for(int i=1;i<=6;i++){

//here i have to read that string
    fscanf(f,"%d",&spiridusi[i].x);
    fscanf(f,"%d",&spiridusi[i].y);
    fscanf(f,"%d",&spiridusi[i].hp);
    fscanf(f,"%d",&spiridusi[i].stamina);

}


fclose(f);
}

我如何读取该字符串?

1 个答案:

答案 0 :(得分:0)

只需将所有行读入<div class="form-group child"> <h1>{{patient.first_name}} {{patient.last_name}}</h1> <div class="col form-group form-control-md mt-5"> <label for="profileNo" style="width:25%">Profile No: </label> <label></label> <input type="text" class="input-group-sm" [(ngModel)]="patient.profile_no"> </div> <div class="col form-group form-control-md"> <label for="DOB" style="width:25%">Date of Birth: </label> <input type="text" class="input-group-sm" [(ngModel)]="patient.DOB"> </div> <div class="col form-group"> <label for="gender" style="width:25%">Gender: </label> <input type="text" class="input-group-sm"[(ngModel)]="patient.gender"> </div> <div class="col form-group"> <label for="bloodGroup" style="width:25%">Blood Group: </label> <input type="text" class="input-group-sm" [(ngModel)]="patient.blood_group"> </div>

fscanf(f, "%s %d %d %d %d\n", ...);

或者我当然认为字符串中的4个整数前没有空格

奇怪的是,第一个索引始终为1,我认为所有 中的索引都必须为0(除非您有充分的理由以1开头)