tags:

views:

118

answers:

1

scanf("%[^\n]\n",A[i]);

       /*that reads input line by line; example:first line is stored in A[0]*/

    -> But I want read each element of line and send to struct fuction  until 
           the EOL (end of line) 
    -> Explaining: In current line,read one data ,then send to  struct funcion 
      to hold,after then ,in for loop, read next data decide it is float 
      then send it to function.
      When eol is read, then activate next struct.
   >Question is I want write something in scanf such that I stop read when 
      I see eol. 

   Can I do                                    
              for( ; ; )                     for( ; ; )
               { scanf("...",&Sam);            { if(scanf("....", ...)==EOL)     
                 if(Sam=='\n)                        break;
                    break;                       }
               }                             
+1  A: 

Here's some ropey old untested code full of flaws but hopefully illustrating the basic principle of not reading directly into scanf:

#define BUFLEN 1024
#define MAX_VALS 200

char buf[BUFLEN + 1];

char* ptr;

float val[MAX_VALS];

int chars_read;
int n = 0;

while (!0)
{
    /* read the entire line into buf */
    if (!fgets(buf, BUFLEN, file))
        break;

    /* read buf using sscanf - assume float values, will barf if not */
    ptr = buf;
    while((chars_read = sscanf(ptr, "%f", &val[n])))
    {
        ptr += chars_read;
        ++n;
    }
}

Unfortunately, sscanf does not return the number of characters read, but the number items read and assigned. Alternatively:

char tmp[BUFLEN];
/* only read numerical values... this method is full of problems... still */
while((chars_read = sscanf(ptr, "%[0-9-+e.]", tmp)))
{
    sscanf(tmp, "%f", &val[n]);
    ptr += strlen(tmp);
    ++n;
}

etc, etc.

James Morris