-3

I have data in a text file in the following format

WA5362288SeaTac city         25496    10176      434224    9.964121    0.167655 47.441406-122.293077
WA5363000Seattle city       563374   270524   151956998   83.872647   58.670927 47.626353-122.333144

Now I am trying to extract specific values from the above data (which has had some padding spaces removed), for example

WA SeaTAC 47.441406 -122.293077

I am unsure of how to get values from specific columns in C while streaming through a text file

    FILE *fp;
    fp = fopen("places.txt", "r");

    if(fp == NULL){
            fprintf(stderr, "Can't open the file");
            exit(1);
    }

    while(!feof(fp)){
       //extract values from specific column
       //fscanf()
    }
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
RRP
  • 2,563
  • 6
  • 29
  • 52
  • This is well documented and has countless answers here on SO and across the web. Is there something specific you're having problems with? – Christopher Schneider Feb 12 '17 at 03:52
  • Step 1: Read [`while (!feof(file))` is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) and fix your control loop. Step 2: Use [`fgets()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fgets.html) or POSIX function [`getline()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getline.html) to read lines and then maybe use [`sscanf()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/sscanf.html) to parse the lines. – Jonathan Leffler Feb 12 '17 at 03:54
  • Be more specific please. Your data extract is confusing. Are you only looking for the first column without number, and the last two columns? – SwiftMango Feb 12 '17 at 04:04
  • What's confusing about the format? It is not uncommon for things to run together in fixed-width fields. There's a 2-character field at the start, then a 7-digit number, then a string with spaces in it, then a bunch of numbers, and the geographic coordinates happen to run together when the longitude is negative and 3 digits before the decimal point. Nothing dreadfully difficult about that. You can specify the widths of the fields. You might think hard about a scan set such as `%30[\001-\377]` to pick up 30 characters (bytes) other than null bytes for the name, and then strip trailing blanks. – Jonathan Leffler Feb 12 '17 at 05:08

1 Answers1

3

I have assumed your file contains the following :

WA 5362288 SeaTac city         25496    10176      434224    9.964121    0.167655 47.441406 -122.293077
WA 5363000 Seattle city       563374   270524   151956998   83.872647   58.670927 47.626353 -122.333144

Let the file name is simple.txt . Then the following code meets your purpose :

   #include <stdio.h>

FILE *fp;

int main()
{
   char buff1[80],buff2[80];
   char buff3[80];
   int num,num1,num2,num3 ;
   float num4,num5,num6,num7;
   fp = fopen("simple.txt", "r");
    if(fp == NULL){
            fprintf(stderr, "Can't open the file");
            exit(1);
    }
    while(fscanf(fp, "%19s %d %19s %19s %d %d %D %f %f %f %f ", buff1,&num,buff2,buff3,&num1,&num2,&num3,&num4,&num5,&num6,&num7)!= EOF)
       printf("%s %d %s %s %d %d  %d %f %f %f %f \n",buff1,num,buff2,buff3,num1,num2,num3 ,num4,num5,num6,num7);


   fclose(fp);
}

This code prints the contents of file as shown below :

WA 5362288 SeaTac city 25496 10176  434224 9.964121 0.167655 47.441406 -122.293076
WA 5363000 Seattle city 563374 270524  151956998 83.872650 58.670925 47.626354 -122.333145

Hope this helps .

Christopher Marlowe
  • 2,098
  • 6
  • 38
  • 68