I'm having a harsh time achieving this. I read a string of integer, extract them from that chain and put them in an array. I wanted to try something new because a used to read character by character with getChar in the past, but now I find sscanf which handle the job.
From Doc (sscanf) : On success, the function returns the number of items in the argument list successfully filled. This count can match the expected number of items or be less (even zero) in the case of a matching failure. In the case of an input failure before any data could be successfully interpreted, EOF is returned.
line : char line[] = "100 185 20 11 1000"; // number of int is unknown, it can differ
Problem: How to make diffrence between end of line and a whatever error
Eg: char line[] = "100 185 abc(not int) 11 1000";
Code :
int main(int argc, char** argv) {
char line[] = "100 185 20 11 1000";
int arrOfInt[10];
char *data = line;
int track, number, index = 0;
while ((sscanf(data, " %d%n", &number, &track)) == 1)
{
arrOfInt[index] = number;
data += track;
index++;
}
return 0;
}
while....== 1...emm..why? - Sourav Ghosh%nis not counted as read elements. - BLUEPIXY(status=sscanf(data, " %d%n", &number, &track)) == 1... end of line : status == EOF, read error : status == 0 - BLUEPIXY