0

I having an issue with my program which is expected to either ask a user to input a directory path or to use an predefine directory path as an argument so that my program can open that directory.

Asking a user to input the directory path (C:\Users\Desktop\Test) works fine. But I am having an issue when I am passing the file path as argument.

It should be passed as this:

char location[1000] = "C:\Users\Desktop\Test";

if ((dir = opendir (location)) != NULL){

...... }

But using argument, my program can only open the directory if location is initialised and assigned as this:

char location[1000] = "C:\\Users\\Desktop\\Test";

if ((dir = opendir (location)) != NULL){

...... }

However, I need to concatenate location with a filename in another part of my program so that it becomes: C:\Users\Desktop\Test\file.txt

It won`t work with: C:\\Users\\Desktop\\Test\\file.txt.

char location[1000] can`t be modified as it works well with my code when opening or closing directory.

Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
  • 1
    Possible duplicate of [How to concatenate const/literal strings in C?](http://stackoverflow.com/questions/308695/how-to-concatenate-const-literal-strings-in-c) – MooseBoys Jun 16 '16 at 18:42
  • @MooseBoys I have an issue with the: C:\\Users\\Desktop\\Test\\....I need to be able to open the directory when it is like: C:\Users\Desktop\Test\ but windows don`t accept to open it as C:\Users\Desktop\Test\ but with C:\\Users\\Desktop\\Test\\ only. –  Jun 16 '16 at 18:48
  • 2
    are you accepting input from a user? [this](http://en.cppreference.com/w/cpp/language/escape) is the reason your former example isn't working...backslash is reserved in string literals. – kirinthos Jun 16 '16 at 18:52
  • @kirinthos I give the user two choices, either to input the path or I define a path. When User input the path, it works well as it is: C:\Users\Desktop\Test\ but with parameter, it only open when it is like: C:\\Users\\Desktop\\Test\\ but I need it to be open with: C:\Users\Desktop\Test\ instead. –  Jun 16 '16 at 18:57
  • 1
    right...well when declaring a _string literal_ "C:\\Users" => C:\Users – kirinthos Jun 16 '16 at 19:00
  • A minimum working test source snippet would go further to explain what you're trying to fix then continued linguistic explanations. – Aumnayan Jun 16 '16 at 19:01
  • @kirinthos i tried to do it in another way like: char location[1000]; strcpy(location,"C:\Users\ASG\Desktop\TEst\"); but i get error: expected ';' before '}' token| –  Jun 16 '16 at 19:07
  • @Aumnayan i have already tested with string it only worked with char location[1000]; not with string location; –  Jun 16 '16 at 19:15
  • please, please [read this link](http://en.cppreference.com/w/cpp/language/escape) – kirinthos Jun 16 '16 at 19:17
  • cool, to you see why the first string is actually want you want? that "\\" => \ and why you got the 'no semi colon' error in your strcpy texampl – kirinthos Jun 16 '16 at 19:30
  • @kirinthos so basically char location[1000] = "C:\\Users\\Desktop\\Test"; is the same as char location[1000] = "C:\Users\Desktop\Test"; ? –  Jun 16 '16 at 19:41
  • 3
    You need to understand the `\\` is an escape character in string literals. – drescherjm Jun 16 '16 at 19:52
  • Is this question about C or C++? Please pick one and delete the other tag. – xaxxon Jun 16 '16 at 23:48

0 Answers0