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

  • Thread starter Thread starter nenyan
  • Start date Start date
  • Tags Tags
    Error Program
Click For Summary
The discussion revolves around a program designed to read data from a text file, which encounters a "Segmentation fault <core dumped>" error during execution. The issue is traced back to incorrect usage of the `fscanf` function. The original code attempts to read values directly into the structure fields without using the address-of operator (&), leading to memory access violations. The corrected line of code uses the address-of operator to properly pass the addresses of the structure members to `fscanf`. Additionally, suggestions are made to use a debugger to identify the exact line causing the crash and to ensure that memory allocation for the structure is handled correctly, including zeroing the memory before use. The importance of proper memory management and pointer usage in C programming is emphasized throughout the discussion.
nenyan
Messages
67
Reaction score
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
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:
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));
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 6 ·
Replies
6
Views
5K
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 22 ·
Replies
22
Views
6K
Replies
2
Views
7K
Replies
8
Views
3K
  • · Replies 11 ·
Replies
11
Views
4K
  • · Replies 11 ·
Replies
11
Views
2K