I'm trying to get numbers from txt file, I wrote a code like below;
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <ctype.h>
int main() {
FILE *file;
file = fopen("example_input.txt", "r");
fseek(file, 0L, SEEK_END);
long int res = ftell(file);
fseek(file, 0L, SEEK_SET);
int n[9999];
for (int i = 0; i < res; i ) {
fscanf(file, "%d,", &n[i]);
}
int size = sizeof(n)/ sizeof(n[0]);
for (int a = 0; a < size 1; a ) {
printf("%d ", n[a]);
}
}
It's working correctly but I want to create more dynamic array, txt file has a comma between each number so I added a piece of code that counts commas and tried to find the amount of numbers in the txt file. I made the code like this
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <ctype.h>
int main() {
FILE *file;
file = fopen("example_input.txt","r");
fseek(file, 0L, SEEK_END);
long int res = ftell(file);
fseek(file, 0L, SEEK_SET);
int m;
int counter = 0;
while((m = fgetc(file)) != EOF) {
if (m == 44) {
counter = 1;
}
}
int n[counter];
for (int i = 0; i < res; i ) {
fscanf(file, "%d,", &n[i]);
}
for (int a = 0; a < counter 1; a ) {
printf("%d ", n[a]);
}
}
After this change, the program started to generate outputs randomly. What is the reason of this?
Some of the output of the first code looks like this:
46 92 31 66 55 82 11 95 68 13 62 18 16 43 12 22 82 10 45 90 55 84 6 27 2 47 67 51 20 42 51 36 3 87 74 18 46 21 45 87 22 47 17 32 68 48 34 76 56 22 43 64 22 43 96 74 7 58 96 16 71 23 32 85 70 70 56 56 85 52 14 85 80 57 1 12 34 54 80 37 49 53 39 85 55 15 75 76 98 37 8 28 20 67 92 67 43 48 63 54 17 88 53 76 12 29 94 22 49 61 22 96 62 33 86 38 94 63 79 21 45 46 43 25 22 38 33 93 2 19 10 25 9 36 34 57 95 25 12 42 90 10 32 38 15 39 83 53 24 24 80 39 5 62 53 17 39 77 68 47 70 85 41 5 98 68 99 23 38 2 36 0 11 3 89 54 53 35 17 19 81 92 13 87 62 49 58 2 15 7 8 63 37 19 65 38 31 40 22 11 99 43 24 52 83 18 54 77 85 36 30 52 72 22 19 71 97 74 87 23 50 92 7 66 21 1 8 69 9 49 55 53 91 70 18 2 20 59 38 19 6 55 9 77 17 54 1 79 15 51 83 27 93 16 78 75 67 11 28 52 27 38 96 52 34 65 29 50 30 52 94 99 51 16 83 48 69 80 89 6 31 66 50 8 44 4 40 51 32 92 88 16 53 23 63 48 75 65 29 1 57 23 38 9 56 7 54 74 39 33 20 81 48 28 24 89 45 19 7 1 18 21 28 26 76 96 68 74 72 78 66 37 86 15 49 34
Second one like this:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 0 713594160 32767 -458707629 32653 0 0 155648 0 151736 0 151736 0 0 0 1 0 155648 0 1503232 0 1502284 0 1502284 0 155648 0 5 0 1503232 0 1810432 0 1808899 0 1808899 0 1503232 0 1 0 1814528 0 1839104 0 1836592 0 1852480 0 1810432 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1194 0 8192 0 -458634272 32653 0 0 0 0 -458838016 32653 -458694208 32653 0 0 -458559904 32653 32 0 -458836864 32653 10 0 0 0 0 0 0 0 713594160 32767 -458694993 32653 0 0 713594160 32767 -458838016 32653 -458707945 32653 0 0 0 0 0 3 -458841024 32653 3 4095 713593824 32767 3 0 1852480 0 0 0 713594376 32767 6 0 -458854400 32653 1017627840 22008 -458559904 32653 0 3 -458563360 32653 2049 0 399077 0 2049 0 399077 0 1 0 33261 0 0 0 0 0 1839168 0 4096 0 3600 0 1636126187 0 400000112 0 1632077219 0 0 0 1636037385 0 769124878 0 0 0 0 0 0 0 -458756030 32653 0 0 -458563104 32653 -458673324 32653 -458567616 32653 0 0 0 0 -458705939 32653 1 0 0 0 713594296 32767 0 0 1 0 0 0 -458559904 32653 0 0 -4585
CodePudding user response:
You need to seek to the beginning of the file after counting all the commas. You're at the end of the file when you start the fscanf()
loop, so they're all failing and you're not reading anything.
The loop that reads the numbers should only run counter
times. res
is the number of bytes in the file, which is at least twice as many numbers. There's no point in using res
.
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <ctype.h>
int main() {
FILE *file;
file = fopen("example_input.txt","r");
int m;
int counter = 0;
while((m = fgetc(file)) != EOF) {
if (m == ',') {
counter ;
}
}
counter ; // 1 more for the last number
int n[counter] = {0};
fseek(file, 0L, SEEK_SET);
for (int i = 0; i < counter; i ) {
if (fscanf(file, "%d,", &n[i]) != 1) {
break;
}
}
for (int a = 0; a < counter; a ) {
printf("%d ", n[a]);
}
}