Tag: string

怎么来fflush(stdin)function不起作用?

我的主要问题是为什么fflush(stdin); function不起作用? 每当我运行代码时,我都无法使用空格ex获得第二个输入。 你好世界,但我得到你好? 谢谢 #include main(){ int x; double y; char string[100]; /* * string input */ printf(“Enter one word: “); scanf(“%s”, string); // note there is no & before string */ printf(“The word you entered was >>%s<<\n"); printf("Enter many words: "); fflush(stdin); // >%s<<\n"); getchar(); }

与if语句中的字符串比较不起作用

我尝试比较从scanf和fscanf获得的两个字符串。 我已经弄明白每个变量里面的内容是什么。 它都显示相同的字符串,但在if语句中与这两个字符串进行比较后,它不起作用并执行else语句。 我的代码出了什么问题? int main(void) { … char input[256]; printf(“Enter your name: “); scanf(“%s”,&input); fp = fopen(_FILE,”r+”); for(i=0;i<textlines(_FILE);i++) { fscanf(fp,"%s %d",stuff[i].name,&stuff[i].salary); if(input == stuff[i].name) { // Update name here } else { printf("Not Found"); } } return 0; }

如何检查C字符串中是否存在单个字符?

我想检查单个字符串是否在C字符串中。 字符是’|’ 用于Linux中的管道(实际上,我也想检查'<' , ‘>’ , ‘>>’ , ‘&’ )。 在Java中我可以这样做: String.indexOf() 但是如何在C中执行此操作,而不循环遍历整个字符串( char*字符串)?

在简单的C代码中进行分段错误(核心转储)

我是C的新人。我指的是Brian W Kernighian和Dennis Ritchie所着的“C编程语言”一书。 本书中给出了一个指针增量和赋值的代码,如下所示。 #include int main() { char *s = “Goal”; char *t = “Home”; while(*s++ = *t++) printf(*s); return 0; } 使用该命令保存和编译代码 gcc ptr.c -o ptr -std=c99 现在通过运行命令运行代码 ./ptr 我收到以下错误 分段故障(核心转储) 错误似乎在while循环条件中。 但是代码与本书中给出的完全相同。 我错过了什么?

使用iconv进行简单的UTF8-> UTF16字符串转换

我想写一个函数将UTF8字符串转换为UTF16(little-endian)。 问题是, iconv函数似乎不会让您事先知道存储输出字符串需要多少字节。 我的解决方案是首先分配2*strlen(utf8) ,然后在循环中运行iconv ,必要时使用realloc增加该缓冲区的大小: static int utf8_to_utf16le(char *utf8, char **utf16, int *utf16_len) { iconv_t cd; char *inbuf, *outbuf; size_t inbytesleft, outbytesleft, nchars, utf16_buf_len; cd = iconv_open(“UTF16LE”, “UTF8”); if (cd == (iconv_t)-1) { printf(“!%s: iconv_open failed: %d\n”, __func__, errno); return -1; } inbytesleft = strlen(utf8); if (inbytesleft == 0) { printf(“!%s: empty string\n”, __func__); […]

如何通过终端读取段落作为C中的单个字符串?

我正在编写一个C程序,应该在用户的文章中阅读。 论文分为多段。 我不知道文章会有多少行或字符,但我知道它以哈希符号( # )结尾。 我想只使用尽可能多的内存来保存文章。 这是我到目前为止所尝试的: #include #include #include main(){ int size; char *essay; printf(“\n how many characters?\n”); scanf(“%d”, &size); essay =(char *) malloc(size+1); printf(“Type the string\n”); scanf(“%s”,essay); printf(“%s”,essay ); } 正如我之前所说,我事先并不知道(也不想问)关于字符的数量。 如何动态分配内存以节省空间? (什么是动态内存分配?)是否存在另一种节省不依赖动态分配的内存的方法? 另外,我的代码现在一次只能读取一行。 如何读取多行并将它们存储为单个字符串? 这是另一个代码 #include #include int main () { char input; int count = 0; int n; char* characters= NULL; […]

ORA-01795:列表中的最大表达式数为1000,如何拆分字符串

如何拆分以下字符串? Ax IN (changeList), changeList具有以下值,因此它使得子句如下: Ax IN (HEXTORAW(‘1E2956B9266F11DDA85810000000C959’),HEXTORAW (‘ADD834AD6A3911DF923C10000000C959’),HEX……….. 上面的IN有超过1000个值,因此ORA-01795错误,如何修改它以便我拥有 AX IN(id1,.. id999)或Ax IN(id1000,…),任何c代码都可以帮助我……

printf with%s包含空字符

我需要连接一些字符串,我需要包含NULL字节。 我不想将’\ 0’视为终止字节。 我想保存我宝贵的NULL字节! 在代码示例中,如果 char *a = “\0hey\0\0”; 我需要以输出“\ 0hey \ 0 \ 0”的格式printf。 -AUstin

在C中将字符串转换为long long

我无法通过环礁函数在c中正确设置长long值。 这是我的例子: #include int main(void) { char s[30] = { “115” }; long long t = atoll(s); printf(“Value is: %lld\n”, t); return 0; } 打印:值为:0 这有效: printf(“Value is: %lld\n”, atoll(s)); 这里发生了什么?

函数’strtok_r’的隐式声明尽管包括

我有以下代码来标记包含由\n分隔的行的字符串,每行包含由\t分隔的整数: void string_to_int_array(char file_contents[BUFFER_SIZE << 5], int array[200][51]) { char *saveptr1, *saveptr2; char *str1, *str2; char delimiter1[2] = "\n"; char delimiter2[2] = " "; char line[200]; char integer[200]; int j; for(j = 1, str1 = file_contents; ; j++, str1 = NULL) { line = strtok_r(str1, delimiter1, &saveptr1); if (line == NULL) { break; } for […]