Hello,

I am having trouble with a simple math operation after reading from a file.
If a particular condition is met, i am copying a number(7 positions), starting from position 31.
My aim is to sum all these numbers when the condition is met.
I am testing the code on a file where the condition is met only once, as follows:

ATOM   5616  N   MET B   1     334.916 199.163  84.447  0.70172.02           N  

And yet the ouput is double what it should be: 669.831970 (right answer: 334.916)

while(fgets(s, 80 , fp1)!=NULL)
{
if((s[13] == 'N' && s[24] == ' ' && s[25] == '1'))
{
strncpy (sx, s+31, 7);
sx[7] = '/0';
x = atof(sx);
sum = sum + x;
}
}
printf("%f", sum);

Thanks in advance

Recommended Answers

All 6 Replies

In the example line you posted, position 13 is the letter 'E', (in MET), not 'N'. And pos 24 is '1' (in 916).

In the example line you posted, position 13 is the letter 'E', (in MET), not 'N'. And pos 24 is '1' (in 916).

For some strange reason when i added a printf line (printf("%f",x);)
within the if statement the program is behaving well:

while(fgets(s, 80 , fp1)!=NULL)
{
if((s[13] == 'N' && s[24] == ' ' && s[25] == '1'))
{
printf("%s", s);
strncpy (sx, s+31, 7);
sx[7] = '/0';
x = atof(sx);
printf("%f",x);
sum = sum + x;
}
}

printf("%f", sum);
Member Avatar for r.stiltskin

Assuming you have a line that actually does satisfy your test condition, if the following line is short enough (or even contains only a newline) so that its contents do not overwrite s[13], s[24] and s[25], the test condition will still be satisfied and the copy/add operations will be repeated.

Try this code, for example, with an input file containing these 2 lines:
line 1
l2

#include <stdio.h>

int main()
{
    FILE * infile;
    char s[80];
    infile = fopen("testfile.txt" , "r");
    while(fgets(s, 80 , infile)!=NULL)
    {
        if( s[5] == '1' )
        {
            printf("%c\n", s[5]);
        }
    }
    return 0;
}

What then is the solution to this problem? certainly there must be an alternate way of doing it.

@mjoshi : Null character is represented as '\0' and not '/0' as you have done in your program.

And hope you have used double variables for sum, x etc as atof returns double.

Member Avatar for r.stiltskin

The solution is simple: use strlen(s) to test if the new string is long enough. In my example I'm printing the char at s[5], so the string s must be at least 6 chars long. This code will ignore any string that is too short to satisfy my original test condition:

#include <stdio.h>
#include <string.h>

int main()
{
    FILE * infile;
    char s[80];
    infile = fopen("testfile.txt" , "r");
    while(fgets(s, 80 , infile)!=NULL)
    {
        if( strlen(s) > 5 && s[5] == '1' )
        {
            printf("%c\n", s[5]);
        }
    }
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.