我正在嘗試從 txt 檔案中獲取數字,我撰寫了如下代碼;
#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]);
}
}
它作業正常,但我想創建更多動態陣列,txt 檔案在每個數字之間有一個逗號,所以我添加了一段計算逗號的代碼,并試圖找到 txt 檔案中的數字數量。我制作了這樣的代碼
#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]);
}
}
在此更改之后,程式開始隨機生成輸出。這是什么原因?
第一個代碼的一些輸出如下所示:
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
第二個是這樣的:
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
uj5u.com熱心網友回復:
您需要在計算所有逗號后尋找檔案的開頭。當您開始fscanf()回圈時,您位于檔案的末尾,因此它們都失敗了,您沒有閱讀任何內容。
讀取數字的回圈應該只運行counter時間。res是檔案中的位元組數,至少是數字的兩倍。使用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]);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/350340.html
