Computing.Net > Forums > Programming > C program bug is driving me nuts

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C program bug is driving me nuts

Reply to Message Icon

Name: Jennifer
Date: May 12, 2003 at 15:41:40 Pacific
OS: RedHat Linux
CPU/Ram: Dunno
Comment:

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??



Sponsored Link
Ads by Google

Response Number 1
Name: zzray
Date: May 12, 2003 at 18:57:26 Pacific
Reply:

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


0

Response Number 2
Name: zzray
Date: May 12, 2003 at 19:02:03 Pacific
Reply:

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


0

Response Number 3
Name: Jennifer
Date: May 13, 2003 at 09:53:58 Pacific
Reply:

Ah ha! It works perfectly now - thanks!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C program bug is driving me nuts

is this possible in vb.net? www.computing.net/answers/programming/is-this-possible-in-vbnet/8190.html

Basic Java Program (A Vending Machi www.computing.net/answers/programming/basic-java-program-a-vending-machi/11246.html

Assembly Problems www.computing.net/answers/programming/assembly-problems/7886.html