Blanking out cstrings in a loop - c

I am trying to iterate through char*
Is there any way to like reset these char* strings back to blank?
I am trying to reset from1 and send1.
Is there anything else wrong with my code.. it is only copying the first file in my array
for(i = 0; i < 3; i++)
{
from1 = " ";
send1 = " ";
from1 = strncat(fileLocation,filesToExport[i],50);
send1 = strncat(whereAmI,filesToExport[i],50);
CopyFile(from1,send1,TRUE);
printf("%s\n",from1);
printf("%s",send1);
}

THe strings are nul terminated, which means they have a zero character at the end. You can set the first char in the string to zero to truncate it back to being empty:
from1[0] = '\0';
Another way would be to copy a blank string:
strcpy(from1, "");

What do you mean by "blank"? Zeroed, spaces, or empty?
For filling a memory area you're best off using memset(), so
#include <string.h>
memset(pBuffer, ' ', length); /* Fill with spaces */
pBuffer[length] = '\0'; /* Remember to null-terminate manually when using memset */
memset(pBuffer, '\0', length); /* Fill with zeroes */
pBuffer[0] = '\0'; /* Set first element to null -- effectively set the string
* to length 0
*/

The easiest way is to set the first byte to 0. Like this:
from1[0] = 0;
send1[0] = 0;
C/C++ checks the end of a char* string by looking for the 0 byte. It doesn't care what follows that.

To clear a string to empty, so that strncat() has an empty string to concatenate to, just do:
from1[0] = '\0';
This sets the first character to the zero terminator that indicates end of string, thus making the string have length 0. This assumes that from1 is an actual modifiable char buffer, but your call to strncat() implies that it is.

You are copying into filelocation and whereami. Are they buffers or strings? You may be writing off the end of your string.
I think you would do better to allocate a suitably sized buffer
fromLen = strlen(fileLocation);
fileLen = strlen(filesToExport[i]);
from1 = malloc(fromLen + fileLen + 1);
/* add check here that string fits */
strcpy( from1, filelocation);
strcat( from1 + fromLen, filesToExport[i]);
/** etc **/
free(from1);

you mean like
send1[0] = 0;
from1[0] = 0;
?

Related

Append extra null char to wide string

Some Win32 API structures require to concatenate an extra null character to a string, as in the following example taken from here:
c:\temp1.txt'\0'c:\temp2.txt'\0''\0'
When it comes to wide strings, what is the easiest way to append a L'\0' to the end of an existing wide string?
Here's what works for me but seems too cumbersome:
wchar_t my_string[10] = L"abc";
size_t len = wcslen(my_string);
wchar_t nullchar[1] = {'\0'};
memcpy(my_string + len + 1, nullchar, sizeof(wchar_t));
In your example you can just assign the value just like any other array. There's nothing special about wchar_t here.
my_string already has a single null-termination, so if you want double null-termination, then just add another 0 after it.
wchar_t my_string[10] = L"abc";
size_t len = wcslen(my_string);
// todo: check out-of-bounds
my_string[len + 1] = 0;
Or even simpler, if it's really just a string literal,
wchar_t my_string[10] = L"abc\0";
This will be doubly-null-terminated.
Assuming you have the various paths in a std::vector<std::wstring>, you can just build the required format in a loop:
std::vector<std::wstring> paths;
paths.emplace_back(L""); // This empty path will add the extra NUL
std::wstring buf(1000, 0);
for (auto p : paths) {
buf.append(p);
buf.append(1, 0);
}
wchar_t *ptr = buf.c_str(); // Now do stuff with it
assuming my_string is long enough:
my_string[wcslen(my_string)+1]='\0';
The terminating null will be translated to a wide char.
(Posted as a first comment to the question)
If you use std::wstring instead of wchar_t[], you can use its operator+= to append the extra null terminator, eg:
wstring my_string = L"abc";
...
my_string += L'\0';
// use my_string.c_str() as needed...

Concatenate a filename to a directory name

