This is a input file

DEL 0050 XYZ 0050 0310 CCU 0710
MAA 1325 TRZ 10415
KWI 0050 FJR 0325 0325 CCJ
KWI 0050 FJR 0325 0425 CCJ 0925 1005

i.e maximum items(column entries) in a row can be 8
but their data type will be in same order
i.e char int char int int char int int

i am simply trying to read it
here's my code

#include<stdio.h>
#include<string.h>
main()
{
   FILE *fp;
   struct air
   {
    char a[50];
    int b;
    char c[50];
    int d;
    int e;
    char f[50];
    int g;
    int h;
   };
    struct air ia;
    fp=fopen("SC.txt","rb");
    if(fp==NULL)
    {
     puts("Cannot open file");
     exit(0);
    }

while(fscanf (fp,"%s %d %s %d %d %s %d %d\n",ia.a,&ia.b,ia.c,&ia.d,&ia.e,ia.f,&ia.g,&ia.h) != EOF)
 
{
  printf("%s %d %s %d %d %s %d %d\n",ia.a,ia.b,ia.c,ia.d,ia.e,ia.f,ia.g,ia.h);
       
}
          
fclose(fp);
}

But the output contains garbage value...
it is reads the1st line till 7th column enrty but after that it prints garbage value
similarly it reads the 2nd line till 4th entry but after that it prints garbage value
but in last line it is showing the entire line...
please help me...i need to read each element in file cuz i have to use and compare these characters with some value.

Recommended Answers

All 9 Replies

since the lines have variable number of entries fscanf() will be little use because it requires fixed length number of entries on the line. Instead, use fgets() to read the entire line into memory than parse for spaces and assign to approirate members of the structure. you can use either strtok() to check for spaces or simply strchr() which will return a pointer to the first space. strtok() is probably easier to work with because it will return a pointer to a null-terminated string, while strchr() will not (you have to do it yourself).

okay i can my input file

DEL 0050 XYZ 0050 0310 CCU 0710
MAA 1325 TRZ 10415
KWI 0050 FJR 0325 0325 CCJ
KWI 0050 FJR 0325 0425 CCJ 0925 1005

but i am facing another probelm.
After reading the file, i need to point to the data elements.
i.e if they are present then do some comparison(or condition)
else proceed to next line.

For example like first two lines

DEL 0050 XYZ 0050 0310 CCU 0710
MAA 1325 TRZ 1415

i need to point each element...if its present then do something(a condition), otherwise proceed to next line...and so on
for example data[4] i.e 0310 is present whereas in 2nd line data[4] is not present cuz the line ends after data[3]...
can i do this, if yes then how??? please help

please someone help...

Member Avatar for iamthwee

please someone help...

Hullo there, what's the problem? You have been told that using scanf is a waste of time, since each line does not contain the exact same number of variables. (It also makes your struct pretty useless too :-)

Do what ancient dragon suggested. If you can't do that you need to read up on the basics.

>>i need to point each element.

do you mean you want a pointer that connects element 1 of line 1 to element 1 of line 2? If line 1 and line 2 have correcponding element numbers then do something?

for example data[4] i.e 0310 is present whereas in 2nd line data[4] is not present cuz the line ends after data[3]...
can i do this, if yes then how??? please help

Perhaps something like this:

#include <stdio.h>

int main()
{
   static const char filename[] = "file.txt";
   FILE *file = fopen(filename, "r");
   if ( file != NULL )
   {
      char line[80];
      while ( fgets(line, sizeof line, file) != NULL )
      {
         char a[4], c[4], f[4];
         int b, d, e, g, h;
         int items = sscanf(line, "%3s %d %3s %d %d %3s %d %d", 
                            a, &b, c, &d, &e, f, &g, &h);
         if ( items > 0 )
         {
            printf("a = \"%s\"  ", a);
         }
         if ( items > 1 )
         {
            printf("b = %d  ", b);
         }
         if ( items > 2 )
         {
            printf("c = \"%s\"  ", c);
         }
         if ( items > 3 )
         {
            printf("d = %d  ", d);
         }
         if ( items > 4 )
         {
            printf("e = %d  ", e);
         }
         if ( items > 5 )
         {
            printf("f = \"%s\"  ", f);
         }
         if ( items > 6 )
         {
            printf("g = %d  ", g);
         }
         if ( items > 7 )
         {
            printf("h = %d  ", h);
         }
         putchar('\n');
      }
   }
   else
   {
      perror(filename);
   }
   return 0;
}

/* file.txt
DEL 0050 XYZ 0050 0310 CCU 0710
MAA 1325 TRZ 10415
KWI 0050 FJR 0325 0325 CCJ
KWI 0050 FJR 0325 0425 CCJ 0925 1005
*/

/* my output
a = "DEL"  b = 50  c = "XYZ"  d = 50  e = 310  f = "CCU"  g = 710  
a = "MAA"  b = 1325  c = "TRZ"  d = 10415  
a = "KWI"  b = 50  c = "FJR"  d = 325  e = 325  f = "CCJ"  
a = "KWI"  b = 50  c = "FJR"  d = 325  e = 425  f = "CCJ"  g = 925  h = 1005  
*/

If the numbers are really just text, I suppose you could use an array of strings and simplify that a bit.

[edit]My attention is lacking lately, but just in case your other thread pulls you along further, Daniweb readers now know that it's there.

Thanks alot to all you guys...
The problem somehow just got solved...
so once again thanks aton to all you who helped me out...and to those you read my query/problem.
Bye

The problem somehow just got solved...

If you don't know what it was that solved it, then you ought to consider it not yet fixed. I'd recommend posting the "solution" for further scrutiny.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.