how to create files named with current time?

前端 未结 4 472

I want to create a series of files under \"log\" directory which every file named based on execution time. And in each of these files, I want to store some log info for my p

4条回答
  •  醉梦人生
    2021-01-15 02:54

    Steps to create (or write to) a sequential access file in C++:

    1.Declare a stream variable name:

    ofstream fout;  //each file has its own stream buffer
    

    ofstream is short for output file stream fout is the stream variable name (and may be any legal C++ variable name.) Naming the stream variable "fout" is helpful in remembering that the information is going "out" to the file.

    2.Open the file:

    fout.open(filename, ios::out);

    fout is the stream variable name previously declared "scores.dat" is the name of the file ios::out is the steam operation mode (your compiler may not require that you specify the stream operation mode.)

    3.Write data to the file:

    fout<

    The data must be separated with space characters or end-of-line characters (carriage return), or the data will run together in the file and be unreadable. Try to save the data to the file in the same manner that you would display it on the screen.

    If the iomanip.h header file is used, you will be able to use familiar formatting commands with file output.

    fout<

    4.Close the file:

    fout.close( );
    

    Closing the file writes any data remaining in the buffer to the file, releases the file from the program, and updates the file directory to reflect the file's new size. As soon as your program is finished accessing the file, the file should be closed. Most systems close any data files when a program terminates. Should data remain in the buffer when the program terminates, you may loose that data. Don't take the chance --- close the file!

提交回复
热议问题