C language: reading a txt file and segment fault

In summary, the code provided has several issues with incorrect use of pointers, leading to a segmentation fault when run. The code attempts to read data from a file called "ranked_galaxies.txt" and write it to a file called "f_test.txt", but due to the issues with pointers, it is not able to successfully do so. These issues should be addressed in order for the code to function properly.
  • #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);


}
 
Last edited:
Technology news on Phys.org
  • #2
Help us out here. Are you having problems with this code? If so, are you getting compiler errors or link errors? If the code is compiling and linking without problems, but not producing the correct results, how are the results incorrect?
nenyan said:
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);


}
 
  • #3
the code is compiling and linking without problems.But no result. It creat f_test.txt file but the file is blank. and when I run this program. it told me: segmentation fault (core dumped).
 
  • #4
You are not using your pointers correctly. For example, in the first condition in the first if statement:

Code:
if ( !(*pgc=(double *)malloc(n*sizeof(double)))||
This assignment statement should not have * before pgc, since pgc already is a pointer. You want to store the address returned by malloc (cast as a double *) in pgc. You don't want to store this address at what pgc points at. You'll need to fix the same problem in
12 places, by my count (in both if statements).
 
  • #5
Also, you should put your code inside [ code] and [ /code] tags (without the leading space). Put a [ code] tag at the top and a [ /code] tag after the last line of your code. I have done that for you, and have also indented your code to make it easier to read.
nenyan said:
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


Code:
#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[i], mass[i], distance[i], tile[i], rank[i]); /*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);
}
 
  • #6
Your while loop is faulty.

After the last line is read from file, feof(fp) will still return false.
But if you read another line, the fscanf() will fail to read proper numbers.
Only then will feof(fp) return true.

I'd recommend checking the return value of fscanf().
 
  • #7
thank you all. the proble is solved.
 

FAQ: C language: reading a txt file and segment fault

How do I read a text file in C language?

To read a text file in C language, you need to use the fopen() function to open the file and then use the fscanf() function to read the contents of the file. You will also need to specify the file mode (e.g. "r" for read) when using fopen().

What is a segment fault in C language?

A segment fault, also known as a segmentation fault, is an error that occurs when a program tries to access a memory location that it does not have permission to access. This can happen when a program tries to access a null pointer or when it tries to write to read-only memory.

How can I prevent a segment fault in my C program?

To prevent a segment fault, it is important to properly allocate and free memory in your program. You should also use error handling techniques, such as checking for null pointers and using functions like malloc() and free() correctly.

How do I handle errors when reading a text file in C language?

You can handle errors when reading a text file in C language by checking the return value of the fopen() and fscanf() functions. If either of these functions return an error value, you can use the perror() function to print an error message and use the exit() function to terminate the program.

Can I read and write to a text file in C language simultaneously?

Yes, you can read and write to a text file in C language simultaneously by using the fopen() function with the "r+" or "w+" file mode. However, you must be careful to properly handle the file pointer and avoid overwriting important data in the file.

Back
Top