问题描述
我有一个文本文件,它的值(我通常称其为upc_values)
01080006210
69685932764
40000114485
40000114724
07410855329
72908100004
66484101000
04000049163
43701256600
99999909001
07726009493
78732510053
78732510063
78732510073
78732510093
02842010109
02842010132
78732510213
02410011035
73999911110
字符* UPC_val = 99999909001;
char upcbuf [100] [12];
char buf [12];
memset(buf,0,sizeof(buf));
memset(upcbuf,0,sizeof(upcbuf));
当我尝试获取fgets时,我将其存储在2D缓冲区中。
而(fgets(buf,sizeof(buf),f)!= NULL){
strncpy(upcbuf [i],buf,11) ;
i ++;
}
我试图在缓冲区中打印数据。
puts(upcbuf [0]);
upcbuf [0]具有连续流中的整个数据,
0108000621069685932764400001144854000011472407410855329729081000046648410100004000049163437012566009999990900107726009493787325100537873251006378732510073787325100930284201010902842010132787325102130241001103573999911110
b与[b] c的位数相加(11)与数字(11)比较(b)和(b)b与b(c)的数字(b)我用过,
if(strncmp(UPC_Val,upcbuf [i],11)== 0)
{
//在这里做东西
}
它没有正常工作,我使用过的strstr()也是如此,
if(strstr(upcbuf [0],UPC_val)!= NULL)
{
//在这里做东西
}
我完全不知道它在做,我在正确地进行比较吗?
如何执行此操作,请提供任何帮助?
预先感谢。
要读取一行11位数字和'\n'
放入 string 至少需要13个数组来存储 string 。没有什么理由要那么紧。建议使用2倍的预期最大大小
char upcbuf [100] [12]; //足够容纳100 *(11个数字和\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’;
检查长度并以某种方式处理。
如果(len!= 11)退出(EXIT_FAILURE);
保存/打印数据
// strncpy(upcbuf [i],buf,11); //无法确保
结尾处为空字符strcpy(upcbuf [i],buf);
i ++;
puts(upcbuf [i]);
比较字符串
if(strcmp(UPC_Val,upcbuf [i])== 0){
//字符串匹配
}
I have a text file, it has values(I usually call them as upc_values) of
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));
When I tried to fgets, I stored that in a 2D buffer.
while ( fgets(buf, sizeof(buf), f) != NULL ) {
strncpy(upcbuf[i], buf, 11);
i++;
}
I tried to print the data in the buffer.
puts(upcbuf[0]);
upcbuf[0] has the whole data in a continues stream,
0108000621069685932764400001144854000011472407410855329729081000046648410100004000049163437012566009999990900107726009493787325100537873251006378732510073787325100930284201010902842010132787325102130241001103573999911110
and I want to compare this upc values(11 digit) with another string(11 digit). I used,
if(strncmp(UPC_Val,upcbuf[i],11) == 0)
{
//do stuff here
}
It didn't work properly, I used strstr() too like,
if(strstr(upcbuf[0],UPC_val) != NULL)
{
//do stuff here
}
I am totally unaware of what it is doing, am I doing the comparison properly?How to do this, any help please?
Thanks in advance.
To read a line of text of 11 digits and a '\n'
into a string needs an array of at least 13 to store the string. There is little reason to be so tight. Suggest 2x expected max size
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 ) {
Lop off the potential tailing '\n
'
size_t len = strlen(buf);
if (len && buf[len-1] == '\n') buf[--len] = '\0';
Check length and handle that somehow.
if (len != 11) exit(EXIT_FAILURE);
Save/print the data
// strncpy(upcbuf[i], buf, 11); // fails to insure a null character at the end
strcpy(upcbuf[i], buf);
i++;
puts(upcbuf[i]);
To compare strings
if(strcmp(UPC_Val,upcbuf[i]) == 0) {
// strings match
}
这篇关于如何从文件中获取字符串并将其存储在2D char数组中,然后将该2D char数组与C中的字符串进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!