Reading data from file

ok problem looks simple for now.

here is the text that i want to read from the file.

1 5
2
3 7
4 8
9

i had this line in the code. c=fscanf(fp,"%lf %lf
",&x,&y);

now it reads the table fine when the values are two. but it doesnt read the table properly if their is only one value in the record.

can anyone help in writing the line for scaning the record line with one value.

Comments

  • I would read the entire line at one time -- fgets() -- then use strtok() or something to retrieve the individual integers. Something like this.
    [code]
    char line[80];
    int nums[2],i;
    while( fgets(line,sizeof(line),fp) )
    {
    char* p = strtok(line," ");
    nums[0] = nums[1] = 0;
    i = 0;
    while(p != 0)
    {
    nums[i] = atoi(p);
    i++;
    p = strtok(NULL," ");
    }
    }
    [/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion