2

Thus I have read the data from file and print in display but I cannot save the data read from file into new array.

file data eg:

23,56
78,65

My code

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

int main() {
    FILE *ptr = fopen("datasetLR1.txt", "r");
    if(ptr == NULL) {
        printf("File not found or allocated!");
        exit(1);
     }
    int str[85];
    while(fgets(str, sizeof(str), ptr) != NULL) { 
        printf(fgets(str, sizeof(str), ptr));
    }
    system("pause");
    fclose(ptr);
    return 0;
}
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
Hello
  • 29
  • 2

1 Answers1

1

You need to change theese parts:

    char rowBuffer[85];  // fgets want's a char buffer
    while (fgets(rowBuffer, sizeof(rowBuffer), ptr) != NULL) {
        printf("%s", rowBuffer);  // We do not want to read and print again at the same time
    }

Your solution does nothing (yet) to interpret the integer values.

Also, as commented by others, printf(src) is unsafe in-case the data contains formatting content like "... %s ..." that can mess with your program execution. puts(str) (if you want to add LF) or printf("%s", str) for literal string reproduction.

Bo R
  • 2,334
  • 1
  • 9
  • 17