Thread: Help needed to remove error

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    19

    Help needed to remove error

    Hello,

    I wrote a piece of code (using dev c compiler on windows), in which one function, namely 'noise_block_reader' is called by another function (I am not an expert programmer). This function searches if input file contains 'NAME (or Name or name)' string, if it founds (input file may have this at multiple places), it stores the line no where this is found (part-1 of function and it works nicely, no error). After this, program goes one by one on the line numbers where it was founded and reads further data in that line. The data line has this format -> NAME: string double double double. So program looks if ':' appears after NAME and than it stores one string and three double data and these data are passed to the calling function (part-2 of function). The string is passed by double pointer. The first loop runs correctly, but after this error occurs (familiar error: .exe has encountered a problem....Please tell Microsoft.....). I think most probably I am doing some mistake in using double pointer, but I could not dig it out.I am using double pointer first time Here is the function (part-2 only with function declaration line) and also the part of calling function, where such function is called.

    Function declaration line:

    Code:
    void noise_block_reader(FILE *fp_in, int start_line, int end_line, int nel, 
          char **var_name, double *el_data, int *err_index, FILE *fp_log)
    Part-2 of function, where error occurs

    Code:
    /* PART-2: Reading the data (one string and three double) from the lines, in which NAME appears
            format of line->   NAME: string  double  double  double */
         j=0;
         counter=0;
         if(k>0)  /* Line with NAME: found*/
         {
            for(i=0; i<=(k-2)/2; i++)
            {
                
                printf("\n Skipped lines: %d\n",*(line_element+counter));
                rewind(fp_in);
                skip_line_file(fp_in, *(line_element+counter), fp_log, err_index);
                
                fscanf(fp_in,"%c",&a);
                brk_index=0;
                
                /* checking the occurance of ':' after NAME (or Name or name)*/
                while(1)
                {
                   fscanf(fp_in,"%c",&a);
                   printf("\n   %c",a);
                   getch();
                   if(a == ':')brk_index=1;
                       printf("\n  BRKIND: %d",brk_index);
                   if(brk_index == 1)break;
                   if(a == '\n')break;
                }
                if(brk_index == 0)
                {
                   *err_index=1;
                   return;
                }
                
                /* Reading the data after : */
                printf("\n\n   ------I= %d",i);
                read_chk=fscanf(fp_in,"%s",*(var_name+i));
                if((read_chk < 1) || (read_chk == EOF))
                {
                   *err_index=1;
                   printf("\n\n  ERR in read");
                   getch();
                   return;
                }
                printf("\n Passed");
                //printf(fp_in,"\n\n %s",*(var_name+i));
                read_chk=fscanf(fp_in,"%lf %lf %lf",el_data+j, el_data+j+1, el_data+j+2);
                printf("  %lf  %lf  %lf",*(el_data+j), *(el_data+j+1), *(el_data+j+2));
                if((read_chk < 3) || (read_chk == EOF))
                {
                   *err_index=1;
                   return;
                }
                j=j+3;
                counter=counter+2;
            }
         }
         
         free(line_element);
         printf("\n\n MEMORY FREE");
         return;
    Part of the calling function, where this function is called

    Code:
    char **pnoise_elem, *noise_elem[100];
         int n_noise_elem;
         double *noise_a_f_ph;
    
         ------//-------
         n_noise_elem=nel_noise(fp_in, major_line_status[20], err_index, fp_log);
            noise_a_f_ph=calloc(3*n_noise_elem,sizeof(double));
    
         pnoise_elem=calloc(n_noise_elem, ELNAME_LEN);
            pnoise_elem=&noise_elem[0];
            noise_block_reader(fp_in, major_line_status[20], major_line_status[22], n_noise_elem, 
                      pnoise_elem, noise_a_f_ph, err_index, fp_log);

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Many difficult code for simple textlines, do you know sscanf?
    Last edited by BillyTKid; 10-06-2011 at 01:48 PM.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    19

    Unhappy

    Thanks Billi,

    Many difficult code for simple textlines, do you know sscanf?
    Yes.. I know the code is bigger than required....Only for debugging I made it longer and longerhttp://cboard.cprogramming.com/image...BJRU5ErkJggg== Here I am elaborating the problem more clearly...

    Here code is smaller

    Code:
    j=0;
         counter=0;
         if(k>0)  /* Line with NAME: found*/
         {
            for(i=0; i<=nel-1; i++)
            {
                rewind(fp_in);
                skip_line_file(fp_in, *(line_element+counter), fp_log, err_index);
                a=' ';
                
                fscanf(fp_in,"%c",&a[0]);
                a[1]=a[0];
                brk_index=0;
                
                while(1)
                {
                   fscanf(fp_in,"%c",&a[0]);
                   if(a == ':')brk_index=1;
                   if(brk_index == 1)break;
                   if(a == '\n')break;
                }
                if(brk_index == 0)
                {
                   *err_index=1;
                   return;
                }
                
                if(fgets(linech, sizeof(linech), fp_in) > 0)
                {
                   if(sscanf(linech,"%s %lf %lf %lf",(var_name+i),
                        el_data+j, el_data+j+1, el_data+j+2) != 4)
                        {
                           *err_index=1;
                           printf("\n  ERR in read");
                           
                        }
                }
                else
                {
                    printf("\n Wrong line:");
                }
                j=j+3;
                counter=counter+2;
            }
         }
         rewind(fp_in);
         free(line_element);
         return;
    Here I HAVE to check if user provided ':' in the file which separates NAME and string to be read. Thus NAME and : is not included in gets. What exactly error is that, the function correctly reads all the strings and doubles, but when these are passed to another function i.e. noise_block_reader, there error occurs. As an example I have in input file these three following lines:

    NAME: Q[5] 2.0e-03 10.000 0.0000
    NAME:Q[12] 5.5e-04 25.000 -0.040
    NAME: Q[8] 6.0e-02 15.540 0.010

    In function, if I use the statement
    Code:
    printf("\n     --------ELEMENT-%d:  %s",i+1,var_name+i);
    printf("\n      %lf  %lf  %lf",*(el_data+j), *(el_data+j+1), *(el_data+j+2));
    I got correct answer i.e.
    ELEMENT-1: Q[5] doubles also correct
    ELEMENT-2: Q[12] doubles also correct
    ELEMENT-3: Q[8] doubles also correct
    But in passed values (in another function), when I use the statement
    Code:
    for(local_i=0; local_i<=n_noise_elem-1; local_i++)
            {
               printf("\n ELEM%d: %s  Doubel  %lf   %lf   %lf",local_i+1,(pnoise_elem+local_i),
               *(noise_a_f_ph+local_j), *(noise_a_f_ph+local_j+1), *(noise_a_f_ph+local_j+2));
               local_j=local_j+3;
            }
    I got wrong answer and program halts with error as mentioned in previously.
    Here what I got is that:
    ELEM1: Q[5]Q[12Q[8] double correctly passed
    ELEM2: Q[12Q[8] double correctly passed
    ELEM3: Q[8] double correctly passed

    If I remove square brackets (square brackets will be in actual file), it passes correctly. I could not understand...Please help...

    Thanks and regards,
    Amal.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    19
    I am extremely sorry. I was using a copy of function and was making some changes in that. I pasted that edited code piece here in which, in the line no. 11, 12 and 17 'a' appears as an array. It is actual scalar so treat it like just int (so line 12 will not be there). Problem remains still....Sorry for the above err......

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error Help needed!
    By alionas in forum C Programming
    Replies: 2
    Last Post: 04-07-2011, 12:29 PM
  2. Remove a specific char and remove multiple spaces
    By stam in forum C++ Programming
    Replies: 9
    Last Post: 12-18-2010, 07:50 AM
  3. How to remove an error message from a p C++ program
    By tweetybrd22 in forum C++ Programming
    Replies: 2
    Last Post: 10-04-2010, 07:00 PM
  4. Running remove() gets permission denied error...
    By edomingox in forum C Programming
    Replies: 4
    Last Post: 01-11-2009, 12:55 PM
  5. compiler error...help needed!
    By Conquerer in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2002, 03:31 AM