Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strings handling C linux and windows

I'm a newbie in C programming. I have this issue that I don't understand. It seems that strings under windows are treated in a completely different way respect to linux, why?

Thant's my code

#include <stdio.h>
#include <string.h> // compare strings
void addextname(char *str1, char *str2, char *nome1){
    int i,j;
    i = 0;
    while (str1[i]!='.') {
        nome1[i] = str1[i];
        i++;
    }
    j = 0;
    while (str2[j]!='\0') {
        nome1[i] = str2[j];
        i++;
        j++;
    }
}

int main()
{
    char str1[9]="file.stl";
    char str2[9]="name.stl";
    int len1 = strlen(str1);
    int len2 = strlen(str2);
    char nome1[len1+len2+1];
    addextname(str1,str2,nome1);
    printf("%s  %s  %s\n",str1,str2,nome1);
    return 0;
}

My purpose is to read an input filename within its extension (.stl) and add some chars to it keeping that extension. Under linux I have no problem, under windows instead the output filenames are saved unproperly. My compiling line is

gcc modstr.c -std=c99 -o strings

I really appreciate an answer to that!

like image 309
Nicholas Avatar asked Feb 04 '26 04:02

Nicholas


1 Answers

You're not 0-terminating nome1. Try:

nome1[i] = 0; /* After the second while. */
like image 115
cnicutar Avatar answered Feb 05 '26 21:02

cnicutar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!