Reading data from file

ok problem looks simple for now.

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

1 5
3 7
4 8

i had this line in the code. c=fscanf(fp,"%lf %lf

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.


  • I would read the entire line at one time -- fgets() -- then use strtok() or something to retrieve the individual integers. Something like this.
    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);
    p = strtok(NULL," ");
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!


In this Discussion