如何读取C中每行都有数字的文件?

如何读取C中每行都有数字的文件?,c,C,我试图读取一个包含10个数字的文件,然后将它们添加到一个数组中,以便稍后对它们进行排序,但在读取它们时遇到了问题。不知道为什么这对我不起作用,有人能解释一下怎么了吗?每行上只有一个数字 10.05 11.01 9.03 出了什么问题: 您只存储一个字符 您正在对非换行字符每次更新count,而更新应针对换行字符 count在未初始化的情况下使用 转换为double不适用于此用途 可能的解决方案: int c; FILE* fptr; char line[1024]; // add lin

我试图读取一个包含10个数字的文件,然后将它们添加到一个数组中,以便稍后对它们进行排序,但在读取它们时遇到了问题。不知道为什么这对我不起作用,有人能解释一下怎么了吗?每行上只有一个数字

10.05
11.01
9.03
出了什么问题:

  • 您只存储一个字符
  • 您正在对非换行字符每次更新
    count
    ,而更新应针对换行字符
  • count
    在未初始化的情况下使用
  • 转换为
    double
    不适用于此用途
可能的解决方案:

int c;
FILE* fptr;

char line[1024]; // add line buffer and size tracking
int lineCount = 0;
double nums[10] = {0};
int count = 0; // initialize count

if ((fptr = fopen("filename", "r")) == NULL){
        printf("Error opening file.\n");
} else { // avoid using NULL to read file

    while ((c = getc(fptr)) != EOF){
            if (c == '\n'){ // update nums on newline character
                    line[lineCount] = '\0'; // don't forget to terminate the string
                    nums[count] = atof(line); // atof() from stdlib.h is useful to convert string to number
                    count = count + 1;
                    lineCount = 0; // start to read next line
            } else { // read line contents
                line[lineCount] = (char)c;
                lineCount = lineCount + 1;
            }
    }
    fclose(fptr);
}
我来了

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

int main()
{
   double values[10];
   int count;
   FILE *f = fopen("filename", "r");
   if (f == NULL)| {
      fprintf(stderr, "Some error message");
      return EXIT_FAILURE; // We cannot go any further - file is dead
   }
   // This is basic - you could overcome error problems
   // When able to read (including white space) we carry on until the array is full
   //  This is an area for improvement - error checking etc. 
   for (count = 0;  count < 10 && fscanf(f, " %lf", &values[count]) != 1; count ++);
   fclose(f);
   return EXIT_SUCCESS;
}
#包括
#包括
int main()
{
双值[10];
整数计数;
文件*f=fopen(“文件名”,“r”);
如果(f==NULL)|{
fprintf(stderr,“一些错误消息”);
return EXIT_FAILURE;//我们不能再继续了-文件已死亡
}
//这是基本的-您可以克服错误问题
//当能够读取(包括空白)时,我们将继续执行,直到阵列已满
//这是一个需要改进的领域-错误检查等。
对于(count=0;count<10&&fscanf(f,“%lf”,&value[count])!=1;count++);
fclose(f);
返回退出成功;
}

是否禁止
fscanf()
使用?
c
仅为一个字符。你希望
(double)c
做什么?10.05不是整数!没有,但我认为您需要知道文件中的确切数字才能使用“fscanf()”?该文件将追加新的数字,并以最大数字为10对其进行排序,以便其中不会总是有一个设定的数量。
printf(“打开文件时出错。\n”)-在此之后,您无法继续
printf(“打开文件时出错。\n”)-在此之后,您无法继续并。。。如果文件中有10000个值呢?可能还想说明
atof()
对失败的转换提供零诊断<代码>atof(“我的奶牛”)
将愉快地返回
0
,而不会发出任何警告。(最好使用
sscanf
strod
#include <stdio.h>
#include <stdlib.h>

int main()
{
   double values[10];
   int count;
   FILE *f = fopen("filename", "r");
   if (f == NULL)| {
      fprintf(stderr, "Some error message");
      return EXIT_FAILURE; // We cannot go any further - file is dead
   }
   // This is basic - you could overcome error problems
   // When able to read (including white space) we carry on until the array is full
   //  This is an area for improvement - error checking etc. 
   for (count = 0;  count < 10 && fscanf(f, " %lf", &values[count]) != 1; count ++);
   fclose(f);
   return EXIT_SUCCESS;
}