I want to add a directory to a filename in c but i get an error like:
Segmentation fault (core dumped)
Here is my code:
char fname[255];
char directoryTmp[262];
/*Working Code for entering the filename fname with fgets() */
...
/* specify the directory */
directoryTmp[0] = "/";
directoryTmp[1] = "f";
directoryTmp[2] = "i";
directoryTmp[3] = "l";
directoryTmp[4] = "e";
directoryTmp[5] = "s";
directoryTmp[6] = "/";
/* Copy fname at the end of directoryTmp */
strcat(directoryTmp,fname);
/* new fname with the directory, should look like: "/files/afilename */
for(i=0;i<strlen(directoryTmp);i++){
fname[i] = directoryTmp[i];
}
//edit
OK this is my new code, but I still get the same error code:
char fname[255];
char directory[262];
directory[sizeof(directory) - 1] = '\0';
strncpy(directory,sizeof(directory) - 1, "/files/");
for(i=0;i<strlen(directory);i++){
fname[i] = directory[i];
}
puts(fname);
fname[sizeof(fname) - 1] = '\0';
chars are put in single quotation marks (''), not double (""). You're assigning string literals to each array index.
The error is that you forgot to NUL terminate your string when filling directoryTmp (in C strings are array of char that by convention ends at the first char whose value is 0). Then, when strcat try to append fname it begins by iterating over directoryTmp looking for a NUL character. As the array is initialized on the stack (at least I guess from the code snippet), its content is undefined, and strcat scan past the end of the array which is undefined behavior (in your particular case this cause a segmentation fault).
So, the correct code would be:
/* specify the directory */
directoryTmp[0] = '/';
directoryTmp[1] = 'f';
directoryTmp[2] = 'i';
directoryTmp[3] = 'l';
directoryTmp[4] = 'e';
directoryTmp[5] = 's';
directoryTmp[6] = '/';
directoryTmp[7] = 0; // NUL terminate the string
Or as mentioned by others, just use strncpy:
memset(directoryTmp, 0, sizeof(directoryTmp));
strncpy(directoryTmp, "/files/", sizeof(directoryTmp) - 1);
Note that strncpy does not guarantee that the string will be NUL terminated, so we have to take care of that ourselves.
You can simply do like this
char fname[255];
char directoryTmp[262];
/*Working Code for entering the filename fname with fgets() */
...
/* specify the directory */
strcpy(directoryTmp,"/files/");
/* Copy fname at the end of directoryTmp */
strcat(directoryTmp,fname);
/* new fname with the directory, should look like: "/files/afilename */
for(i=0;i<strlen(directoryTmp);i++){
fname[i] = directoryTmp[i]; //You need to take care here. Because size of the fname is 255 and size of the directoryTmp is 262. you should check length of the fname in for loop.
}

Print a string reversed in C

