Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The problem may be sscanf

  1. Oct 4, 2011 #1
    the function for read data from a txt.file.

    the data looks like:


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

    Code (Text):

    #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.*/
    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.

        }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);
            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"); }
                n*=2;   /*double the buffer*/


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

        /*convert string to data*/
            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));
                sscanf((ctemp+j)->absmag, "%lf", &((*catalog+j)->absmag));
                sscanf((ctemp+j)->dist, "%lf", &((*catalog+j)->dist));

    return i;      

        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);


  2. jcsd
  3. Oct 4, 2011 #2


    User Avatar
    Homework Helper

    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().
  4. Oct 4, 2011 #3
    I find something
    &(ctemp->pgc), &(ctemp->name), should be (ctemp+i)->pgc
    but still out of order
  5. Oct 5, 2011 #4


    Staff: Mentor

    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?
  6. Oct 5, 2011 #5
    i==14168 causing the problem
    memory error...
  7. Oct 5, 2011 #6
    in fact, the error occur in fscanf()
  8. Oct 5, 2011 #7


    Staff: Mentor

    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.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook