Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatted I/O using fscanf 1

Status
Not open for further replies.

PSITom

Technical User
Feb 19, 2002
2
US
I'm doing something wrong....

If I have a multirow file with three fields per row like

Code:
 1  3.123 "line 1"
 2  29l.2 "line 2"
 3  42.45 "line 3"

and I want to read it in and manipulate the numeric fields independently, why won't this work:

Code:
int num[3], i;
float data[3];
char text[30];
main(){

for (i=0; i<2; i++){

pointer = fopen(filename, &quot;r&quot;));
fscanf(pointer, &quot; %d  %f %s&quot;, &num[i], &data[i]), &text); 
}
}
 
Something went screwy in the formatting so I'm reposting this:

I'm doing something wrong....

If I have a multirow file with three fields per row like


1 3.123 &quot;line 1&quot;
2 29l.2 &quot;line 2&quot;
3 42.45 &quot;line 3&quot;


and I want to read it in and manipulate the numeric fields independently, why won't this work:

int num[3], c;
float data[3];
char text[30];
main(){

for (c=0; c<2; c++){

pointer = fopen(filename, &quot;r&quot;));
fscanf(pointer, &quot; %d %f %s&quot;, &num[c], &data[c], &text);
}
}

 
Open the file Once and read its content upto the end of the file.

int num[3], c;
float data[3];
char text[30];
main()
{
FILE *pointer;
if( (pointer = fopen(filename, &quot;r&quot;)) == NULL) //error checking added
{
printf(&quot;Unable to open the file\n&quot;);
exit(-1);
}
for(c=0; ; c++) //Infinite loop
{
fscanf(pointer, &quot;%d %f %s&quot;, &num[c], &data[c], text); //text itself an address.
if(feof(pointer)) break; //break if the end of the file reached.
printf(&quot;\n%d %f %s&quot;, numc], data[c], text);
}
fclose(pointer);
}

Regards
Maniraja S
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top