Hello,
I am making some program, and have some problem, i want to list all files that are on HD, and if I want to list all files in C: with this command with only one backslash after C:( system(dir C:\ /s/b/a-d> C.txt)) it only list files that are in C:\Users\Tomo\Documents\Visual Studio 2008\Projects\name\name, but when I enter this command with two backslashs after C:(system(dir C:\\ /s/b/a-d> C.txt)) it lists all files in C:, what is OK, but there are many files that I want to read, and I cant add everytime one backslash more,(if I want to list all files in D:, I can do it with two or one backslash, no difference).
Note: I can list all files whith one backslash in CMD, but not in c++.
Hope you understand my problem.
Thans.

Anytime you want a special character in a string, you use \x where x is the character. Therefore, the \ itself is a special character, too. So you must use \x format to add one -- hence \\

Hello,
I am making some program, and have some problem, i want to list all files that are on HD, and if I want to list all files in C: with this command with only one backslash after C:( system(dir C:\ /s/b/a-d> C.txt)) it only list files that are in C:\Users\Tomo\Documents\Visual Studio 2008\Projects\name\name, but when I enter this command with two backslashs after C:(system(dir C:\\ /s/b/a-d> C.txt)) it lists all files in C:, what is OK, but there are many files that I want to read, and I cant add everytime one backslash more,(if I want to list all files in D:, I can do it with two or one backslash, no difference).
Note: I can list all files whith one backslash in CMD, but not in c++.
Hope you understand my problem.
Thans.

My guess ( I don't use Micro$oft! ) is that the double backslash is acting as an 'escape' character. This is what happens:
Most operating systems use special characters inside of strings to mean special things. For example '\n' means newline, '\t' means TAB, etc. So, whenever the program sees a \ it is expecting a special character to follow. This is called escaping, because the character that follows 'escapes' from being processed normally, and the computer tries to find the special command to execute for that character. In this case the "C:\ /s/b/a" is trying to escape a space and ignoring the \. Since there is no special command for space, it just leaves the space in the string. The \ however, disappears. So, the computer sees "C: /s/b/a" which (iirc) means look in the current working directory of C, which is your visstudio porject's folder. When you use the double \\, you are escaping a \. So, the program drops the first slash, sees that there is no special command for an escaped \, and leaves only the second \ in the string. Consequently, the computer sees "C:\ /s/b/a" which corresponds to the root directory of the C: drive. You will have to use the double backslash as long as you are processing strings this way.

My guess ( I don't use Micro$oft! ) is that the double backslash is acting as an 'escape' character. This is what happens:
Most operating systems use special characters inside of strings to mean special things. For example '\n' means newline, '\t' means TAB, etc. So, whenever the program sees a \ it is expecting a special character to follow. This is called escaping, because the character that follows 'escapes' from being processed normally, and the computer tries to find the special command to execute for that character. In this case the "C:\ /s/b/a" is trying to escape a space and ignoring the \. Since there is no special command for space, it just leaves the space in the string. The \ however, disappears. So, the computer sees "C: /s/b/a" which (iirc) means look in the current working directory of C, which is your visstudio porject's folder. When you use the double \\, you are escaping a \. So, the program drops the first slash, sees that there is no special command for an escaped \, and leaves only the second \ in the string. Consequently, the computer sees "C:\ /s/b/a" which corresponds to the root directory of the C: drive. You will have to use the double backslash as long as you are processing strings this way.

thank for your reply, I understand what you are saying, more/less.
The thing is, i want to make a .txt file of all files in C: and i want program to read the paths of all of them and automatically read each at a time.
I can make the list of all files in C: with double \\, but then the paths of files are like C:\user\...., and then I cant read them because there is only one \.
Is there any other way I could make or read files that are on C:, without double \\, because I would copy the path from the .txt file, but not manually?
Thank you.

If you are reading a file with the \'s in them, you have no problem. Same with typing the \ from the keyboard at one of your program's prompts. It's only when using the \ in a string in your source code that the double \ is needed.

And lima01, this is true for the language, not a specific compiler. Whether M$, DevC, g++, all compilers act the same.

If you are reading a file with the \'s in them, you have no problem. Same with typing the \ from the keyboard at one of your program's prompts. It's only when using the \ in a string in your source code that the double \ is needed.

And lima01, this is true for the language, not a specific compiler. Whether M$, DevC, g++, all compilers act the same.

Tnx for reply,
see this:
strcpy(windows,"dir :\/s/b/a-d> .txt");
windows[4]=C;
windows[15]=C;
system(windows);
it will list all the files in working c++ project, not files in the C:, same as: system(dir C:\/s/b/a-d>C.txt) would do, no difference.
Is there any way to avoid the double \\, because it make no sence that the c++ cant read file by file automatically without me entering the \ . Do you Know maybe another way of searching and reading the content of many files and searching particulary part?

Is strcpy(windows,"dir :\/s/b/a-d> .txt"); part of your C++ source code? What have we said about \ in C++ source code? \\ in C++ Source code is a single character -- a single \.

If you want a list of all the files on your he you don't need the \ character at all. Just this: system("dir c: /s > c.txt");

If you want a list of all the files on your he you don't need the \ character at all. Just this: system("dir c: /s > c.txt");

Yes you do. The command above will list files starting with the current directory -- probably from the location of the .exe file. system("dir c:\\ /s > c.txt"); will start listing from the root directory.

Ok guys, I understand that I need to use double \\, but for example, if I have an txt file - 1.txt, in wich is "C:\Users\2.txt", and if I read that(C:\Users\2.txt) and save it to -string[20]={C:\Users\2.txt}, how could I open that file using identifier string(not C:\Users\2.txt)?
PS. I dont know what is in 1.txt, it could be any file, so I cant manually add one more slash.

PS. I dont know what is in 1.txt, it could be any file, so I cant manually add one more slash.

You still have absolutely no idea what you are doing, even with all the help we've tried to give you.

The contents of the file have no bearing on whether you use \ or \\.

You'd better explain in extremely minute detail exactly what you want, exactly what you're doing, exactly what is happening, and exactly why you think it's not working.

Yes you do. The command above will list files starting with the current directory -- probably from the location of the .exe file. system("dir c:\\ /s > c.txt"); will start listing from the root directory.

You are right -- I thought it worked, but all it gave me was a list of the current directory.

Ok guys, I understand that I need to use double \\, but for example, if I have an txt file - 1.txt, in wich is "C:\Users\2.txt", and if I read that(C:\Users\2.txt) and save it to -string[20]={C:\Users\2.txt}, how could I open that file using identifier string(not C:\Users\2.txt)?
PS. I dont know what is in 1.txt, it could be any file, so I cant manually add one more slash.

All you have to do is read the line into a string object then pass that to open(). As Walt explained before the \\ issue only occurs in string literals -- text between quotes such as "c:\\" It does NOT apply to strings that are read from a data file or from the keyboard.

// open the file created by the dir command
ifstream in("i.txt");
std::string
// read a line from that file
getline(in, string);
// use the contents of that string
// to open another file.
ifstream in2(string.c_str());

post removed.....AncientDragon already said it

AncientDragon, thank you for reply, but i think that I cant understand you, the thing with passing the string to open(), not with my knowledge of c++ so far. I can compare every character with \ and then add another one but it would be too slow, and I think that it has to be a more easier way, and it obviously does, but so far I dont know how to work with fstream, I think that that is object programming, I will learn that next semester in college.

>>ompare every character with \ and then add another one but it would be too slow,

HOW MANY TIMES DO WE HAVE TO TELL YOU WHAT YOU DO NOT HAVE TO DO THAT!!! :@

>>ompare every character with \ and then add another one but it would be too slow,

HOW MANY TIMES DO WE HAVE TO TELL YOU WHAT YOU DO NOT HAVE TO DO THAT!!! :@

I saw a post of some guy who sad that I could compare every character with \ and then add \, but it seems that moderator has deleted that, I know that that it is not the way to be done, thats what i am saying, you dont need to tell me that.

I figured it out(Čavaru-skonto sam kako se moze), thank you guys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.