简体   繁体   中英

How to read lines from a file based on a criteria on the lines's content?

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.

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

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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM