[英]How to separate binary strings from text file and store them in 1d or 2d char array?
[英]How to get the strings from a file and store in a 2D char array and compare that 2D char array with a string in C?
我有一個文本文件,它的值(我通常稱其為upc_values)為
01080006210
69685932764
40000114485
40000114724
07410855329
72908100004
66484101000
04000049163
43701256600
99999909001
07726009493
78732510053
78732510063
78732510073
78732510093
02842010109
02842010132
78732510213
02410011035
73999911110
char *UPC_val = "99999909001";
char upcbuf[100][12];
char buf[12];
memset(buf,0,sizeof(buf));
memset(upcbuf,0,sizeof(upcbuf));
當我嘗試獲取fget時,我將其存儲在2D緩沖區中。
while ( fgets(buf, sizeof(buf), f) != NULL ) {
strncpy(upcbuf[i], buf, 11);
i++;
}
我試圖在緩沖區中打印數據。
puts(upcbuf[0]);
upcbuf [0]在連續流中具有整個數據,
0108000621069685932764400001144854000011472407410855329729081000046648410100004000049163437012566009999990900107726009493787325100537873251006378732510073787325100930284201010902842010132787325102130241001103573999911110
我想將此upc值(11位)與另一個字符串(11位)進行比較。 我用了,
if(strncmp(UPC_Val,upcbuf[i],11) == 0)
{
//do stuff here
}
它工作不正常,我也使用strstr(),
if(strstr(upcbuf[0],UPC_val) != NULL)
{
//do stuff here
}
我完全不知道它在做什么,我是否在正確進行比較? 怎么做,有什么幫助嗎?
提前致謝。
要將11位數字的文本行和'\\n'
讀入字符串中,需要至少13個數組來存儲該字符串 。 沒有什么理由要那么緊。 建議兩倍最大尺寸
char upcbuf[100][12]; // large enough for 100 * (11 digits and a \0)
...
#define BUF_SIZE (13*2)
char buf[BUF_SIZE];
while (i < 100 && fgets(buf, sizeof buf, f) != NULL ) {
挖掉潛在的尾巴'\\n
'
size_t len = strlen(buf);
if (len && buf[len-1] == '\n') buf[--len] = '\0';
檢查長度並以某種方式處理。
if (len != 11) exit(EXIT_FAILURE);
保存/打印數據
// strncpy(upcbuf[i], buf, 11); // fails to insure a null character at the end
strcpy(upcbuf[i], buf);
i++;
puts(upcbuf[i]);
比較字符串
if(strcmp(UPC_Val,upcbuf[i]) == 0) {
// strings match
}
如果您仍然無法在@chux的答案之后使邏輯正常工作,那么以下是一個簡短的示例,該示例實現了他的建議:將文件名讀取為第一個參數,可選地將upc搜索為第二個參數(它將默認情況下,搜索"99999909001"
(在這種情況下,您只需在stdin
上讀取文件即可))。
注意使用enum
為行和列值定義全局常量。 (如果願意,可以使用獨立的#define ROW 128
和#define COL 32
)如果需要在代碼中定義常量,請在頂部對其進行一次定義,因此,如果需要更改,則可以在一個方便的位置進行更改值,而不必仔細檢查代碼或執行全局搜索/替換以更改它們。
例如,您可以將邏輯匯總如下:
#include <stdio.h>
#include <string.h>
enum { COL = 32, ROW = 128 }; /* an enum is convenient for constants */
int main (int argc, char **argv) {
char buf[COL] = "", /* buffer to read each line */
upcbuf[ROW][COL] = { "" }, /* 2D array of ROW x COL chars */
*upcval = argc > 2 ? argv[2] : "99999909001";
size_t n = 0; /* index/counter */
FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin; /* file */
if (!fp) { /* validate file open for reading */
fprintf (stderr, "error: file open failed '%s'.\n", argv[1]);
return 1;
}
/* fill upcbuf (you could search at same time, but let's fill) */
while (n < ROW && fgets (buf, COL, fp)) {
size_t len = strlen (buf); /* get length */
/* test last char '\n', overwrite w/nul-terminating char */
if (len && buf[len - 1] == '\n')
buf[--len] = 0;
strcpy (upcbuf[n++], buf); /* copy to upcbuf */
}
if (fp != stdin) fclose (fp); /* close file if not stdin */
/* step through upcbuf - search for upcval */
for (size_t i = 0; i < n; i++)
if (strcmp (upcbuf[i], upcval) == 0) {
printf ("upcval: '%s' found at line '%zu'.\n", upcval, i + 1);
return 0;
}
printf ("upcval: '%s' not found in file.\n", upcval);
return 0;
}
使用/輸出示例
$ ./bin/upcbuf dat/upcfile.txt
upcval: '99999909001' found at line '10'.
$ ./bin/upcbuf dat/upcfile.txt 01080006210
upcval: '01080006210' found at line '1'.
$ ./bin/upcbuf dat/upcfile.txt 02410011035
upcval: '02410011035' found at line '19'.
$ ./bin/upcbuf dat/upcfile.txt "not there!"
upcval: 'not there!' not found in file.
還要注意,如果您只是在搜索單個upc,則可以在一個循環中組合讀取和搜索,但是由於您通常將讀取作為一個單獨的函數,然后對代碼中其他位置的數據進行操作,因此本示例僅讀取了將文件中的所有upc值放入您的數組,然后在單獨的循環中搜索數組。 查看所有內容,查看所有答案,如果還有其他問題,請告訴我們。
最后一點,您已經檢查了最后一個字符是否為'\\n'
,但是如果不是,該怎么辦? 您應該檢查長度是否為COL-1
指示該行中的其他字符仍未讀取並處理錯誤(或僅讀取並丟棄其余字符)。 您可以通過添加類似於以下內容的方法來做到這一點:
/* test last char '\n', overwrite w/nul-terminating char */
if (len && buf[len - 1] == '\n')
buf[--len] = 0;
else if (len == COL - 1) { /* if no '\n' & len == COL - 1 */
fprintf (stderr, "error: line excces %d chars.\n", COL - 1);
return 1;
}
並且,您需要使用else if
並檢查COL - 1
而不是僅在此處使用else
,因為在此之后,您可能正在從沒有POSIX行尾 (例如換行符)的文件中進行讀取文件的最后一行。 即使沒有POSIX行結尾, fgets
也會正確讀取最后一行,但是buf
不會有'\\n'
。 因此,即使沒有POSIX行結尾,該行也可以是有效行,並且只要讀取的字符數(+以nul終止的 char)不等於緩沖區大小,就可以保證讀取完整。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.