1

I have a file abc.txt with 8000 lines.The file structure would be:

121220122563841210000000 999.999 999.999 999.999
121220122563841210000000 999.999 999.999 999.999
121220122563841210000000 999.999 999.999 999.999
121220122563841210000000 999.999 999.999 999.999
121220122563841210000000 999.999 999.999 999.999
121220122563841210000000 0.2563 0.25698 2.3658
121220122563841210000000 999.999 999.999 999.999
121220122563841210000000 999.999 999.999 999.999
121220122563841210000000 2.365 2.365894 0.15463
121220122563841210000000 999.999 999.999 999.999
121220122563841210000000 999.999 999.999 999.999
121220122563841220000000 4.2563 6.25698 25.3658

The sequence goes on.

I needed to write a program to read the line which do not contain 999.999.Here's how I went.I made a comparison with the values that I read,but it's giving the complete file as output.What is the correct way to do it?

#include <stdio.h>

int main()
{
    FILE *fp,&fp2;
    char aa[50];
    float a,b,c;

    fp=fopen("abc.txt","r");
    fp2=fopen("aa.txt","w");
    while(!feof(fp))
    {
        fscanf(fp,"%s %f %f %f",&aa,&a,&b,&c);
        if((a!=999.999)&&(b!=999.999)&&(c!=999.999))
            fprintf(fp2,"%s %f %f %f",&aa,&a,&b,&c);
    }
    fclose(fp);
    fclose(fp1);
}

When I try to use

if((a=999.999)&&(b=999.999)&&(c=999.999))

it's giving only lines that contains 999.999 but I want the lines which do not contain 999.999..Bear with me I am new to C.

Thokchom
  • 1,602
  • 3
  • 17
  • 32
vijay krishna
  • 63
  • 1
  • 8

2 Answers2

6

Here's how to do it:

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

int main(void)
{
char str1[90];
FILE *fp=fopen("D:\\source.txt","r");
if(fp==NULL)
{
 printf("ERROR");
 exit(-1);
 }

while(fgets(str1,89,fp)!=NULL)
{
    if(strstr(str1,"999.999")==NULL)
    printf("%s",str1);
}
fclose(fp);

}

How it works: I've stored your data in the file source.txt.Then I read from that file line by line using fgets() into a string str.I make sure that it's size is big enough for each line.I used 90 arbitrarily.Then once I start reading from the lines from the file using the loop, I print it only if strstr() verifies that 999.999 is not a part of that line.Then loop exits when I reach the end of the file.Quite simple.Here's the output.

121220122563841210000000 0.2563 0.25698 2.3658
121220122563841210000000 2.365 2.365894 0.15463
121220122563841220000000 4.2563 6.25698 25.3658
Thokchom
  • 1,602
  • 3
  • 17
  • 32
  • +1 for `fgets() + strstr()` solution, I was going to post the exact same thing. –  May 16 '13 at 07:20
  • @H2CO3 Don't embarrass me saying that you were about to post what I posted.You are a Mercedes on StackOverflow, I am still an ox-cart. – Thokchom May 16 '13 at 07:21
  • Reputation doesn't mean *anything.* Last week there was a guy with 200k+ rep, yet he was so ignorantly insisting that a correct answer of mine was wrong that it hurt. I don't even understand... **On:** this is **the** correct solution, that's why I upvoted. Good luck for collecting reputation on SO! –  May 16 '13 at 07:26
  • @Thotkchom (two things to improve in your answer is 1. formatting, 2. `fgets(str1, sizeof(str1), f);` is safer, if you're interested :) ) –  May 16 '13 at 07:27
4

Comparison in floating point may not work properly. Refer here for more information Comparing floating point numbers in C

Just read the whole line using fgets() and check for substring 999.999 using strstr(). Values can be separated using strtok().

some problems in existing code:

  1. Don't use address of variable while printing.

    fprintf(fp2,"%s %f %f %f",**&aa,&a,&b,&c**);
    
  2. While reading its not mandatory to use address for char array.

     fscanf(fp,"%s %f %f %f",**&aa**,&a,&b,&c);
    
Community
  • 1
  • 1
Jeyaram
  • 9,158
  • 7
  • 41
  • 63
  • 1
    The idea Jeyram is proposing is simply to read the whole line into a buffer (similar to how you do it with the first item on a line into variable 'aa'). Then use the function strstr() to verify if there is a '999.999'. Google 'strstr' for more details and examples. – PeterK May 16 '13 at 07:12