B
bowlderster
Hello, all.
This is the text file named test.txt.
1041 1467 7334 9500 2169 7724 3478 3358 9962 7464
6705 2145 6281 8827 1961 1491 3995 3942 5827 6436
6391 6604 4902 1153 1292 4382 9421 1716 2718 2895
I wanna to read the data to an array, as the follows:
#include <stdio.h>
#define MAX 30
int main()
{
int i = 0 ;
FILE *fp ;
int a[30] = {0};
if( (fp = fopen("test.txt", "r")) == NULL )
printf("Open file fail!!!\n");
for(i = 0 ; i < MAX ; i++)
{
fscanf(fp, "%i", &a) ;
printf("%i\t",a);
}
fclose(fp);
return 0;
}
It is successful.
And I change the type of the array to float
....
float a[30];
....
fscanf(fp,"%f",&a);
printf("%f\t",a);
....
It works as well.
But when I change the type of the array to double
....
double a[30];
....
fscanf(fp,"%g",&a);
printf(fp,"%g",a);
....
It cannot work. It seams that the array is zero.
I wanna to know the reason and how can read the data to a double array.This is one question.
Another is that:
If the data file test.txt is in the following format,
a b c d e f g h i j
=============================================
1041 1467 7334 9500 2169 7724 3478 3358 9962 7464
6705 2145 6281 8827 1961 1491 3995 3942 5827 6436
6391 6604 4902 1153 1292 4382 9421 1716 2718 2895
how to read the file directly to the 3rd line? I mean that I just wanna to read the numbers.
I know fseek function can move the file pointer to a certain position.
But for a text file, how to caculate he offset?
If it can work, please give me your example for the above file.
I am learning the language at the beginning. Any encouragement will be helpful.
This is the text file named test.txt.
1041 1467 7334 9500 2169 7724 3478 3358 9962 7464
6705 2145 6281 8827 1961 1491 3995 3942 5827 6436
6391 6604 4902 1153 1292 4382 9421 1716 2718 2895
I wanna to read the data to an array, as the follows:
#include <stdio.h>
#define MAX 30
int main()
{
int i = 0 ;
FILE *fp ;
int a[30] = {0};
if( (fp = fopen("test.txt", "r")) == NULL )
printf("Open file fail!!!\n");
for(i = 0 ; i < MAX ; i++)
{
fscanf(fp, "%i", &a) ;
printf("%i\t",a);
}
fclose(fp);
return 0;
}
It is successful.
And I change the type of the array to float
....
float a[30];
....
fscanf(fp,"%f",&a);
printf("%f\t",a);
....
It works as well.
But when I change the type of the array to double
....
double a[30];
....
fscanf(fp,"%g",&a);
printf(fp,"%g",a);
....
It cannot work. It seams that the array is zero.
I wanna to know the reason and how can read the data to a double array.This is one question.
Another is that:
If the data file test.txt is in the following format,
a b c d e f g h i j
=============================================
1041 1467 7334 9500 2169 7724 3478 3358 9962 7464
6705 2145 6281 8827 1961 1491 3995 3942 5827 6436
6391 6604 4902 1153 1292 4382 9421 1716 2718 2895
how to read the file directly to the 3rd line? I mean that I just wanna to read the numbers.
I know fseek function can move the file pointer to a certain position.
But for a text file, how to caculate he offset?
If it can work, please give me your example for the above file.
I am learning the language at the beginning. Any encouragement will be helpful.