Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c语言中从文本文件中读取列的算法_C_Arrays_Scanf_Fopen - Fatal编程技术网

c语言中从文本文件中读取列的算法

c语言中从文本文件中读取列的算法,c,arrays,scanf,fopen,C,Arrays,Scanf,Fopen,这是我目前的代码: void NormRead(Normal *norm1) { FILE *normfile = fopen("C:\\Users\\Me\\Desktop\\NormData.txt", "r"); double NormArray[250]= {0}; if (normfile == NULL) { fprintf(stderr, "Error, could not open file.\n"); exit(99)

这是我目前的代码:

void NormRead(Normal *norm1) {
    FILE *normfile = fopen("C:\\Users\\Me\\Desktop\\NormData.txt", "r");

    double NormArray[250]= {0};

    if (normfile == NULL) {
        fprintf(stderr, "Error, could not open file.\n");
        exit(99);
    }

    for (int i=0 ; i<250 ; ++i) {

        if(feof(normfile)) {
            break;
        }

        fscanf(normfile, "%lg", &NormArray[i]);
    }

    printf("%lg", NormArray[0]);

    fclose(normfile);
}
实际值不同,但这不重要

有什么帮助吗?我觉得这应该在互联网上做到死,但我找不到任何帮助

提前谢谢。

试着把

在你的程序中没有像这样的错误

for (int i=0 ; i<250 ; ++i) {
当文件中没有进一步的数据时,
fscanf()
返回
-1
,时间
i
只不过是文件中的
no值
,打印列时,旋转循环
i

不要只打印数组[0],而是尝试将所有数组都打印为

for( j=0 ;j<i ; ++j)//  here i is the total no input in file
        printf("%lg\n", NormArray[j]);

for(j=0;j从许多不同的角度来看,您的代码毫无意义。首先:

void NormRead(Normal *norm1) 
您正在将
Normal*norm1
作为参数传递,但它从未在函数中使用过

不要在代码中硬编码文件名

    FILE *normfile = fopen("C:\\Users\\Me\\Desktop\\NormData.txt", "r");
如果函数中需要文件名-将
char*filename
作为参数传递。如果需要常量(例如默认文件名),在源文件的顶部声明它们。这样,您就永远不会通过源代码来调整常量,它们都是在一个简单方便的位置定义的,而不是在整个代码中散布和隐藏在函数中

如果只是输出从文件读取的前250个值,则不需要数组。只需打印这些值

关于函数中数组声明的重要旁注

此外(即使您没有尝试),您也无法返回指向在函数中本地声明了自动存储的数组的指针。该数组的存储存在于为函数提供的内存(函数堆栈帧)中,并在函数返回后立即销毁(释放以供重新使用)。(任何试图访问已释放内存的行为都是未定义的行为)

如果要填充函数中的数组并使调用方中的值可用,则唯一的选项是(1)将具有足够存储空间的数组作为参数传递给要填充的函数,或(2)在函数中动态分配内存,并返回一个指向新分配的块的指针,该块在函数返回后仍然有效。(或实际上是3-将有效指针的地址作为参数传递,并在函数中分配(或重新分配)和填充)

继续

如果要将打印的值限制为
250
,请不要在代码中硬编码幻数。可以如上所述声明一个常量,或者将该限制作为参数传递给函数。您可以使用简单的
#define
来定义这两个常量,例如

#define READMAX 250
#define FNDEFAULT "C:\\Users\\Me\\Desktop\\NormData.txt"
您确实验证了
normfile
是否已打开,但使用了
exit(99);
。C库提供了两个
exit
条件,并提供了常量
exit\u SUCCESS(0)
exit\u FAILURE(1)
。它们在
stdlib.h
中定义。建议您使用库提供的内容,而不是在代码中硬编码更多的神奇数字

您的读取循环很笨拙(充其量)。如果您将读取限制为
250
值,那么更合适的方法就是:

    int n = 0;
    double tmp;
    ...
    /* read up to READMAX double values from filename */
    while (n < READMAX && fscanf (normfile, "%lg", &tmp) == 1) {
        printf (" %g", tmp);
        n++;
    }
    putchar ('\n');     /* tidy up with newline */
(您只需反复打印读取的第一个值。
NormArray[0]
Zero实际上意味着Zero。此外,
%lg
的格式说明符不正确,因为
%g
在默认情况下需要一个
double
(与
scanf
函数不同))

总而言之,您试图做的事情可以归结为:

#include <stdio.h>
#include <stdlib.h>

#define READMAX 250
#define FNDEFAULT "C:\\Users\\Me\\Desktop\\NormData.txt"

void NormRead (char *filename) 
{
    int n = 0;
    double tmp;
    FILE *normfile = fopen (filename, "r");

    if (!normfile) {    /* validate file open for reading */
        fprintf (stderr, "error: file open failed '%s'.\n", filename);
        exit (EXIT_FAILURE);    /* defined in stdlib.h */
    }

    /* read up to READMAX double values from filename */
    while (n < READMAX && fscanf (normfile, "%lg", &tmp) == 1) {
        printf (" %g", tmp);
        n++;
    }
    putchar ('\n');     /* tidy up with newline */

    fclose (normfile);  /* close file */
}

int main (int argc, char **argv) {
    /* set fn to 1st argument (or FNDEFAULT if none given) */
    char *fn = argc > 1 ? argv[1] : FNDEFAULT;

    NormRead (fn);  /* call NormRead function */

    return 0;
}
示例使用/输出

$ ./bin/normread dat/normread.txt
 1 2 3 4 5

仔细检查一下,让我知道我是否正确解释了您试图执行的操作。如果您确实打算以某种方式使用数组,请让我知道,我很乐意进一步提供帮助。

“如果我以后打印整个内容,它仍然是零。”-->您如何打印?在您的函数中
NormRead()
,您正在打印读取文件内容的数组的第0个元素。A请注意,您没有使用
norm1
执行任何操作!这是答案!+1
    printf("%lg", NormArray[0]);
#include <stdio.h>
#include <stdlib.h>

#define READMAX 250
#define FNDEFAULT "C:\\Users\\Me\\Desktop\\NormData.txt"

void NormRead (char *filename) 
{
    int n = 0;
    double tmp;
    FILE *normfile = fopen (filename, "r");

    if (!normfile) {    /* validate file open for reading */
        fprintf (stderr, "error: file open failed '%s'.\n", filename);
        exit (EXIT_FAILURE);    /* defined in stdlib.h */
    }

    /* read up to READMAX double values from filename */
    while (n < READMAX && fscanf (normfile, "%lg", &tmp) == 1) {
        printf (" %g", tmp);
        n++;
    }
    putchar ('\n');     /* tidy up with newline */

    fclose (normfile);  /* close file */
}

int main (int argc, char **argv) {
    /* set fn to 1st argument (or FNDEFAULT if none given) */
    char *fn = argc > 1 ? argv[1] : FNDEFAULT;

    NormRead (fn);  /* call NormRead function */

    return 0;
}
$ cat dat/normread.txt
1

2

3

4

5
$ ./bin/normread dat/normread.txt
 1 2 3 4 5