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).
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
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.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
>>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?
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
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.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314