samplegame.txt (actual_file)Lesen einer Anzahl Zeile für Zeile und teilen Sie es auf mehrere Variable in C
6
2 3 3
11 2
18 1
10 3
22 14
30 19
8 16
12 24
20 33
ich will Daten lesen .txt in meine C-Datei program.For jede Zeile trennen sie und speichert sie in der variablen, ich kann in weiteren step.i'm so verwenden versuchen
FILE* file = fopen (argv[1], "r");
int i = 0;
fscanf (file, "%d", &i);
while (!feof (file))
{
//printf ("%d ", i);
fscanf (file, "%d", &i);
}
fclose (file);
zu verwenden, aber ich habe die ganze Zahl in einer Zeile und kann nicht herausfinden, wie jede Gruppe trennen wie
int board_size = 6 << 1st line
int minion_box_size = 2; << 2nd line
int trap_size = 3;
int trampoline_size = 3;
int minion_box[minion_box_size] = {11,18}; << 3rd to N line
int minion_walk[minion_box_size] = {2,1};
int trap_position[trap_size] = {10,22,30}; << N+1 to M line
int trap_end[minion_box_size] = {3,14,19};
int trampoline_position[trampoline_size] = {8,12,20}; << M+1 to k line
int trampoline_jump[trampoline_size] = {16,24,33};
Hat jemand einen Vorschlag?
samplegame.txt (erklärt)
______Board_________
6 // size of game board
_____size of each item_________
2 3 3 // first number in this line is Minion_box,which means there is 2 Minion_box
// Second number is trap_holes,which means there is 3 traps
// third number is trampoline,which means there is 3 trampolines
_____Minion_Box_______
// first number : where the minion_box are
// Second number : how many step that the minion can walk
11 2 // the box is in 11th block , minion has aiblity to walk 2 step
18 1 // the box is in 18th block , minion has ability to walk 1 step
______Traps__________
// first number : where the traps are
// Second number : the block you gonna be,after stepping on the trap.
10 3 // there is a trap hole in the 10th block,step on it and you will fall down 3rd block
22 14 // a trap hole in the 22th block,fall down to 14th block
30 19 // a trap hole in the 30th block,fall down to 19th block
____trampoline_______
// first number : where the trampoline are
// Second number : the block you gonna be,after stepping on the trampoline.
8 16 // there is a trampoline in 8th block,if you step on it you will jump to 16th block
12 24 // trampoline in 12th block,jump to 24th block
20 33 // trampoline in 20th block,jump to 33th block
Sorry für schlechtes Englisch und wirklich lange posten, da ich bin nicht gut in Englisch, so versuche ich, um das Beispiel zu zeigen, so viel wie möglich :(
http://stackoverflow.com/questions/5431941/why-is-which-feof-file-always-wrong –
Verwenden Sie 'fgets()', um die Zeile _line_ und die Verwendung 'sscanf()' zu lesen, um nach Bedarf zu analysieren . – chux