我有分裂字符串的问题.下面的代码有效,但只有在字符串之间是''(空格).但即使有任何空白字符,我也需要拆分字符串.是strtok()甚至是必要的?
char input[1024];
char *string[3];
int i=0;
fgets(input,1024,stdin)!='\0') //get input
{
string[0]=strtok(input," "); //parce first string
while(string[i]!=NULL) //parce others
{
printf("string [%d]=%s\n",i,string[i]);
i++;
string[i]=strtok(NULL," ");
}
Run Code Online (Sandbox Code Playgroud) 我在读取未知大小的标准输入时遇到问题。事实上,它是 .txt 文件中的一个表,我通过调用参数“<”table.txt 到达标准输入。我的代码应该是这样的:
#include <stdio.h>
#include <string.h>
int main(int argc,char *argv[])
{
char words[10][1024];
int i=0;
while(feof(stdin)==0)
{
fgets(words[i],100,stdin);
printf("%s", words[i]);
i++;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是有一个问题,我不知道行数,在这种情况下是 10(我们知道行中的字符数 - 1024)。如果有人知道解决方案,那就太好了。提前致谢。
我需要比较存储在两个变量中的两个日期,格式为YYYY-MM-DD. 我想我可以将它们存储在另一个变量中并使用一些tr -d "-",然后像整数一样进行比较,但我不知道如何。提前致谢 !
我正在编写一个从标准输入返回一个字符串的程序,但我收到警告,它返回一个本地 wariable 的地址。我怎样才能返回字符串?
提前致谢
#include <stdio.h>
char* readLine()
{
int i;
char input[1024];
for(i=0;i<1024;i++)
{
input[i]=fgetc(stdin);
if(input[i]=='\n')
{
break;
}
}
return input;
}
int main()
{
printf("%s",readLine());
return 0;
}
Run Code Online (Sandbox Code Playgroud) 所以我想创建触发器:
CREATE TRIGGER add_primary_key
AFTER INSERT ON CAJ
FOR EACH ROW
WHEN (ID_CAJ IS NULL)
DECLARE
maximum NUMBER;
BEGIN
maximum := SELECT MAX(ID_CAJ) FROM caj;
if (maximum <> 0)
UPDATE caj
SET ID_CAJ = maximum + 1;
WHERE CAJ_ID = NULL
maximum = maximum +1;
END;
Run Code Online (Sandbox Code Playgroud)
出现此错误:
"invalid NEW or OLD specification"
*Cause: An invalid NEW or OLD specification was given for a column.
*Action: Re-specify the column using the correct NEW or OLD specification.
Run Code Online (Sandbox Code Playgroud)
你们有什么想法吗?
我有一个简单的函数,每次我想添加一个字符时都会重新分配缓冲区,它一直工作到我想第 24 次重新分配。然后 realloc(): invalid next size 出现。这是代码:
char * addDataChunk(char * data,char c)
{
char * p;
if(data==NULL)
{
data=(char*)malloc(sizeof(char)*2);
data[0]=c;
data[1]='\0';
return data;
}
else
{
if(p = (char*)realloc(data,((strlen(data)+1)*sizeof(char))))
{
data = p;
}
else
{
printf("realloc error\n");
}
data[strlen(data)] = c;
data[strlen(data)+1] = '\0';
return data;
}
}
Run Code Online (Sandbox Code Playgroud)
和错误:
*** Error in `./bootstrap': realloc(): invalid next size: 0x0000000000b9f2a0 ***
Aborted (core dumped)
Run Code Online (Sandbox Code Playgroud)