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

  • Thread starter nenyan
  • Start date
In summary: That way, you can control how big of a string you accept, and you can use sscanf.Also, if you use sscanf("%s", &((*catalog+j)->pgc)) to read into a string (i.e. array of characters), you don't need the &. So it could be just sscanf("%s", (*catalog+j)->pgc).In summary, The conversation discusses a function for reading data from a txt.file and the use of debugging tools to find an error that occurred in sscanf. The error is related to fscanf and is caused by attempting to read a string that is too large for the allocated memory. It is suggested to use a loop and sscanf to control the size of the input string.
  • #1
nenyan
67
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
  • #2
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().
 
  • #3
I find something
&(ctemp->pgc), &(ctemp->name), should be (ctemp+i)->pgc
but still out of order
 
  • #4
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?
 
  • #5
i==14168 causing the problem
memory error...
 
  • #6
in fact, the error occur in fscanf()
 
  • #7
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.
 

What is the "problem may be sscanf" error?

The "problem may be sscanf" error is a common error message that occurs when using the sscanf function in C. It indicates that there is an issue with the format string or with the variables being passed into the function.

Why does the "problem may be sscanf" error occur?

The "problem may be sscanf" error occurs when the format string and the variables being passed into the sscanf function do not match. This could be due to a typo in the format string or passing in the wrong types of variables.

How can I fix the "problem may be sscanf" error?

To fix the "problem may be sscanf" error, you should check your format string and make sure it matches the types of variables being passed into the sscanf function. You should also ensure that the variables are being passed in the correct order.

Can other functions cause the "problem may be sscanf" error?

Yes, other functions can also cause the "problem may be sscanf" error if they use a similar format string and variable structure. It is important to double check all functions that use a format string to prevent this error from occurring.

Is there a way to prevent the "problem may be sscanf" error?

Yes, to prevent the "problem may be sscanf" error, you should carefully check your format string and variables before using the sscanf function. You should also use proper error handling techniques to catch and handle any errors that may occur.

Similar threads

  • Programming and Computer Science
Replies
6
Views
4K
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Programming and Computer Science
Replies
6
Views
3K
  • Programming and Computer Science
Replies
13
Views
19K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
2K
  • Programming and Computer Science
Replies
2
Views
13K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
3K
Back
Top