#### Archived

This topic is now archived and is closed to further replies.

# File operations in C++

This topic is 5968 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi. I''m a total newbie in C++, and need some urgent help with file management in this language: - Which functions should I use to move, delete and rename files, and in which header files are those functions defined? - How does C++ reference folders and sub-folders in file paths? I tried the usual DOS/VB approach (c:\program files\Adope for example , but that doesn''t work. - Also, how can I reference a sub-folder without knowing which folder the main program is? (in DOS I used the period, for example, ./Sprites or whatever) Thanks in advance!

##### Share on other sites
Make sure "hard coded" strings have two \ every time u use them
i.e. "c:\\Projects\\nuts\\data\\" as other wise they may be translated into other characters. "c:\Projects\nuts\data\" will cause c++ to place a newline at the \n (carriage return or lf i can''t remember (or both?)) and your string will most likely be open as \" is another escape sequence (i think that is what they are called)

You should be able to use . to refer to the current directory.
As for file functions look into rename,remove and as for moving well i can''t find a standard c lib function for that atm

As You get more advanced you will probably want to look into iostreams which is the c++ rather than the c way to do it

##### Share on other sites
quote:
- Which functions should I use to move, delete and rename files, and in which header files are those functions defined?

Those aren''t standard functions, they''re operating-system specific.

quote:
- How does C++ reference folders and sub-folders in file paths? I tried the usual DOS/VB approach (c:\program files\Adope for example , but that doesn''t work.

C and C++ require you to "escape" certain special characters - double quotation mark ("), backslash (\). The reason is that to place a qoutation mark in a quoted string would close the string, so the language needs some way of knowing the quote is part of the string:

cout << "The word \"the\" appears quoted in this sentence" << endl;

Since we use the backslash to escape the quotation mark, then how does the language know when you want to use an actual backslash? Escape it too:

cout << "This is a backslash: \\" << endl;

Now that we''ve established this, we might as well use escape sequences (they''re called escape sequences because they "escape" the usual string interpretation) for a few other purposes:

\n - New line
\t - Tab
etc

quote:
- Also, how can I reference a sub-folder without knowing which folder the main program is? (in DOS I used the period, for example, ./Sprites or whatever)

Same way.

I wanna work for Microsoft!
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

##### Share on other sites
quote:
- Which functions should I use to move, delete and rename files, and in which header files are those functions defined?

quote:
Oluseyi
Those aren''t standard functions, they''re operating-system specific.

int remove( char* filename ) is actually ANSI C.

The world holds two classes of men -- intelligent men without religion, and religious men without intelligence. Abu''l-Ala-Al-Ma''arri (973-1057; Syrian poet)

##### Share on other sites
quote:
- Which functions should I use to move, delete and rename files, and in which header files are those functions defined?

This normally isn''t recommened, but you can use System() to use DOS commands. For instance, to rename a file, you''d use System("ren file.txt file.bat");. Again, you probably shouldn''t use this, especially if you''re planning to use this for anything besdies private purposes.

quote:
posted by _Stringer
Make sure "hard coded" strings have two \ every time u use them
i.e. "c:\\Projects\\nuts\\data\\" as other wise they may be translated into other characters. "c:\Projects\nuts\data\" will cause c++ to place a newline at the \n (carriage return or lf i can''t remember (or both?)) and your string will most likely be open as \" is another escape sequence (i think that is what they are called)

Actually, I just read about this yesterday: you can create macros to do that for you. For instance, if the macro name was MyMacro, and I pass use
MyMacro(Hello, my name is "//Matrix2113//");
, it would replace it (when compiling) with
 cout << "Hello, my name is /"////Matrix2113/////";
. I think you have to use the # symbol, but it might be ## (I''m too lazy to look back into the MSVC++ docs, you can do it yourself)
Assuming its ##, it would look like this:
#define MyMacro(x) cout << ##x

It would turn x into a direct quote (inserting everything needed manually, including the "quotes"). It might be VC++ specific, I''m not sure.

"I''ve learned something today: It doesn''t matter if you''re white, or if you''re black...the only color that REALLY matters is green"
-Peter Griffin

##### Share on other sites
A clipping from the man pages (rename(2) to be precise):
NAME       rename - change the name or location of a fileSYNOPSIS       #include <stdio.h>       int rename(const char *oldpath, const char *newpath);CONFORMING TO       POSIX, 4.3BSD, ANSI C

• 11
• 20
• 12
• 10
• 34
• ### Forum Statistics

• Total Topics
631399
• Total Posts
2999854
×