help reading from file

Hello

I am supposed to read IPs from a file. In the following code I have replaced the processing part with simple printfs. The point is the number of IPs is not known but they are arranged in ranges each
on a new line, e.g.
2.3.4.5-10.255.255.255

With the following code, I could get all digits correct except the first digit. Please suggest.

while((a=getc(stdin))!=EOF){
ungetc(a, stdin);
for(i=0;i<4;i++){
fscanf(stdin, “%d”, &a);
printf("%d
“, a);
fscanf(stdin, " %c”, &c);
}
for(i=0;i<4;i++){
fscanf(stdin, “%d”, &a);
printf("%d
“, a);
fscanf(stdin, " %c”, &c);
}
}

Thank you.

Why not just read in the whole line and parse it with one sscanf?


char line[256];
int a1, a2, a3, a4, b1, b2, b3, b4;
while (fgets(line, sizeof(line), stdin) != NULL) {
    int status = sscanf(line, "%d.%d.%d.%d-%d.%d.%d.%d", &a1, &a2, &a3, &a4, &b1, &b2, &b3, &b4);
    if (status < 8)
        // not enough values
    else
        // process data
}

is this homework such that you are restricted to using C? if not, use dedicated parsing tools, eg awk (or better languages suited for parsing files, eg Python,Perl) will be better.

ghostdog74 wrote:
> is this homework such that you are restricted to using C? if not, use
> dedicated parsing tools, eg awk (or better languages suited for parsing
> files, eg Python,Perl) will be better.
>
>
This is not homework but personal indulgence. Thank you for the suggestion; I will have a look at those tools for sure.

Thank you.

ken yap wrote:
> Why not just read in the whole line and parse it with one sscanf?
>
> Code:
> --------------------
>
> char line[256];
> int a1, a2, a3, a4, b1, b2, b3, b4;
> while (fgets(line, sizeof(line), stdin) != NULL) {
> int status = sscanf(line, “%d.%d.%d.%d-%d.%d.%d.%d”, &a1, &a2, &a3, &a4, &b1, &b2, &b3, &b4);
> if (status < 8)
> // not enough values
> else
> // process data
> }
> --------------------
>
>
Initially, I was also thinking of getting strings from files extracting the integers; but my method of parsing was inefficient. Thank you ken.

Cross_AM wrote:
> Hello
>
> I am supposed to read IPs from a file. In the following code I have replaced the processing part with simple printfs. The point is the number of IPs is not known but they are arranged in ranges each
> on a new line, e.g.
> 2.3.4.5-10.255.255.255
>
> With the following code, I could get all digits correct except the first digit. Please suggest.
>
> while((a=getc(stdin))!=EOF){
> ungetc(a, stdin);
> for(i=0;i<4;i++){
> fscanf(stdin, “%d”, &a);
> printf("%d
“, a);
> fscanf(stdin, " %c”, &c);
> }
> for(i=0;i<4;i++){
> fscanf(stdin, “%d”, &a);
> printf("%d
“, a);
> fscanf(stdin, " %c”, &c);
> }
> }
>
> Thank you.
Friends, we all know that there are numerous workarounds to this problem; but what I wanted was to understand is what is causing the problem.

Sorry mate, I gave up on trying to understand learners’ code way back when I was a uni tutor. :frowning: I don’t want to go back to those days. These days I just write code my way. You’ll have to nut this one out without my help.

ken yap wrote:
> Sorry mate, I gave up on trying to understand learners’ code way back
> when I was a uni tutor. :frowning: I don’t want to go back to those days. These
> days I just write code my way. You’ll have to nut this one out without
> my help.
>
>
Thanks neways ken.