我正在讀取每行有十六進制值的文件。它看起來像這樣:如何將特定的字符串與C中數組的所有元素進行比較?
F0BA3240C
083FA52
45D3687AF
etc.
十六進制值不會有相同的長度。
我把fgets從這個文件讀入一個緩衝區,然後是一段代碼來擺脫換行符。從那裏我把緩衝區中的字符串放入我的數據數組中。但在將字符串從緩衝區放入我的數據數組之前,我試圖比較緩衝區中的字符串和已存儲在數據數組中的字符串,看看是否存在匹配,以便更新某些計數器。但是,我遇到了使用strcmp和strncmp的問題。任何幫助將不勝感激,謝謝。
相關代碼:
char **data = NULL;
char data_buffer[100];
//program first goes through the file and determines amount of lines there are hence this variable
int count_line = 0;
...
data = malloc(count_line * sizeof(char *));
int f;
int i;
for(i=0; i<count_line; i++)
{
fgets(data_buffer, sizeof(data_buffer), fp);
...
//allocate space to store copy of line and add one for null terminator
data[i] = malloc(line_length + 1);
...
if(asdf != NULL)
{
//problem here. don't know how to compare stream from buffer and compare to all elements of data buffer
for(f=0; f<sizeof(data); f++)
{
if(strcmp(data[f], data_buffer) == 0)
there_was_a_match++;
}
}
...
//copy string from buffer into data array
strcpy(data[i], data_buffer);
}
非常有幫助,感謝您的解釋。 – Kevin 2014-09-23 20:59:37