I'm coding a program that takes some files as parameters and prints all lines reversed. The problem is that I get unexpected results:
If I apply it to a file containing the following lines
one
two
three
four
I get the expected result, but if the file contains
september
november
december
It returns
rebmetpes
rebmevons
rebmeceds
And I don't understand why it adds a "s" at the end
Here is my code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void reverse(char *word);
int main(int argc, char *argv[], char*envp[]) {
/* No arguments */
if (argc == 1) {
return (0);
}
FILE *fp;
int i;
for (i = 1; i < argc; i++) {
fp = fopen(argv[i],"r"); // read mode
if( fp == NULL )
{
fprintf(stderr, "Error, no file");
}
else
{
char line [2048];
/*read line and reverse it. the function reverse it prints it*/
while ( fgets(line, sizeof line, fp) != NULL )
reverse(line);
}
fclose(fp);
}
return (0);
}
void reverse(char *word)
{
char *aux;
aux = word;
/* Store the length of the word passed as parameter */
int longitud;
longitud = (int) strlen(aux);
/* Allocate memory enough ??? */
char *res = malloc( longitud * sizeof(char) );
int i;
/in this loop i copy the string reversed into a new one
for (i = 0; i < longitud-1; i++)
{
res[i] = word[longitud - 2 - i];
}
fprintf(stdout, "%s\n", res);
free(res);
}
(NOTE: some code has been deleted for clarity but it should compile)
You forget to terminate your string with \0 character. In reversing the string \0 becomes your first character of reversed string. First allocate memory for one more character than you allocated
char *res = malloc( longitud * sizeof(char) + 1);
And the try this
for (i = 0; i < longitud-1; i++)
{
res[i] = word[longitud - 2 - i];
}
res[i] = '\0'; // Terminating string with '\0'
I think I know the problem, and it's a bit of a weird issue.
Strings in C are zero terminated. This means that the string "Hi!" in memory is actually represented as 'H','i','!','\0'. The way strlen etc then know the length of the string is by counting the number of characters, starting from the first character, before the zero terminator. Similarly, when printing a string, fprintf will print all the characters until it hits the zero terminator.
The problem is, your reverse function never bothers to set the zero terminator at the end, which it needs to since you're copying characters into the buffer character by character. This means it runs off the end of your allocated res buffer, and into undefined memory, which just happened to be zero when you hit it (malloc makes no promises of the contents of the buffer you allocate, just that it's big enough). You should get different behaviour on Windows, since I believe that in debug mode, malloc initialises all buffers to 0xcccccccc.
So, what's happening is you copy september, reversed, into res. This works as you see, because it just so happens that there's a zero at the end.
You then free res, then malloc it again. Again, by chance (and because of some smartness in malloc) you get the same buffer back, which already contains "rebmetpes". You then put "november" in, reversed, which is slightly shorter, hence your buffer now contains "rebmevons".
So, the fix? Allocate another character too, this will hold your zero terminator (char *res = malloc( longitud * sizeof(char) + 1);). After you reverse the string, set the zero terminator at the end of the string (res[longitud] = '\0';).
there are two errors there, the first one is that you need one char more allocated (all chars for the string + 1 for the terminator)
char *res = malloc( (longitud+1) * sizeof(char) );
The second one is that you have to terminate the string:
res[longitud]='\0';
You can terminate the string before entering in the loop because you know already the size of the destination string.
Note that using calloc instead of malloc you will not need to terminate the string as the memory gets alreay zero-initialised
Thanks, it solved my problem. I read something about the "\0" in strings but wasn't very clear, which is now after reading all the answers (all are pretty good). Thank you all for the help.

How to truncate C char*?

As simple as that. I'm on C++ btw. I've read the cplusplus.com's cstdlib library functions, but I can't find a simple function for this.
I know the length of the char, I only need to erase last three characters from it. I can use C++ string, but this is for handling files, which uses char*, and I don't want to do conversions from string to C char.
If you don't need to copy the string somewhere else and can change it
/* make sure strlen(name) >= 3 */
namelen = strlen(name); /* possibly you've saved the length previously */
name[namelen - 3] = 0;
If you need to copy it (because it's a string literal or you want to keep the original around)
/* make sure strlen(name) >= 3 */
namelen = strlen(name); /* possibly you've saved the length previously */
strncpy(copy, name, namelen - 3);
/* add a final null terminator */
copy[namelen - 3] = 0;
I think some of your post was lost in translation.
To truncate a string in C, you can simply insert a terminating null character in the desired position. All of the standard functions will then treat the string as having the new length.
#include <stdio.h>
#include <string.h>
int main(void)
{
char string[] = "one one two three five eight thirteen twenty-one";
printf("%s\n", string);
string[strlen(string) - 3] = '\0';
printf("%s\n", string);
return 0;
}
If you know the length of the string you can use pointer arithmetic to get a string with the last three characters:
const char* mystring = "abc123";
const int len = 6;
const char* substring = mystring + len - 3;
Please note that substring points to the same memory as mystring and is only valid as long as mystring is valid and left unchanged. The reason that this works is that a c string doesn't have any special markers at the beginning, only the NULL termination at the end.
I interpreted your question as wanting the last three characters, getting rid of the start, as opposed to how David Heffernan read it, one of us is obviously wrong.
bool TakeOutLastThreeChars(char* src, int len) {
if (len < 3) return false;
memset(src + len - 3, 0, 3);
return true;
}
I assume mutating the string memory is safe since you did say erase the last three characters. I'm just overwriting the last three characters with "NULL" or 0.
It might help to understand how C char* "strings" work:
You start reading them from the char that the char* points to until you hit a \0 char (or simply 0).
So if I have
char* str = "theFile.nam";
then str+3 represents the string File.nam.
But you want to remove the last three characters, so you want something like:
char str2[9];
strncpy (str2,str,8); // now str2 contains "theFile.#" where # is some character you don't know about
str2[8]='\0'; // now str2 contains "theFile.\0" and is a proper char* string.

Concatenating multiple strings?

I am processing an input string, which consists of a process name, followed by an arbitrary amount of arguments.
I need the process name , along with all of the arguments, in one string.
I thought I could use strcat in a loop, so that it cycles through all of the args and each time appends the arg to the string, but I am having problems with getting a string that in empty to begin the loop.
Can anyone help me out with some basic code?
Thanks.
EDIT:
I'm posting my code for clarity. Mike's post is closest to what I have now:
char * temp;
strcpy(temp,"");
for (i = 4; i < argc-1; i++) // last arg is null, so we need argc-1
{
strcat(temp,argv[i]);
strcat(temp," ");
}
ignore the 4 in my for loop for the moment (magic number, i know.)
I am getting a segfault with this code. Is it because of my string assignment? I assume that is the case and hence I asked the question of how i could combine the strings.
Let's say your input strings are in an array of char pointers, suggestively called argv, of length argc.
We first need to determine how much space is needed for the output:
int length = 0;
for (int i = 0; i < argc; ++i)
length += strlen(argv[i]);
Then we allocate it, adding an extra char for the '\0' terminator:
char *output = (char*)malloc(length + 1);
Finally, the concatenation:
char *dest = output;
for (int i = 0; i < argc; ++i) {
char *src = argv[i];
while (*src)
*dest++ = *src++;
}
*dest = '\0';
Note that I don't use strcat here. Reason is that this sets us up for a Schlemiel the Painter's algorithm: for each iteration, the entire output string would be scanned to find its end, resulting in quadratic running time.
Don't forget to free the output string when you're done:
free(output);
I'm a bit tired so I may be overlooking something here. A better solution, using standard library functions, is welcome. It would be convenient if strcat returned a pointer to the terminator byte in dest, but alas.
You want an empty C string? Is this what you are looking for: char p[] = "";?
UPDATE
After you posted some code it is clear that you have forgotten to allocate the buffer temp. Simply run around the arguments first, counting up the length required (using strlen), and then allocate temp. Don't forget space for the zero terminator!
You could provide the "arbitrary amount of arguments" as one argument, ie an array/list, then do this pseudocode:
str = "";
i = 0;
while i < length of input
{
str = strcat ( str , input[i]);
i++;
}
#include<stdio.h>
#include<stdarg.h>
int main(int argc, char** argv) {
// the main parameters are the same situation you described
// calling this program with main.exe asdf 123 fdsa, the program prints out: asdf123fdsa
int lengths[argc];
int sum =0;
int i;
for(i=1; i<argc; i++) { // starting with 1 because first arg is program-path
int len = strlen(argv[i]);
lengths[i] = len;
sum+=len;
}
char* all = malloc(sum+1);
char* writer = all;
for(i=1; i<argc; i++) {
memcpy(writer, argv[i], lengths[i]);
writer+=lengths[i];
}
*writer = '\0';
printf("%s\n", all);
system("pause");
return 0;
}
A string in C is represented by an array of characters that is terminated by an "null" character, '\0' which has the value 0. This lets all string functions know where the end of a string is. Here's an exploration of different ways to declare an empty string, and what they mean.
The usual way of getting an empty string would be
char* emptyString = "";
However, emptyString now points to a string literal, which cannot be modified. If you then want to concatenate to an empty string in your loop, you have to declare it as an array when you initialize.
char buffer[] = "";
This gives you an array of size one. I.e. buffer[0] is 0. But you want an array to concatenate to- it has to be large enough to accomodate the strings. So if you have a string buffer of certain size, you can initialize it to be empty like so:
char buffer[256] = "";
The string at buffer is now "an empty string". What it contains, is buffer[0] is 0 and the rest of the entries of the buffer might be garbage, but those will be filled once you concatenate your other strings.
Unfortunately, the problem with C, is you can never have an "infinite" string, where you are safe to keep concatenating to, you have to know it's definite size from the start. If your array of arguments are also strings, you can find their length using strlen. This gives you the length of a string, without the null character. Once you know the lengths of all your sub-strings, you will now know how long your final buffer will be.
int totalSize; // assume this holds the size of your final concatenated string
// Allocate enough memory for the string. the +1 is for the null terminator
char* buffer = malloc(sizeof(char) * (totalSize + 1));
buffer[0] = 0; // The string is now seen as empty.
After this, you are free to concatenate your strings using strcat.

Resources