我正在尝试使用 strstr() 在字符串中查找子字符串。仅使用 char[] 有效,char* 无效,导致分段错误。
所以这个正在工作:
int main() {
char str[] = "apple apple peach";
char *p;
p = strstr(str, "peach");
if (p!= NULL) {
strncpy(p, "apple", 5);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但这一个不起作用:
int main() {
char *str = "apple apple peach";
char *p;
p = strstr(str, "peach");
if (p!= NULL) {
strncpy(p, "apple", 5);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这个既不是:
int main() {
char *str = "apple apple peach";
char *peach = "peach";
char *p;
p = strstr(str, peach); …Run Code Online (Sandbox Code Playgroud) 我有以下字符串:
HTTP/1.1 200 OK
Date: Tue, 06 Dec 2011 11:53:22 GMT
Server: Apache/2.2.14 (Ubuntu)
X-Powered-By: PHP/5.3.2-1ubuntu4.9
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 48
Content-Type: text/html
??(?????I?O????H????????
?4?@??AQ??????t
Run Code Online (Sandbox Code Playgroud)
我想提取"\r\n\r\n",丢弃二进制数据.即:
HTTP/1.1 200 OK
Date: Tue, 06 Dec 2011 11:53:22 GMT
Server: Apache/2.2.14 (Ubuntu)
X-Powered-By: PHP/5.3.2-1ubuntu4.9
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 48
Content-Type: text/html
Run Code Online (Sandbox Code Playgroud)
如何在C中执行此操作?
这个警告意味着什么?我读了一些关于类似问题的其他帖子,但我仍然不理解如何解决它.
# define EOT_CHAR '\04'
char buffer[MAXLINE];
if ( strstr( buffer, EOT_CHAR ) != NULL )
{
break;
}
Run Code Online (Sandbox Code Playgroud) 我在我的本地运行PHP 5.3.8,在我的遥控器上运行5.2.17.
strstr()在我的本地工作正常,但在远程加载时它什么都不返回.甚至错误报告都没有说什么.
一个简单的用法:
echo strstr('1234567890', '3', true);
Run Code Online (Sandbox Code Playgroud)
应该返回12.而不是什么都没有.在这一点上,我迷失了想法.两个相同的脚本如何返回不同的结果?
是秘密关闭?或者有一个我不知道的设置?任何帮助非常感谢,谢谢.
我正在尝试UART使用STM32F407V6T6和CubeMx.
因为我已经发布了一些strstr()功能问题.这是一些新问题.
这是代码:
char rxBuff[10];
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
Green_Blink(100);
Orange_Blink(100);
Blue_Blink(100);
Red_Blink(100);
while (1)
{
HAL_UART_Transmit_IT(&huart2,(uint8_t*)"hello\r\n",10);
HAL_Delay(1000);
Orange_Blink(100);
if (strstr(rxBuff,"hi"))
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
这里我正在做的是我正在hello\r\n使用发送中断进行传输.发射后,我检查了像某些值hi在rxBuff[10].
在这个特定的代码中,我没有收到任何我刚创建缓冲区并检查它是否包含的内容hi.
请注意,在传输时我只发送7个字节的数据.但是在HAL_UART_Transmit_IT()大小的地方,我给了10.
HAL_UART_Transmit_IT(&huart2,(uint8_t*)"hello\r\n",10);
将此代码刷新到我的stm32后,我可以在终端中看到各种输出
你好\ r\NHI
如果我将txt hi改为其他类似行星的东西,那么输出就像
你好\ r\NPL
但是当我给出这样的完美尺寸时
HAL_UART_Transmit_IT(&huart2,(uint8_t*)"hello\r\n",7);
我得到了应该得到的实际输出
你好\ r \n
通过UART传输数据并查找某个缓冲区中是否存在子串是完全不同的任务.
有什么建议为什么会发生这种情况?
PS:基本上我的项目是GSM MODULE SIM800c和STM32f407VGt6我想从stm32发送各种AT命令并检查响应,以便我可以相应地工作.
以前我用AVR系列mcus尝试过这个项目,我可以发送短信,接收特定的短信,使用post和get方法向服务器发送数据,一切都运行得很好.
转移到ARM后,我没有得到STM32的表现.