The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
[C] printing integers from file
Discuss [C] printing integers from file in the C Programming forum on Dev Shed. [C] printing integers from file C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 22nd, 2012, 01:30 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 4
Time spent in forums: 1 h 51 m 34 sec
Reputation Power: 0
|
|
|
[C] printing integers from file
I want to print integers from file. If I know how many integers will be in a file, let's say 10, i would write a simple loop, like this:
Code:
for(i = 0; i < 10; i++){
scanf("%d",&i);
printf("%d ",i);
}
but what to do if i don't know how many integers will be in a file?
|

November 22nd, 2012, 02:07 PM
|
|
|
|
Read a number.
If that succeeds, print it and loop back
Otherwise you can assume the data has ended and proceed with the rest of the program.
|

November 25th, 2012, 04:18 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 4
Time spent in forums: 1 h 51 m 34 sec
Reputation Power: 0
|
|
Thanks. But how do i check is it succeeded to read a number?
Code:
for (i = 1; i < 2; i++){
scanf("%d",&n);
if (/* number found */){
printf("%d ",n);
i--;
}
}
|

November 25th, 2012, 04:32 AM
|
 |
Contributed User
|
|
|
|
|
if ( scanf("%d", &n ) == 1 )
But beware that typing in say 123456789012345678901234567890 will generate numeric overflow, but scanf will still claim success and give you some modulo-n result.
If this is a problem, use fgets() to read a line of input, then strtol() to convert from a string to a long integer. This also sets errno in case of overflow (of a long), then you can range-check it for an integer (or whatever the valid range is).
|

November 25th, 2012, 05:45 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 4
Time spent in forums: 1 h 51 m 34 sec
Reputation Power: 0
|
|
Thanks, works like a charm.
But now i'm a bit confused how comparison in C works. If a number is not 1, but say 3, then '3 == 1' should be false, because 3 is not equal 1. So 'if' sentece shouldn't work. But it works 
|

November 25th, 2012, 06:47 AM
|
 |
Contributed User
|
|
|
|
|
I think you mis-understand what scanf returns.
if ( scanf("%d %d", &x, &y ) == 2 )
The 2 refers to the fact that there are two conversions in the format string, and that both of them were successful - from say typing in 123 456.
But if you typed in 123 abc, then scanf() would return 1 (assigning 123 to x, and leaving y undefined).
|

November 25th, 2012, 07:38 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 4
Time spent in forums: 1 h 51 m 34 sec
Reputation Power: 0
|
|
Ohh, i got it now.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|