Questions tagged [strncmp]

The strncmp function is a string comparison function found in languages like C and C++. In contrast with strcmp, the strncmp function takes a third argument denoting the maximum length of the strings. This is used to prevent out of bounds errors.

85 questions
0
votes
2 answers

Determine which char * is the shortest without strlen

I have two different char *, char *string1 which is constant and char *string2 which can change. I retrieve char *string2 from a list. I want to find the length of the shortest char * to use it in: strncmp(string1, string2, shortest);. This will…
ZeppRock
  • 988
  • 1
  • 10
  • 22
0
votes
2 answers

Delete specific rows by imposing a condition

I have a structure consisting of a Names column and a Data column. I need to delete a series of row by imposing the condition with respect to a specific name. I used this code in another exercise and it seemed to be fine, but I guess it's not…
Antonio
  • 186
  • 2
  • 12
0
votes
3 answers

Comparing N bytes in string

I want to compare argv[1] and the string "pid", but I want to put restrict how much bytes to be compared. I used strncmp, but there are problem, if I put on third argument 3 it compares more than 3 bytes. #include #include…
Itra
  • 45
  • 10
0
votes
2 answers

Comparing int8_t and char* using strcmp/strncmp

Currently trying to fix code that was left unused for a while. I have two variables: int8_t foo[size] and const char* const bar. There is an if that checks if(0 != strcmp((char *)foo, bar)) Currently this is failing even though printf("%s | %s",…
Shox2711
  • 139
  • 1
  • 12
0
votes
1 answer

delete rows with character in cell array

I need some basic help. I have a cell array: TITLE 13122423 NAME Bob PROVIDER James and many more rows with text... 234 456 234 345 324 346 234 345 344 454 462 435 and many MANY (>4000) more with only numbers text text and more text…
user6110593
0
votes
1 answer

read lines from file with fgets and compare each line with strncmp in c

i want to read every line from a file which looks something like this: readEveryLine { "Bart [m]" -> "Marge [f]"; "Lisa [f]" -> "Homer [m]"; ... } i want to use: fgets() to read the file line by line strncmp() to compare…
MBD
  • 95
  • 3
  • 10
0
votes
1 answer

c find exact word from string starting with "

i am doing an exercise in C for my C programming course. I have to read data from a text file into a linked list and look for matches, then print the result out. Example of the text…
Cyrus Leung
  • 106
  • 5
0
votes
1 answer

Compare md5 after tftp transfer

Because I use the tftp command to transfer an important file, I would like to compare md5 in order to valid the transfer. Note : The file has been already transfered in the the example below int main(int argc, char *argv[]) { FILE…
ogs
  • 1,139
  • 8
  • 19
  • 42
0
votes
2 answers

Am I using strncmp and fgets in the right way?

I'm a beginner programmer trying to learn C. Currently I'm taking a class and had a project assigned which I managed to finish pretty quickly, at least the main part of it. I had some trouble coding around the main() if functions though, because I…
0
votes
2 answers

Obtain lines of a file beginning with a prefix

I am trying to detect which lines of my plaintext start with "linePrefix". Using the code above, even when there is a coincidence, strcmp never returns 0. Does anyone know where am I failing? const char PREFIX[] = {"linePrefix"}; FILE *fp; …
qwerty
  • 486
  • 1
  • 11
  • 37
0
votes
1 answer

What will happen we pass -1 as value for 3rd parameter in strncmp() ie. n number bytes of bytes to compare?

I have three questions, What will happen we pass -1 as value for 3rd parameter in strncmp() ie. n number bytes of bytes to compare? Why the output differs in ARM and PPC? ie. if which one correct? If I use memcmp instead of strncmp, I m getting…
Harini
  • 13
  • 1
0
votes
1 answer

request for member in something not a structure or union, but it's a struct

this code is a binary search tree I'm doing for my school assignment. #include "binary_tree.h" #include #include #include #define MAXLEN 10 Node* create_tree() { return NULL; } void insert_tree(Node** root, char*…
JinMin Lee
  • 13
  • 3
0
votes
2 answers

Is there an idiomatic use of strncmp()?

The strncmp() function really only has one use case (for lexicographical ordering): One of the strings has a known length,† the other string is known to be NUL terminated. (As a bonus, the string with known length need not be NUL terminated at…
jxh
  • 69,070
  • 8
  • 110
  • 193
0
votes
1 answer

strncmp in two statements takes more time than in single statement

For doing a file sorting in my code i used strncmp for the comparison of first elements of each string. Including my code here : Code 1: for (i = 1; i < file_cnt; ) { if ((strncmp(info[i-1].name, "1", 1) == 0) && (strncmp(info[i].name, "2",…
user-ag
  • 224
  • 3
  • 20
0
votes
2 answers

strcpy / strncmp segmentation fault in struct fileds

I'm trying to add a new node to a struct with a char* field (word) Definition of listT: enum boolean {false, true}; struct list { enum boolean sorted; union{ int words; char *word; }; struct list* next; struct…