我試圖逐行讀取文本文件並打印前17個字符。無法讀取文件中的字符串c
FILE *devices;
devices = NULL;
devices = fopen("devices.txt", "r");
char deviceaddr[17];
char addr[17];
char line[1024];
while (fgets(line,1024,devices) != NULL)
{
fscanf(devices,"%s", deviceaddr);
printf("%s\n", deviceaddr);
}
fclose(devices);
輸出應該是00:07:80:4C:0E:EE
的第一線,但它給6
。
你確定你的意思是C++嗎?這是純粹的C. – 2013-02-25 15:13:27
這比C++多得多。 C++將使用'std :: ifstream',使用'while(std :: getline)'讀取,並使用'.substr()'獲取前17個字符。 – chris 2013-02-25 15:13:34
使用固定大小的數組時,不要忘記尾部'\ 0'。 – 2013-02-25 15:13:56