I have the code below to read data from file into structure using C.
However, I would like to convert it to C++.
Plz, help me. I'm from VietNam, so I speak English not well...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
...
...
struct knight
{
int HP;
int level;
int remedy;
int maidenkiss;
int phoenixdown;
};
//read data from input file to corresponding variables
//return 1 if successfully done, otherwise return 0
int readFile(knight& theKnight, int& nEvent, int arrEvent[])
{
char* file_name = "input.txt";
FILE* f = 0;
char* str = new char[MAX_CHARACTER_EACH_LINE];
int num;
f = fopen(file_name, "r");
if (f == NULL) //file not found || cannot read
return 0;
fgets(str, MAX_CHARACTER_EACH_LINE, f);
if (feof(f))
return 0;
ifstream sourceFile("input.txt", ios::in);
int start = 0;
int len = strlen(str);
//read HP
while (start < len && str[start] == ' ')
start ++;
num = 0;
while (start < len && str[start] <= '9' && str[start] >= '0'){
num = num * 10 + str[start] - '0';
start++;
}
theKnight.HP = num;
if (theKnight.HP < 1 || theKnight.HP > 999)
return 0;
//read level
while (start < len && (str[start] > '9' || str[start] < '0'))
start ++;
num = 0;
while (start < len && str[start] <= '9' && str[start] >= '0'){
num = num * 10 + str[start] - '0';
start++;
}
theKnight.level = num;
if (theKnight.level < 1 || theKnight.level > 10)
return 0;
//read remedy
while (start < len && (str[start] > '9' || str[start] < '0'))
start ++;
num = 0;
while (start < len && str[start] <= '9' && str[start] >= '0'){
num = num * 10 + str[start] - '0';
start++;
}
theKnight.remedy = num;
if (theKnight.remedy < 0 || theKnight.remedy > 99)
return 0;
//read maidenkiss
while (start < len && (str[start] > '9' || str[start] < '0'))
start ++;
num = 0;
while (start < len && str[start] <= '9' && str[start] >= '0'){
num = num * 10 + str[start] - '0';
start++;
}
theKnight.maidenkiss = num;
if (theKnight.maidenkiss < 0 || theKnight.maidenkiss > 99)
return 0;
//read phoenixdown
while (start < len && (str[start] > '9' || str[start] < '0'))
start ++;
num = 0;
while (start < len && str[start] <= '9' && str[start] >= '0'){
num = num * 10 + str[start] - '0';
start++;
}
theKnight.phoenixdown = num;
if (theKnight.phoenixdown < 0 || theKnight.phoenixdown > 99)
return 0;
//read events
nEvent = 0;
while (1){
if (feof(f))
break;
fgets(str, MAX_CHARACTER_EACH_LINE, f);
if (str == NULL)
break;
start = 0;
len = strlen(str);
while (start < len){
while (start < len && (str[start] > '9' || str[start] < '0'))
start ++;
if (start >= len)
break;
arrEvent[nEvent] = 0;
while (start < len && str[start] <= '9' && str[start] >= '0'){
arrEvent[nEvent] = arrEvent[nEvent] * 10 + str[start] - '0';
start++;
}
nEvent++;
}
}
fclose(f);
return 1;
}
...
|
And here, data from "input.txt"
line 1: HP;level, remedy; maidenkiss, phoenixdown;
1<HP<999; 1<level<10; 0<remedy<99; 0<maidenkiss<99; 0<phoenixdown<99;
line 2: an array, it's contain events, ex 9, 5, 8, 4.
I need read data from line 1 into struct, and read data from line 2 into arrayEvent..
****
And if you can, plz explain the code that you converted for me.
Thank you very much!