【问题标题】:Read data file and save to array in C读取数据文件并保存到C中的数组
【发布时间】:2018-12-02 19:06:31
【问题描述】:

我目前正在使用 CodeBlocks 尝试从文件中读取双精度类型数据并将其保存到数组中,到目前为止我已经成功地这样做了。问题是,当我尝试将数据保存到数组中时,它 print 会返回其他内容。

数组有分配内存,代码似乎“ok”。但它没有按计划工作。

这是我正在使用的代码。

读取文件:

-9.9

-8.8

-7.7

-6.6

-5.5

-4.4

-3.3

-2.2

-1.1

0.0

1.1

2.2

3.3

4.4

5.5

6.6

7.7

8.8

9.9

10.10

C - 代码:

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i = 0; //Array Size
int j; //Loop index
double* allMags; //Memory Allocation Array; Set Later

//START SECTION OF CODE TO ACCESS DOCUMENT FILE
FILE* ReadFile = NULL; //File Pointer
double FileNumber; //Data Value Read From File "ReadFile"
//OPEN FILE
printf("Opening File: ReadFile.txt\n");
ReadFile = fopen("ReadFile.txt", "r");
//If file fails to open
if (ReadFile ==  NULL)
{
    printf("File could not be opened: 'ReadFile.txt'\n");
    return -1; // -1 indicates Error.
}
//END SECTION OF CODE TO ACCESS DOCUMENT FILE

//START READ DATA AND STORE ARRAY SIZE
printf("Reading and printing numbers.\n");
for (i = i + 1; !feof(ReadFile); i++)
{
    fscanf(ReadFile, "%lf", &FileNumber);
    printf("Entry #%d || Read: %.2lf\n", i, FileNumber);
}
fclose(ReadFile); //End File for this instance
printf("\nArray Size: %d\n", i);
//END READ DATA AND STORE ARRAY SIZE

//START MEMORY ALLOCATION
allMags = (double*)malloc(sizeof(int)*i);
if (allMags == NULL)
{
    printf("Closing File: ReadFile.txt\n");
    fclose(ReadFile); //Done with File, close it
    printf("allMags is NULL\n");
    return -1;
}
//END MEMORY ALLOCATION

//START SCAN AND PRINT FROM ARRAY -- allMags
while (j <= i)
{
    fscanf(ReadFile, "%lf", &(allMags[j-1]));
    j = j + 1;
}
fclose(ReadFile); //Close File
printf("\nSaved Data:\n");
printf("\n");
j = 1;

while (j < i)
{
    printf("Index: %d || Saved Read: %.2f\n", j, allMags[j]);
    j++;
}

return 0; //KILL
}

至于程序的执行,这是我目前得到的(还是不太明白哪里出了问题)。

这是控制台应用程序输出的样子

【问题讨论】:

  • 请注意allMags = (double*)malloc(sizeof(int)*i); 可能没有保留足够的内存。您需要double 的大小而不是int 的大小。试试allMags = malloc(sizeof(*allMags)*i);
  • 旁白:你不应该使用feof来控制文件读取循环,而是来自fscanf的返回值(扫描的项目数),例如while(fscanf(ReadFile, "%lf", &amp;FileNumber) == 1) {...}
  • 并且不要强制转换malloc的返回值:stackoverflow.com/questions/1565496/…
  • 另外请阅读Why is “while ( !feof (file) )” always wrong? 你对for 循环中的条件所做的操作相当于while (!feof(...))
  • 感谢您的反馈!我正在修复代码,希望它能按预期工作。

标签: c computer-science


【解决方案1】:

好吧,我认为以下是问题,纠正它们并看到它有帮助。

J 未初始化,因此它会包含垃圾值,将其初始化为零是潜在的问题

J = 0;

为什么在存储双精度值时使用 sizeof int?它需要像下面的 sizeof(double)

allMags = (double*)malloc(sizeof(double)*i); 

【讨论】:

    【解决方案2】:

    感谢大家的宝贵时间和反馈!

    我设法重建了代码并让它工作。

    这是它的代码:

    #include <stdio.h>
    #include <stdlib.h>
    
    /* UNIVERSAL */
    FILE* ReadFile = NULL; //File Pointer
    double FileNumber; //Data Value Read From File "ReadFile"
    /* UNIVERSAL */
    
    int Read_Check(int i)
    {
    ReadFile = fopen("ReadFile.txt", "r");
    if (ReadFile ==  NULL)
    {
        printf("File could not be opened: 'ReadFile.txt'\n");
        return -1; // -1 indicates Error.
    }
    printf("ReadCheck\n\n");
       while(fscanf(ReadFile, "%lf", &FileNumber) == 1)
    {
        printf("Entry #%d || Read: %.2lf\n", i, FileNumber);
        i++;
    }
    printf("\nClosing File.\n");
    fclose(ReadFile); //Done with file, so close it
    return i;
    }
    
    int main (void)
    {
    int Array_Size = 0; //Initialize to 0
    int i = 0; //index
    Array_Size = Read_Check(Array_Size); //Check Read and Get Array_Size
    printf("\nArray Size: %d\n", Array_Size);
    
    //START SECTION OF CODE TO ACCESS DOCUMENT FILE
    double* allMags;
    //OPEN FILE
    printf("Opening File: ReadFile.txt\n");
    ReadFile = fopen("ReadFile.txt", "r");
    //If file fails to open
    if (ReadFile ==  NULL)
    {
        printf("File could not be opened: 'ReadFile.txt'\n");
        return -1; // -1 indicates Error.
    }
    //END SECTION OF CODE TO ACCESS DOCUMENT FILE
    
    //START MEMORY ALLOCATION
    allMags = (double*)malloc(sizeof(double)*Array_Size);
    //END MEMORY ALLOCATION
    
    //START READ DATA AND STORE ARRAY SIZE
    printf("Reading and printing saved numbers.\n");
    printf("\nSaved Data:\n");
    
    while (fscanf(ReadFile, "%lf", &FileNumber) == 1 && i <= Array_Size)
    {
        allMags[i] = FileNumber;
        printf("Index #%d || Read: %.2lf\n", i, allMags[i]);
        i++;
    }
    //END READ DATA AND STORE ARRAY SIZE
    return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-18
      • 2012-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-05
      相关资源
      最近更新 更多