简体   繁体   中英

I get the error message:Expression: (L “String is not null terminated” & & 0)

When I run my Program,I get the error message.I don't konw how to correct it.May I get a help?

char dir[1024]="C:\\Users\\UserName\Desktop\\new folder\\Pauli\\T3";
void check_dir(char *dir)
{
int i;
i = 0;
while (dir[i] != '\0') {
    if (dir[i] == '/')
        dir[i] = '\\';
    i++;
}
strcat_s(dir, sizeof(dir),"\\");
}

sizeof(dir) doesn't do what you obviously expect here. As dir is a char * inside your function, it just gives you the size of the pointer (*). Your only option is to pass the size of the array to your check_dir() function, too:

void check_dir(char *dir, size_t bufsize)
{
    [...]
    strcat_s(dir, bufsize, "\\");
}

(*) In the scope where dir is declared char dir[1024] , sizeof(dir) will give you the expected result.

edit: On a side note, check_dir() is a misnomer here, as it doesn't check anything but tries to normalize the string to be a windows path with backslashes. Call it eg win32_normalize_path() or something like that. Something called check_<foo>() should return something (eg int ) containing the result of the check.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM