I have a file with numbers on each line. I am trying to write the data read from the file to the int array with the read () function. I can read the file and print it to the terminal. How can i get the numbers i read into arr array ?
Here is my code
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
int main() {
int len, fd,arr[10000],i=0;
fd = open("1.txt", O_RDONLY);
if (fd >= 0) {
while(read(fd, &len, sizeof(int)) > 0){
write(1, &len, sizeof(int));
}
}
close(fd);
return 0;
}
Here, I read the numbers from the file and stored into a character array, then I converted it to an integer array. If there is any character other than number, I am assigining it as zero here in the array.