You can also read an integer from the file without using fscanf
. Here is some code which reads content from the file, without using fscanf
. I also corrected some data type declaration for your value in which you compared int
data to long long
.
int main()
{
FILE * ipf = fopen("input.txt", "r");
long long x = 0 , p = 0, n = 0, b = 0;
char * line = NULL;
size_t len = 0;
ssize_t read;
char *token;
while ((read = getline(&line, &len, ipf)) != -1)
{
token=strtok(line,",");
p=atoi(token);
token=strtok(NULL,",");
n=atoi(token);
token=strtok(NULL,"");
x=atoi(token);
if(x < 4294967296 && p < 32 && n < p + 2)
{
b = getbits(x,p,n);
printf("gebits( x = %lld, p = %lld, n = %lld) = %lld\n", x, p, n, b);
}
else { printf("ERROR\n"); }
}
return 0;
}
For Windows, use the following solution:
int main()
{
FILE * ipf = fopen("input.txt", "r");
long long x = 0 , p = 0, n = 0, b = 0;
char line [256];
size_t len = 0;
ssize_t read;
char *token;
while ((fgets(line,256, ipf)) != 0)
{
token=strtok(line,",");
p=atoi(token);
token=strtok(NULL,",");
n=atoi(token);
token=strtok(NULL,"");
x=atoi(token);
if(x < 4294967296 && p < 32 && n < p + 2)
{
b = getbits(x,p,n);
printf("gebits( x = %lld, p = %lld, n = %lld) = %lld\n", x, p, n, b);
}
else { printf("ERROR\n"); }
}
return 0;
}