Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C program bug is driving me crazy 1

Status
Not open for further replies.

Tarlonniel

Programmer
May 12, 2003
2
US
I'm trying to write a simple program that will read a file with 3 columns of data, test the last column against a given value, and print out the current line if the test is true. This is my input file (Table1b.txt):

-1.84 13.16 TO
-1.99 13.36 RRV
-2.20 14.65 TO
-1.19 14.41 G
-2.44 13.34 G

And this is my program:

#include <stdio.h>

main()
{
FILE *fptr_in,*fptr_out;
int i=0;
char type[4];
float met,vel;

fptr_in = fopen(&quot;Table1b.txt&quot;,&quot;r&quot;);
fptr_out = fopen(&quot;Table1c.txt&quot;,&quot;w&quot;);

while(i<4)
{
fscanf(fptr_in,&quot;%f %f %s&quot;,&met,&vel,&type);
if (type==&quot;RRV&quot;)
fprintf(fptr_out,&quot;%f %f %s&quot;,met,vel,type);
i++;
}

fclose(fptr_in);
fclose(fptr_out);
}

But for some reason I can never get the if statement to read true. The program loops and prints perfectly otherwise - what am I doing wrong??
 
The &quot;[tt]==[/tt]&quot; is a numerical comparison. Try using [tt]strcmp()[/tt]. That would be something like...
Code:
   if ( ! strcmp(type, &quot;RRV&quot;) )
The [tt]strcmp()[/tt] function returns a zero if they're equal.

Hope this helps.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top