[C] How to read numbers from file?

Discussion in 'Programming' started by monumentplayer, Mar 14, 2009.

  1. #1
    Hi!

    I've got a file for example:

    
    Hello 123 456
    123
    Good day very good day
    None 56.67
    
    Code (markup):
    I want to read 123 , 456, 123 and 56.67 numbers without text.

    I found something like that:

    #include <stdio.h>
    
    	FILE *fi;
    	FILE *fo;
    
    int main()
    {
    	float a[20];
    	int i, n, x, z;
    	float limit;
    	char line[150];
    	char letter[25];
    	i=0;
    
    
    
    	fi = fopen("lab03_in.txt", "r");
    
    
    	fo = fopen("output.txt", "w");
    
    
    	while (fgets(line,1500,fi)!=NULL)
    {
    
    	sscanf(line,"%f", &limit);
    	//fscanf(fi,"%f", &limit);
    	a[i] = limit;
    	i++;
    
    }
    	printf("How many numbers would you like to input?  ");
    	scanf("%d", &x);
    
    	for (n = 0; n < x; n++)
    	{
    	printf("%.3f\n", a[n]);
    	}
    
    	
    
    
    fclose(fi);
    fclose(fo);
    getch();
    	return 0;
    }
    
    
    Code (markup):
    But it doesn't work for numbers between words and only for 1st number if it's also on the beginning of each line.


    Thanks,
    mp
     
    monumentplayer, Mar 14, 2009 IP