Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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
main()
{
FILE *fptr_in,*fptr_out;
int i=0;
char type[4];
float met,vel;
fptr_in = fopen("Table1b.txt","r");
fptr_out =
fopen("Table1c.txt","w");
while(i4)
{
fscanf(fptr_in,"%f %f
%s",&met,&vel,&type);
if (type=="RRV")
fprintf(fptr_out,"%f %f
%s",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??

Hi,
I think your bug lies in the fact that you are trying to equate two strings.
if i declare something like char type[4];
later when i refer to type i am refering to the address of the first letter of the string. So in effect when you compare two strings using a = sign you are comparing addresses.
To compare two strings you probably want to
use this function.
strcmp(string1,string2)Include the header
This function returns 0 if the two are equal
a value 0 if otherwise.Why dont you give that a try.
zzray

I'm sorry jennifer, This posting thing is driving me nuts coz it leaves out half my message.
Ignore the prev message from Include the header.Include string.h . The function returns 0 if both are equal some value less than zero if string1 comes before string2 in the dictionary and a value greater than zero if otherwise.
This is what i meant to say but it seems to chop up my posts.
zzray

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |