读取数据文件并保存到C中的数组

时间:2018-12-02 19:06:31

标签: c computer-science

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

该数组有分配内存,并且代码似乎“确定”。但是它不能按计划进行。

这是我正在使用的代码。

ReadFile:

-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
}

关于程序的执行,这就是我目前正在得到的(仍然不太清楚哪里出了问题)。

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

Console Output

2 个答案:

答案 0 :(得分:0)

好吧,我将以下问题视为问题,予以纠正并认为有帮助。

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

J = 0;

为什么在存储双精度值时使用sizeof int?它必须像下面一样是sizeof(double)

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

答案 1 :(得分:0)

谢谢大家的时间和反馈!

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

这是它的代码:

#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;
}