What is causing the segmentation fault in my program when reading a txt file?

In summary, the program written by the speaker was designed to read a txt file. However, when running the program, the system encountered a "Segmentation fault <core dumped>" error. The code provided includes a function for reading the information from the skymap and a main function for executing the program. Further debugging is suggested to ensure that the pointers and memory allocation are correct.
  • #1
nenyan
67
0
I wrote a program to read a txt file. When its running, system appear "Segmentation fault <core dumped>"

the code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define BUF 1024
#define pi 3.14159

typedef struct skymapdata
{
double ra;
double dec;
double p;
double pn;
} skydata;

/* This function is for readding skymap*/
int
readskymap( FILE *fp, skydata **sky)
{
skydata *sky1;
int i=0, n=BUF; /* n is the buffer. */

if ( !(*sky=(skydata *)malloc(n*sizeof(skydata))))
printf("memory error 1\n");

while (!feof(fp))
{ fscanf(fp, "%lf %lf %lf %lf", (*sky+i)->ra, (*sky+i)->dec, (*sky+i)->p, (*sky+i)->pn);
i++;
if (i==(n-1)) /*the number of lines is going to be longer than buffer. */
{
if(!(sky1=realloc(*sky, 2*n*sizeof(skydata))) ) {
printf("memory error 2\n"); }
*sky=sky1;
n*=2; } /*double the buffer*/
}

return i; /* Returning i tell us the accurate number.*/
}


int
main()
{
int i, n1;
skydata *sky;

FILE *fp;

if ( !( fp=fopen("skymap.txt", "r"))) {
printf ("cannot open the skymap.txt file\n");
return 1;}


n1=readskymap(fp, &sky);
for (i=0; i<n1; i++)
printf("%e %e %e %e\n", sky.ra, sky.dec, sky.p, sky.pn);



}



skymap.txt:
3.222 4.323 0.23 0.32
5.434 43.34545 0.3434 0.22
 
Technology news on Phys.org
  • #2
nenyan said:
I wrote a program to read a txt file. When its running, system appear "Segmentation fault <core dumped>"

the code:

Hello nenyan.

For future posts you should embed your code in code tags. Basically use CODE and /CODE with [ and ] around the tags. I will use this so that I can see a cleaned up version of your code:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define BUF 1024
#define pi 3.14159

typedef struct skymapdata
{
	double ra;
	double dec;
	double p;
	double pn;
} skydata;

/* This function is for readding skymap*/
int
readskymap( FILE *fp, skydata **sky)
{
	skydata *sky1;
	int i=0, n=BUF;       /* n is the buffer. */

	if ( !(*sky=(skydata *)malloc(n*sizeof(skydata))))  
		printf("memory error 1\n");

	while (!feof(fp))
	{ fscanf(fp, "%lf %lf %lf %lf", (*sky+i)->ra, (*sky+i)->dec, (*sky+i)->p, (*sky+i)->pn);
	i++;
	if (i==(n-1))                              /*the number of lines is going to be longer than buffer. */
	{
		if(!(sky1=realloc(*sky, 2*n*sizeof(skydata))) ) {
				printf("memory error 2\n"); }
		*sky=sky1;
		n*=2; }    /*double the buffer*/
	}  

	return i;    /* Returning i tell us the accurate number.*/
}int
main()
{
	int i, n1;     
    skydata *sky;

	FILE *fp;

	if ( !( fp=fopen("skymap.txt", "r"))) {
		printf ("cannot open the skymap.txt file\n");
		return 1;}	n1=readskymap(fp, &sky);
	for (i=0; i<n1; i++)
		printf("%e %e %e %e\n", sky[i].ra, sky[i].dec, sky[i].p, sky[i].pn);

   

}

skymap.txt:
3.222 4.323 0.23 0.32
5.434 43.34545 0.3434 0.22

Have you run this through a debugger? What line does it crash on?

I'm thinking that maybe you are passing by reference when you should be passing by value, or passing by value when you should be passing by reference.

To check this you could create some temporary pointer variables for your *(sky+i) and in the debugger make sure that all of these pointers have the right contents.

Also in terms of allocating your memory for your sky data structure, zero the memory and then check while you are debugging that the memory is indeed zeroed and also the right size.
 
Last edited:
  • #3
I found the reason.
fscanf(fp, "%lf %lf %lf %lf", (*sky+i)->ra, (*sky+i)->dec, (*sky+i)->p, (*sky+i)->pn);
should be
fscanf(fp, "%lf %lf %lf %lf", &((*sky+i)->ra), &((*sky+i)->dec), &((*sky+i)->p), &((*sky+i)->pn));
 

FAQ: What is causing the segmentation fault in my program when reading a txt file?

What is an error in a little program?

An error in a little program is a mistake or bug that causes the program to not run as intended. It could be a syntax error, logic error, or runtime error.

How do I identify an error in a little program?

Errors in a little program can be identified by running the program and checking for any unexpected behaviors or error messages. Debugging tools can also help pinpoint the specific location of an error.

3. What are common causes of errors in a little program?

Common causes of errors in a little program include typos, incorrect syntax, logical mistakes, and unexpected input from the user.

4. How can I fix an error in a little program?

The first step in fixing an error in a little program is identifying the specific error and its cause. Once identified, the error can be corrected by making necessary changes to the code. It may also require testing and debugging to ensure the error is fully resolved.

5. How can I prevent errors in a little program?

To prevent errors in a little program, it is important to write clean and organized code, use proper syntax and conventions, and thoroughly test the program before releasing it. Regularly reviewing and debugging the code can also help catch and prevent errors.

Similar threads

Back
Top