lundi 2 mars 2015

Storing integer values from lines of text in a .txt file



I'm trying to store 10 integers from the second and third lines of a text file into an array of integers. When I try to compile, I get multiple errors about certain variables being undeclared.


Input is the name assigned to the Input .txt file.


perm1 and perm2 are both int arrays of size [10].



int current_line = 0; //a line counter
while(fgets(line, 401, Input) != NULL){
if(current_line == 1){
sscanf(line, "%d %d %d %d %d %d %d %d %d %d", &n1, &n2, &n3, &n4, &n5, &n6, &n7, &n8, &n9, &n10);
perm1[0] = n1;
perm1[1] = n2;
perm1[2] = n3;
perm1[3] = n4;
perm1[4] = n5;
perm1[5] = n6;
perm1[6] = n7;
perm1[7] = n8;
perm1[8] = n9;
perm1[9] = n10;
}
else if(current_line == 2){
sscanf(line, "%d %d %d %d %d %d %d %d %d %d", &n1, &n2, &n3, &n4, &n5, &n6, &n7, &n8, &n9, &n10);
perm2[0] = n1;
perm2[1] = n2;
perm2[2] = n3;
perm2[3] = n4;
perm2[4] = n5;
perm2[5] = n6;
perm2[6] = n7;
perm2[7] = n8;
perm2[8] = n9;
perm2[9] = n10;
}
else{
//do nothing
}
current_line++;
}


When I compile this code, it tells me that "line" is undeclared. It also tells me that n1, n2, n3, etc are all undeclared.


How can I fix this?




Aucun commentaire:

Enregistrer un commentaire