Write a file in a specific path in C++

后端 未结 7 747
無奈伤痛
無奈伤痛 2020-11-30 10:29

I have this code that writes successfully a file:

    ofstream outfile (path);
    outfile.write(buffer,size);
    outfile.flush();
    outfile.close();


        
相关标签:
7条回答
  • 2020-11-30 10:45

    That needs to be done when you open the file, see std::ofstream constructor or open() member.

    0 讨论(0)
  • 2020-11-30 10:45

    If you are using linux, try execl(), with the command mv.

    0 讨论(0)
  • 2020-11-30 10:46

    Rationale for posting another answer

    I'm posting because none of the other answers cover the problem space.

    The answer to your question depends on how you get the path. If you are building the path entirely within your application then see the answer from @James Kanze. However, if you are reading the path or components of the path from the environment in which your program is running (e.g. environment variable, command-line, config files etc..) then the solution is different. In order to understand why, we need to define what a path is.

    Quick overview of paths

    On the operating systems (that I am aware of), a path is a string which conforms to a mini-language specified by the operating-system and file-system (system for short). Paths can be supplied to IO functions on a given system in order to access some resource. For example here are some paths that you might encounter on Windows:

    \file.txt
    \\bob\admin$\file.txt
    C:..\file.txt
    \\?\C:\file.txt
    .././file.txt
    \\.\PhysicalDisk1\bob.txt
    \\;WebDavRedirector\bob.com\xyz
    C:\PROGRA~1\bob.txt
    .\A:B
    

    Solving the problem via path manipulation

    Imagine the following scenario: your program supports a command line argument, --output-path=<path>, which allows users to supply a path into which your program should create output files. A solution for creating files in the specified directory would be:

    1. Parse the user specified path based on the mini-language for the system you are operating in.
    2. Build a new path in the mini-language which specifies the correct location to write the file using the filename and the information you parsed in step 1.
    3. Open the file using the path generated in step 2.

    An example of doing this:

    On Linux, say the user has specified --output-path=/dir1/dir2

    Parse this mini-language:

    /dir1/dir2
    --> "/" root
    --> "dir1" directory under root
    --> "/" path seperator
    --> "dir2" directory under dir1
    

    Then when we want to output a file in the specified directory we build a new path. For example, if we want to output a file called bob.txt, we can build the following path:

    /dir1/dir2/bob.txt
    --> "/" root
    --> "dir1" directory under root
    --> "/" path separator
    --> "dir2" directory under dir1
    --> "/" path seperator
    --> "bob.txt" file in directory dir2
    

    We can then use this new path to create the file.

    In general it is impossible to implement this solution fully. Even if you could write code that could successfully decode all path mini-languages in existence and correctly represent the information about each system so that a new path could be built correctly - in the future your program may be built or run on new systems which have new path mini-languages that your program cannot handle. Therefore, we need to use a careful strategy for managing paths.

    Path handling strategies

    1. Avoid path manipulation entirely

    Do not attempt to manipulate paths that are input to your program. You should pass these strings directly to api functions that can handle them correctly. This means that you need to use OS specific api's directly avoiding the C++ file IO abstractions (or you need to be absolutely sure how these abstractions are implemented on each OS). Make sure to design the interface to your program carefully to avoid a situation where you might be forced into manipulating paths. Try to implement the algorithms for your program to similarly avoid the need to manipulate paths. Document the api functions that your program uses on each OS to the user - this is because OS api functions themselves become deprecated over time so in future your program might not be compatible with all possible paths even if you are careful to avoid path manipulation.

    2. Document the functions your program uses to manipulate paths

    Document to the user exactly how paths will be manipulated. Then make it clear that it is the users responsibility to specify paths that will work correctly with the documented program behavior.

    3. Only support a restricted set of paths

    Restrict the path mini-languages your program will accept until you are confident that you can correctly manipulate the subset of paths that meet this set of restrictions. Document this to the user. Error if paths are input that do not conform.

    4. Ignore the issues

    Do some basic path manipulation without worrying too much. Accept that your program will exhibit undefined behavior for some paths that are input. You could document to the user that the program may or may not work when they input paths to it, and that it is the users responsibly to ensure that the program has handled the input paths correctly. However, you could also not document anything. Users will commonly expect that your program will not handle some paths correctly (many don't) and therefore will cope well even without documentation.

    Closing thoughts

    It is important to decide on an effective strategy for working with paths early on in the life-cycle of your program. If you have to change how paths are handled later it may be difficult to avoid a change in behaviour that might break the your program for existing users.

    0 讨论(0)
  • 2020-11-30 10:50

    I was stuck on this for a while and have since figured it out. The path is based off where your executable is and varies a little. For this example assume you do a ls while in your executable directory and see:

    myprogram.out  Saves  
    

    Where Saves is a folder and myprogram.out is the program you are running.

    In your code, if you are converting chars to a c_str() in a manner like this:

    string file;
    getline(cin, file, '\n');
    ifstream thefile;
    thefile.open( ("Saves/" + file + ".txt").c_str() );
    

    and the user types in savefile, it would be

    "Saves/savefile.txt"   
    

    which will work to get to to get to savefile.txt in your Saves folder. Notice there is no pre-slashes and you just start with the folder name.

    However if you are using a string literal like

    ifstream thefile;
    thefile.open("./Saves/savefile.txt");
    

    it would be like this to get to the same folder:

    "./Saves/savefile.txt"  
    

    Notice you start with a ./ in front of the foldername.

    0 讨论(0)
  • 2020-11-30 10:51

    Try this:

    ofstream outfile;
    string createFile = "";    
    string path="/FULL_PATH";
    
    createFile = path.as<string>() + "/" + "SAMPLE_FILENAME" + ".txt";          
    outfile.open(createFile.c_str());       
    outfile.close();
    
    //It works like a charm.
    
    0 讨论(0)
  • 2020-11-30 11:04

    Specify the full path in the constructor of the stream, this can be an absolute path or a relative path. (relative to where the program is run from)

    The streams destructor closes the file for you at the end of the function where the object was created(since ofstream is a class).

    Explicit closes are a good practice when you want to reuse the same file descriptor for another file. If this is not needed, you can let the destructor do it's job.

    #include <fstream>
    #include <string>
    
    int main()
    {
        const char *path="/home/user/file.txt";
        std::ofstream file(path); //open in constructor
        std::string data("data to write to file");
        file << data;
    }//file destructor
    

    Note you can use std::string in the file constructor in C++11 and is preferred to a const char* in most cases.

    0 讨论(0)
提交回复
热议问题