How to put two backslash in C++

前端 未结 5 1538
北海茫月
北海茫月 2021-01-15 19:48

i need to create a function that will accept a directory path. But in order for the compiler to read backslash in i need to create a function that will make a one backslash

5条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-15 20:02

    int arrlength = sizeof(newpath); causes the size of your entire array (in chars) to be assigned to arrlength. This means you are iterating over 99999 characters in the array, even if the path is shorter (which it probably is).

    Your loop condition also allows goes one past the bounds of the array (since the last (99999th) element is actually at index 99998, not 99999 -- arrays are zero-based):

    for (int i = 0; newpath[i]] != '\0'; i++)
    

    Also, there is no reason to copy the string into a character array first, when you can loop over the string object directly.

    In any case, there is no need to escape backslashes from user input. The backslash is a single character like any other; it is only special when embedded in string literals in your code.

提交回复
热议问题