为什么我的程序没有从文件文件中读取? [英] Why is my programme not reading from the textfile?

查看:65
本文介绍了为什么我的程序没有从文件文件中读取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我的程序似乎没有从text文件raw.txt中读取,我将它放在与.exe文件相同的目录中。有谁知道为什么?我以前使用过这个功能,它在另一个程序中工作。但现在当我在数组a []中打印出什么内容时,我得到的只是零和垃圾值。请帮助我!我很绝望!



i收到调试错误,运行时检查失败#@ - 堆栈aorund变量'time'已损坏。







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.



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



推荐答案

您在 i a [i] >已增加。因此,您要打印到目前为止尚未分配的元素。将增量操作移到 printf 语句后面:

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







还有另一个错误:变量 time 声明为 int 但传递给 fscanf()以存储 double 会导致堆栈损坏。




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.


这篇关于为什么我的程序没有从文件文件中读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