如果你添加了一個輸入文件的例子,但你首先需要輸入正確的話會更容易。我建議你第一次嘗試這樣的事情:在名爲 「infile.txt」 看起來像這樣的輸入文件
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp;
char input[80];
int count;
int bin;
fp = fopen("infile.txt", "r");
while ((fscanf(fp,"%s %d", input, &count) != EOF)) {
bin = strtol(input, NULL, 2);
printf("input: %s, count %d, bin %d\n", input, count, bin);
}
fclose(fp);
return 0;
}
:
0011101 10
1001011 20
0101011 30
1001010 40
這將給以下的輸出:
input: 0011101, count 10, bin 29
input: 1001011, count 20, bin 75
input: 0101011, count 30, bin 43
input: 1001010, count 40, bin 74
所以,如果我不在正確的軌道上,讓我知道!
'我需要在讀取二進制輸入後執行位操作。 '我也是這樣。 – wildplasser 2014-10-11 23:40:21
你說文本文件中的第二列是一個字符串,但是你把它看作一個整數。另外,試圖從整個文本文件中讀取一個二進制數字並不會給你正確的值。 – 2014-10-11 23:44:13
另外,使用的變量類型應該匹配'fscanf'格式說明符。所以在你的情況下,使用「%s」和「unsigned int」變量會導致未定義的行爲 – SleuthEye 2014-10-11 23:44:44