This question already has an answer here:
c sscanf with character delimiter
(1 answer)
Closed 2 years ago.
I have a string of the form
"http://something.another.thing:13541/random-text.txt"
I want to extract the "something.another.thing:13541" part out of it, so I am using
sscanf(uri, "http://%s/%*s", host);
To read the specified part into a buffer host. %*s should be ignoring the parts of the string after / right?
The problem is not discarding parts, you need a delimiter [^?]
Try
char *uri = "http://something.another.thing:13541/random-text.txt";
char host[266];
sscanf(uri, "http://%265[^/]", host); // %265 prevents buffer overflows
printf("%s\n", host);
Since a host can not contain more than 255 bytes + say 10 for the port, char host[266]; is what you want to cover all possible cases.
Another option is to get the maximum size with getconf HOST_NAME_MAX and pass it to the program if you are under some linux.
Related
This question already has answers here:
Getting wrong string length
(3 answers)
Closed 4 years ago.
I have this piece of code:
char* input = malloc(sizeof(char)*100);
scanf("%s", input); //let's say that the number of chars in "%s" is 5
How do I calculate how many chars I typed in (5)? I tried by playing around with sizeof(), but couldn't find a solution.
Edit (better explanation): the input variable can host up to 100 chars, but let's say I type in the terminal 'abcde': then it hosts only 5 chars, the other 95 are not taken. I want to calculate that '5'.
You have to find the null terminator.
int i = 0;
while(input[i] != 0) {
++i;
}
//i marks the spot
But yeah, strlen() does a better job, since it has some improved/optimized searching, since it uses word(16/32/64? bit) compare and stuff.
This question already has answers here:
How can I read an input string of unknown length?
(11 answers)
Closed 7 years ago.
There are several ways how to retrieve string input, e.g getline() , or fgets() but all of them require size of the string as an argument. But what if i want to retrieve string of unknown size? How is it possible using getline() or fgets() in C?
The answer is no. You can't read a string of indeterminate length. You can, however, read one character at a time until you reach the size of the storage space you have allocated in your program. Use fgetc in a loop.
int fgetc(FILE *stream)
Open the stream , read one character at a time, and stop reading when you see your sentinel character, which is probably a newline.
This question already has answers here:
Copying a part of a string (substring) in C
(13 answers)
Get a substring of a char* [duplicate]
(5 answers)
Closed 8 years ago.
An MD5 encrypted password is $1$12345678$blahblahblah. The salt is the 8 digit key between the two $ signs after the one. How do i extract those 8?
So far I have char *salt = and i need to make it equal to the third-tenth index of the string.
Your question contains the word "extract" which is somewhat complicated because it is not clear if you want to copy those 8 characters into a new buffer or if you are looking to do something else.
I can imagine that perhaps you simply want to display these 8 characters to a console user. The following code accomplishes this (but probably requires a bit of explanation following):
char *salt = "$1$12345678$blahblahblah";
int from = 3;
int to = 10;
int len = to-from+1;
printf("%.*s\n", len, salt+from);
Printf() and its variants have some pretty powerful string generation/manipulation capabilities. In this case I used a "precision" specifier provided as a parameter for the 's' conversion. If a precision is given for a 's' conversion it is taken to limit the number of characters emitted. I used 'len' via the '*' character to provide this limit in a parametrized fashion. If you know that you are always going to want to emit 8 characters starting from the 3rd (in C we always count from 0 not from 1) then your code can be simplified to the more straightforward form given below:
char *salt = "$1$12345678$blahblahblah";
printf("%.8s\n", salt+3);
Finally, sprintf can be used to copy to another buffer. I'll give a simple form of this task below (note that I changed variable names for clarity; you are extracting the salt from a line of password text):
char *password = "$1$12345678$blahblahblah";
char salt[9]; /* Size of 9 required to hold terminating NULL byte */
sprintf(salt, "%.8s\n", password+3);
/* Now you can use salt for various purposes */
printf ("The salt is %s\n", salt);
This question already has answers here:
sprintf buffer sizes
(3 answers)
Closed 9 years ago.
I'm having an issue with sprintf in C resetting the value of a counter variable when I run it. Here in a nutshell is what is happening:
int count = 0;
count++;
printf("%d\n", count); // count will equal 1
char title[7];
sprintf(title, "%.3d.jpg", count);
printf("%d\n", count); // count now equals 0 again
Is this normal behavior for sprintf?
title is too small, sprintf is writing beyond the bounds of title, and writing into count, corrupting its value.
Note that title needs to be long at least 8 bytes: 3 for %.3d, 4 for .jpg and 1 for the terminator \0.
As Grijesh Chauhan points out, you can make sure that you never write beyond the allocated size of the string by using snprintf, i.e.:
char title[8];
snprintf(title, sizeof(title), "%.3d.jpg", count);
You simply need more space in the string title: 3 digits+".jpg" = 7 chars, and u need one extra for '\0'(end of the string). Sprintf can change argument in a case such as this(Using sprintf will change the specified variable)
Solution: change
char title[7];
to
char title[8];
You problem is called buffer overflow. sprintf has no idea of the size of title, and write has much as needed, even if it exceeds the boundaries of your array. To fully understand, you also have to know that strings are terminated by a zero. This allows to find the end of a string, without prior knowledge of its true size. This extra zero takes the place of an extra character that you have to consider when dimensioning your buffers.
Consider also the use of snprintf that ensures you are not going over the boundaries of your buffer.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
what does ā%.*sā mean in printf in c
I have found the following line :
asprintf(&c, "%s%.*s", *msg_in, size * rxed, buffer)
And I want to know the meaning of %.*s
The %.*s format means "print a string using a field width of n characters, where n is read from the next argument".
So here, it prints buffer with a width of size * rxed characters. (padding with spaces if necessary)
I would highly recommend reading the manual...
.* in a format string means:
the precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.
Details can be seen here.
So you didn't give any details, but if the result of: size * rxed was 5, then you could do this:
asprintf(&c, "%s%.*s", *msg_in, size * rxed, buffer)
or
asprintf(&c, "%s%5s", *msg_in, buffer)
to the same effect.