Why is sscanf Causing an Error When Reading Data from a Text File?

  • Thread starter Thread starter nenyan
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around issues encountered when using the `sscanf` function to read data from a text file in a C program. Participants explore potential causes of errors related to memory allocation, data parsing, and the structure of the input data.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • One participant notes that the allocation of the `catalog` variable in `main` is only for a single instance, while it is treated as an array in `readevent()`.
  • Another participant suggests that the indexing for `ctemp` should be corrected from `&(ctemp->pgc)` to `(ctemp+i)->pgc`, indicating a potential issue with accessing the correct memory location.
  • A participant points out that the error might actually be occurring in the `fscanf()` function rather than `sscanf()`, highlighting the need to identify the specific line causing the problem.
  • One participant speculates that the memory allocated for the `temp` variable may be insufficient to hold the data being read, suggesting that a loop could be used to read each line into a string before parsing it.
  • Another participant mentions that an error occurs when `i` reaches 14168, indicating a possible memory error at that point.

Areas of Agreement / Disagreement

Participants express differing views on the source of the error, with some attributing it to memory allocation issues and others focusing on the parsing logic. The discussion remains unresolved as no consensus is reached on the exact cause of the problem.

Contextual Notes

There are indications of potential memory overflow issues due to insufficient allocation sizes, as well as the possibility of incorrect indexing in data structures. The discussion highlights the complexity of handling dynamic memory and parsing in C programming.

nenyan
Messages
67
Reaction score
0
the function for read data from a txt.file.

the data looks like:

2|name|3.444|...|..|
3|name|5.344|...|..|

I use debugging tools and find the error occurred in sscanf. But I can not find the specific point. please help me.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define BUF 10240
#define pi 3.14159
#define SKYFILE "H1L1V1-LUMIN_cbc_trig-970399241-1.txt"
#define CATALOG "GWGCCatalog.txt"
#define ResultNumber 20


typedef struct catalogdata
{	char pgc[20];
	char name[100];
	double ra;
	double dec;
	double absmag;
	double dist;} cdata;

typedef struct catalogtemp
{	char pgc[20];
	char name[100];
	char ra[30];
	char dec[30];
	char absmag[20];
	char dist[20];} catatemp;






	/* This function is for readding catalog.*/
int
readevent( FILE *pFile, cdata **catalog)
{
	
	catatemp *ctemp, *ctemp1;  
	void *temp;
    int c, i=0, j, n=BUF;       /* n is the buffer. */
	
	if ( !(ctemp=(catatemp *)malloc(n*sizeof(catatemp))))  
		printf("memory error 1\n");                        //allocate memory.
	if ( !(temp=(char *)malloc(100)))  
		printf("memory error 1\n");                        //allocate memory for temp.

	do{
		c=getc(pFile);
	}while(c!='\n');                                   //skip the first line.

	while (!feof(pFile))
	{  
		/*read all data as a string*/
		fscanf(pFile, "%[^|]|%[^|]|%[^|]|%[^|]|%[^|]|%[^|]|%[^|]|%[^|]|%[^|]|%[^|]|%[^|]|%[^|]|%[^|]|%[^|]|%[^|]|%[^|]|%[^|]|%[^|]|", &(ctemp->pgc), &(ctemp->name), &(ctemp->ra), &(ctemp->dec), temp, temp, temp, temp, temp, temp, temp, temp, temp, &(ctemp->absmag), &(ctemp->dist), temp, temp, temp);
		i++;
		
		if (i==(n-1))                              /*the number of lines is going to be longer than buffer. */
		{
			if(!(ctemp1=realloc(ctemp, 2*n*sizeof(cdata)))) {
				printf("memory error 2\n"); }
			ctemp=ctemp1;
			n*=2;   /*double the buffer*/
		}  

	}

	if ( !(*catalog=(cdata *)malloc(i*sizeof(cdata))))  
		printf("memory error 1\n");                        //allocate memory.

	/*convert string to data*/
	for(j=0;j<i;j++)
	{	
		sscanf((ctemp+j)->pgc, "%s", &((*catalog+j)->pgc));
		sscanf((ctemp+j)->name, "%s", &((*catalog+j)->name));
		sscanf((ctemp+j)->ra, "%lf", &((*catalog+j)->ra));
		sscanf((ctemp+j)->dec, "%lf", &((*catalog+j)->dec));
		if((ctemp+j)->absmag=="~")
			(*catalog+j)->absmag=10000.0;
		else
			sscanf((ctemp+j)->absmag, "%lf", &((*catalog+j)->absmag));
		if((ctemp+j)->dist=="~") 
			(*catalog+j)->dist=10000.0;
		else
			sscanf((ctemp+j)->dist, "%lf", &((*catalog+j)->dist));
	}

free(temp);
free(ctemp);
free(ctemp1);
return i;       
}

int
main()
{
	int i, n;
	cdata *catalog;
	FILE *pFile;
	
	if ( !( pFile=fopen(CATALOG, "r"))) {
		printf ("cannot open the cata_std.txt file\n");
		return 1;}
	n=readevent(pFile, &catalog);                         //catalog done.

	for(i=0; i<n; i++)
		printf("%s, %s, %e, %e, %e, %e\n", catalog[i].pgc, catalog[i].name, catalog[i].ra, catalog[i].dec, catalog[i].absmag, catalog[i].dist);

free(catalog);
fclose(pFile);

}
 
Technology news on Phys.org
You only allocate one instance of catalog in main, but you treat it as an array in readevent() when you reference ( ... catalog + j ...). It might also help to add temporary code to print out the structure for each loop in readevent().
 
I find something
&(ctemp->pgc), &(ctemp->name), should be (ctemp+i)->pgc
but still out of order
 
When you are using the debugger and see a problem with sscanf, which line is causing the problem, and what is the error that is generated?
 
i==14168 causing the problem
memory error...
 
in fact, the error occur in fscanf()
 
Here's my guess. The heap memory that temp points to consists of 100 bytes. One of the file reads (using fscanf) pulls in a string that's too big to fit in 100 bytes, which causes the problem you're seeing.

Instead of using fscanf to input to six variables you care about, and a slew of "don't care" variables, it might be better to have a loop. Each iteration of the loop could read one line of data from the file and store it in a string, and then parse the string.
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
5K
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
2
Views
7K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
26K
  • · Replies 13 ·
Replies
13
Views
21K