我遇到了一个问题,在使用 getline 时,我无法在带有 strcmp() 的文件中找到特定单词。
我的文件看起来像这样:
Word1
Word2
Word3
section1
Word4
Run Code Online (Sandbox Code Playgroud)
这是我现在拥有的代码:
while(found == 0)
{
getline(&input, &len, *stream);
if (feof(*stream))
break;
if(strcmp(input, "section1\n") == 0)
{
printf("End of section\n");
found = 1;
}
}
Run Code Online (Sandbox Code Playgroud)
但是 strcmp() 从不返回 0。任何见解将不胜感激!
对代码进行了编辑。我错误地转移了它。
评论中的解决方案:我需要将 \r 添加到正在比较的字符串中
if(strcmp(input, "section1\r\n") == 0)
Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
#include <string.h>
#include <conio.h>
#define SIZE 20
int main( void )
{
int n; //number of characters to be compared
char s1[ SIZE ], s2[ SIZE ];
char *results_word;
printf( "Enter two strings: " );
gets( s1 );
gets( s2 );
printf( "\nEnter the number of characters to be compared: " );
scanf( "%d", &n );
Run Code Online (Sandbox Code Playgroud)
问题从这里开始
results_word =
strncmp( s1, s2, n ) > 0 ? " greater than " :
strncmp( s1, s2, n ) == …Run Code Online (Sandbox Code Playgroud) 我想知道是否可以用空字符串初始化字符串,如下所示:
#include <stdio.h>
#include <string.h>
char* some_function() {
char* w = malloc(100*sizeof(char));
w = "";
if (1 == 2) {
w = "Not empty anymore";
}
return w;
}
int main(void) {
char* word = some_function();
int r = strcmp("", word);
printf("%s\n", word);
printf("%d\n", r);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它编译得很好,并给出了我想要的结果,但是我对 C 仍然很陌生,想知道这是否会导致任何问题。我还可以使用 strcmp 将单词与“”进行比较吗?
我现在正在调整,basename我遇到了一个非常奇怪的案例(至少对我而言)。这是代码:
char buffer[300];
char* p;
strcpy(buffer, "../src/test/resources/constraints_0020_000");
printf("%d\n", strcmp(basename("../src/test/resources/constraints_0020_000"), "constraints_0020_000")); //works as expected
printf("assert testBasename02");
printf("%d\n", strcmp(basename(buffer), "constraints_0020_000") == 0);
printf("done 1\n"); //goes in segmentation fault
printf("%d\n", strcmp(basename(&buffer), "constraints_0020_000") == 0);
printf("done 2\n"); //goes in segmentation fault
printf("%d\n", strcmp(basename(&buffer[0]), "constraints_0020_000") == 0);
printf("done 3\n"); //goes in segmentation fault
p = malloc(strlen("../src/test/resources/constraints_0020_000") +1);
strcpy(p, "../src/test/resources/constraints_0020_000");
printf("%d\n", strcmp(basename(p), "constraints_0020_000") == 0); //works as expected
free(p);
printf("all done\n");
Run Code Online (Sandbox Code Playgroud)
第一个strcmp完全例外;这是第二个让我困惑的问题:为什么缓冲区会出现分段错误?我试图以不同的方式对缓冲区进行编码,但结果是一样的。
我当然可以忍受这种行为,但是......我真的不明白basename如果我给他喂食 aconst char*或缓冲区(最后也是 a char*)有什么区别。 …
为了提高我的二进制开发技能,并加深我在低级环境中的理解,我尝试解决 中的挑战pwnable.kr,第一个挑战-称为fd具有以下 C 代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char buf[32];
int main(int argc, char* argv[], char* envp[]){
if(argc<2){
printf("pass argv[1] a number\n");
return 0;
}
int fd = atoi( argv[1] ) - 0x1234;
int len = 0;
len = read(fd, buf, 32);
if(!strcmp("LETMEWIN\n", buf)){
printf("good job :)\n");
system("/bin/cat flag");
exit(0);
}
printf("learn about Linux file IO\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我objdump -S -g ./fd为了反汇编它而使用它,我感到困惑,因为不是调用 strcmp 函数。它只是比较字符串而不调用它。这是我正在谈论的汇编代码:
80484c6: e8 05 ff ff ff call …Run Code Online (Sandbox Code Playgroud) 我有一个打印调试日志的功能,必须根据环境变量进行切换。print_trace()存储它并重用该值的最佳方法应该是什么,而不是每次调用时检查环境变量?
void print_trace(const char* msg)
{
const char* s = getenv("DEBUG_TRACE");
if(!strcmp(s,"ON"))
printf(msg);
}
Run Code Online (Sandbox Code Playgroud)
没有,main()因为这是一个共享库。
假设没有传递小于 4 个字节的字符串,那么这种优化有什么问题吗?是的,在比较大多数不相似的字符串时,在我测试过的机器上,这是一个显着的加速。
#define STRCMP(a, b) ( (*(int32_t*)a) == (*(int32_t*)b) && strcmp(a, b) == 0)
Run Code Online (Sandbox Code Playgroud)
假设字符串不少于 4 个字节,是否有一种更快的方法可以在不诉诸汇编等的情况下做到这一点?
我正在尝试构建一个在以太网级别上工作的简单echo服务器/客户端(使用原始套接字).服务器端本身工作并显示eth0上的所有传入数据包.客户端工作并在eth0上发送以太网数据包(我用wireshark检查了这个数据包,可以看到数据包输出.)我现在想要制作一个过滤器,只查看我感兴趣的数据包.(这基于目的地/源地址.)
在下面的代码,可能有人请向我解释为什么STRNCMP返回零(指字符串匹配),但还没有了"如果(ethernet_header-> h_dest == MAC)"执行失败(不匹配).变量"mac"和"ethernet_header-> h_dest"都是相同的类型和长度.
一些更多的背景: - 这是Linux 64位完成(Ubuntu的) - 我使用的是同一台机器上发送/接收的eth0 ....我不认为这应该是一个问题吗?
我只是不明白为什么strcmp返回一个匹配,如果没有.我错过了什么?
void ParseEthernetHeader(unsigned char *packet, int len) {
struct ethhdr *ethernet_header;
unsigned char mac[ETH_ALEN] = {0x01, 0x55, 0x56, 0x88, 0x32, 0x7c};
if (len > sizeof(struct ethhdr)) {
ethernet_header = (struct ethhdr *) packet;
int result = strncmp(ethernet_header->h_dest, mac, ETH_ALEN);
printf("Result: %d\n", result);
if(ethernet_header->h_dest == mac) {
/* First set of 6 bytes are Destination MAC */
PrintInHex("Destination MAC: ", ethernet_header->h_dest, 6);
printf("\n");
/* Second set of 6 …Run Code Online (Sandbox Code Playgroud) 我正在开发一个小项目,我无法访问任何C标准库.(从头开始构建一个ARM结构的微内核.甚至必须实现printf)
在这种情况下,我使用Duff的机器方法实现了strcmp.
以下是整个代码.
int
strcmp ( const char *str1, const char *str2 )
{
while ( *str1 || *str2 )
if ( *(str1++) != *(str2++) ) return *str1 - *str2;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是有道理的; 有些时候它似乎适用于测试用例,直到最终系统发生故障.我追查到了这个strcmp.
起初我以为它首先递增str1然后比较str2 BEFORE str2递增.事实证明不是,但有人可以确认在某些情况下会发生这种情况吗?
然后我发现问题出现在*str1 - *str2中,所以将其更改为返回1.即,结果代码如下:
while ( *str1 || *str2 )
if ( *(str1++) != *(str2++) ) return 1;
return 0;
Run Code Online (Sandbox Code Playgroud)
虽然我想要的只是一个'等于'检查,所以改成'1'没有问题,但我仍然想知道为什么原始代码失败了.2.有人可以提出关于它如何失败的建议吗?我宁愿希望strcmp遵循标准的C接口,它返回一个非零值,告诉更多关于str1和str2的信息.
测试用例是:
code_t // a function pointer type
program_find ( char *program )
{
if (strcmp( program, "exit" ) == …Run Code Online (Sandbox Code Playgroud) 可能重复:
如何正确比较C中的字符串?
#include <iostream>
using namespace std;
int main(){
char name[100];
cout<<"Enter: ";
cin>>name;
if(name == "hello"){
cout<<"Yes it works!";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么当我在提示中输入你好时我没有得到"是的,它有效!" 信息?