0

I'm currently using CodeBlocks to try read double type data from a file and save it into an array, which so far I had success doing so. The issue is that when I try to save the data into an array, it print returns something else.

There is allocation memory for the array, and the code seems to be "ok". but it does not works as planned.

This is the code that I'm using.

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 - Code:

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

As for the execution of the program, this is what I'm currently getting (Still don't quite figure out what is wrong).

This is What the console application output looks like

Console Output

ForceBru
  • 43,482
  • 10
  • 63
  • 98
  • 1
    Note that `allMags = (double*)malloc(sizeof(int)*i);` is probably not reserving enough memory. You need the size of `double` not that of `int`.Try `allMags = malloc(sizeof(*allMags)*i);` – Weather Vane Dec 02 '18 at 19:13
  • 1
    Aside: you shouldn't be using `feof` to control the file reading loop, but the return value from `fscanf` (the number of items scanned), such as `while(fscanf(ReadFile, "%lf", &FileNumber) == 1) {...}` – Weather Vane Dec 02 '18 at 19:15
  • 1
    And don't cast the return value of malloc: https://stackoverflow.com/questions/1565496/specifically-whats-dangerous-about-casting-the-result-of-malloc – Thomas Padron-McCarthy Dec 02 '18 at 19:17
  • 1
    Also please read [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) What you're doing with the condition in the `for` loop is equivalent to `while (!feof(...))`. – Some programmer dude Dec 02 '18 at 19:21
  • Thank You for the feedback! I'm fixing the code, hopefully it will work as expected. – Miguel Angel Garcia Acosta Dec 02 '18 at 19:35
  • Please not post picture of output. insert your code output in form of text. – EsmaeelE Dec 02 '18 at 21:14

2 Answers2

0

well i, see following as issues, correct them and see it helps.

J is not initialised so it would contain garbage value, it is potential problem initialise it to zero

J = 0;

why sizeof int when you are storing double values? it need to be sizeof(double) like below

allMags = (double*)malloc(sizeof(double)*i); 
ShivYaragatti
  • 398
  • 2
  • 8
0

Thank You all for your time and feedback!

I managed to rebuild the code and get it working.

This is the code for it:

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