- #1
nenyan
- 67
- 0
I use gcc to compile the code. When I run the program, I get segmentation fault (core dumped).
ranked_galaxies.txt:
# PGC Name RA DEC Mass Distance Diameter Tile_Number Rank_stat
37617 NGC3992 179.399700 53.374450 2.290868 22.909000 5.613000 1 2.51796409431e-05
62964 IC4837A 288.817350 -54.132520 1.819701 33.420000 2.946000 3 2.20718986336e-05
37306 NGC3953 178.454250 52.326530 1.235947 17.783000 5.743000 1 1.95861528891e-05
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* The function is for readding ranked_Galaxies.txt.*/
int
readrank( FILE *pFile, double **pgc, double **mass, double **distance, double **tile, double **rank)
{
void *pgc1, *mass1, *distance1, *tile1, *rank1;
char *temp, *temp1; /* these two variables can be overwritten. */
int i=0, n=1024; /* n is the buffer. */
if ( !(*pgc=(double *)malloc(n*sizeof(double)))||
!(*mass=(double *)malloc(n*sizeof(double)))||
!(*distance=(double *)malloc(n*sizeof(double)))||
!(*tile=(double *)malloc(n*sizeof(double)))||
!(*rank=(double *)malloc(n*sizeof(double)))||
!(temp=(char *)malloc(n*100)) )
printf("memory error\n");
while (!feof(pFile))
{ fscanf(pFile, "%lf %s %s %s %lf %lf %s %f %f", *pgc+i, temp, temp, temp, mass+i, distance+i, temp, tile+i, rank+i);
i++;
if (i==(n-1)) /*the number of lines is going to be longer than buffer. */
{
if(!(pgc1=realloc(*pgc, 2*n*sizeof(double)))||
!(mass1=realloc(*mass, 2*n*sizeof(double)))||
!(distance1=realloc(*distance, 2*n*sizeof(double)))||
!(tile1=realloc(*tile, 2*n*sizeof(double)))||
!(rank1=realloc(*rank, 2*n*sizeof(double)))||
!(temp1=realloc(temp, 2*n*100))) {
printf("memory error\n"); }
*pgc=pgc1;
*mass=mass1;
*distance=distance1;
*tile=tile1;
*rank=rank1;
temp=temp1;
n*=2; } /*double the buffer*/
}
free(temp);
return i; /* Returning i tell us the accurate number.*/
}
int
main()
{
int i,n2;
double *pgcr, *mass, *distance, *tile, *rank;
FILE *f_rank, *f_rank_test;
if ( !( f_rank_test=fopen("f_test.txt", "w"))) {
printf ("cannot open the f_test.txt file\n");
return 1;}
if ( !( f_rank=fopen("ranked_galaxies.txt", "r"))) {
printf ("cannot open the ranked_galaxies.txt file\n");
return 1;}
n2=readrank(f_rank, &pgcr, &mass, &distance, &tile, &rank);
for (i=0; i<n2; i++)
fprintf(f_rank_test, "%e, %e, %e, %e, %e \n", pgcr, mass, distance, tile, rank); /*check the program. if the codes work well we can get f_rank_test.txt.*/
free(pgcr);
free(mass);
free(distance);
free(tile);
free(rank);
fclose(f_rank);
fclose(f_rank_test);
}
ranked_galaxies.txt:
# PGC Name RA DEC Mass Distance Diameter Tile_Number Rank_stat
37617 NGC3992 179.399700 53.374450 2.290868 22.909000 5.613000 1 2.51796409431e-05
62964 IC4837A 288.817350 -54.132520 1.819701 33.420000 2.946000 3 2.20718986336e-05
37306 NGC3953 178.454250 52.326530 1.235947 17.783000 5.743000 1 1.95861528891e-05
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* The function is for readding ranked_Galaxies.txt.*/
int
readrank( FILE *pFile, double **pgc, double **mass, double **distance, double **tile, double **rank)
{
void *pgc1, *mass1, *distance1, *tile1, *rank1;
char *temp, *temp1; /* these two variables can be overwritten. */
int i=0, n=1024; /* n is the buffer. */
if ( !(*pgc=(double *)malloc(n*sizeof(double)))||
!(*mass=(double *)malloc(n*sizeof(double)))||
!(*distance=(double *)malloc(n*sizeof(double)))||
!(*tile=(double *)malloc(n*sizeof(double)))||
!(*rank=(double *)malloc(n*sizeof(double)))||
!(temp=(char *)malloc(n*100)) )
printf("memory error\n");
while (!feof(pFile))
{ fscanf(pFile, "%lf %s %s %s %lf %lf %s %f %f", *pgc+i, temp, temp, temp, mass+i, distance+i, temp, tile+i, rank+i);
i++;
if (i==(n-1)) /*the number of lines is going to be longer than buffer. */
{
if(!(pgc1=realloc(*pgc, 2*n*sizeof(double)))||
!(mass1=realloc(*mass, 2*n*sizeof(double)))||
!(distance1=realloc(*distance, 2*n*sizeof(double)))||
!(tile1=realloc(*tile, 2*n*sizeof(double)))||
!(rank1=realloc(*rank, 2*n*sizeof(double)))||
!(temp1=realloc(temp, 2*n*100))) {
printf("memory error\n"); }
*pgc=pgc1;
*mass=mass1;
*distance=distance1;
*tile=tile1;
*rank=rank1;
temp=temp1;
n*=2; } /*double the buffer*/
}
free(temp);
return i; /* Returning i tell us the accurate number.*/
}
int
main()
{
int i,n2;
double *pgcr, *mass, *distance, *tile, *rank;
FILE *f_rank, *f_rank_test;
if ( !( f_rank_test=fopen("f_test.txt", "w"))) {
printf ("cannot open the f_test.txt file\n");
return 1;}
if ( !( f_rank=fopen("ranked_galaxies.txt", "r"))) {
printf ("cannot open the ranked_galaxies.txt file\n");
return 1;}
n2=readrank(f_rank, &pgcr, &mass, &distance, &tile, &rank);
for (i=0; i<n2; i++)
fprintf(f_rank_test, "%e, %e, %e, %e, %e \n", pgcr, mass, distance, tile, rank); /*check the program. if the codes work well we can get f_rank_test.txt.*/
free(pgcr);
free(mass);
free(distance);
free(tile);
free(rank);
fclose(f_rank);
fclose(f_rank_test);
}
Last edited: