Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone, my program doesnt seem to be reading from the textfile raw.txt which i placed in the same directory as the .exe file. Does anyone know why? i have used this function before and it worked in another program. but now when i printf whats in the array a[], all i get is zeros and rubbish values. Pls help me! im desperate!

i got a debug error, run-time check failure #@ - stack aorund the variable 'time' was corrupted.



C#
int main()
{
int channel, maxdatapoints ,size1=0 ,i=0 ,k=0 ,j=0 , totalsize=0 ;
int time;
int ans,f, maxf=0 ,maxf2=0 ,maxans2=0 ,maxans=0 ;
double *a,*temp ,*diff,x;
FILE *fread, *histogramw, *histogramr,*result;
        int  frequency=0; /*initialise the frequency of a variable, and the variable x*/


    printf("Enter the maximum number of data points to compute \n");
    scanf("%d", &maxdatapoints);


    a=(double *)calloc(maxdatapoints,sizeof(double));

        if ((fread = fopen("raw.txt", "r")) == NULL)
        {
            printf("Cannot open %s\n", "raw.txt");
            exit(EXIT_FAILURE);
        }



            while (!feof(fread) && i<maxdatapoints)
            { /*Read the output file*/
                fscanf(fread, "%lf %*c %d\n", &time, &channel);

                /*If channel 1, write that to channel1.txt*/
                if(channel==1)
                {
                    a[i]=time;

                size1=size1+1;
                    i=i+1;

                printf("%lf \n", a[i]);

                }
            }

    fclose(fread);

Posted
Updated 10-Feb-14 3:25am
v2
Comments
Maarten Kools 10-Feb-14 7:57am    
Is it working if you execute the program from the command line? Sometimes when you run a program out of the debugger (are you using Visual Studio?) the working directory is not where the executable is build.
Member 10512547 10-Feb-14 8:00am    
yes i am using visual studio 2012. how do i execute from the command line? i am running the program from the debug folder where i also pasted my raw.txt file

1 solution

You are printing a[i] after i has been incremented. So you are printing an element which has not been assigned so far. Move the increment operation behind the printf statement:
// i=i+1;
printf("%lf \n", a[i]);
i++;


[EDIT]
There is another error: The variable time is declared as int but passed to fscanf() to store a double which results in a stack corruption.
 
Share this answer
 
v2
Comments
Member 10512547 10-Feb-14 9:24am    
i did that but its still not working. i got an error of runtime check failure #2 stack around the variable time was corrupted
Jochen Arndt 10-Feb-14 9:32am    
See my updated solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900