HanDs
管理员

[Visual Studio文章] c++中关于文件操作资料收集 





学习中请遵循国家相关法律法规,黑客不作恶。没有网络安全就没有国家安全

本站需要登陆后才能查看

c++ file i/o with binary files using fstream class is a simple task. fstream class has the capability to do both input as well as output operati i.e., read and write. all types of operati like reading/ writing of characters, strings, lines and not to mention buffered i/o are supported by fstream.
operating systems store the files in binary file format. computers can deal with binary numbers. but binary files are not readable by humans. our level of comfort lies with proper ascii or unicode characters. this article deals with how c++ file i/o class fstream can be used for reading and writing binary files. for ascii file operati in c++, refer to c++ text file i/o article.
for our c++ file i/o binary file examples, we assume a struct websites with two members as follows.
// struct for c++ file i/o binary file sample
struct websites
{
char sitename[100];
int rank;
};
write operati in c++ binary file i/o:
there are some important points to be noted while doing a write operation.
• the file has to be opened in output and binary mode using the flags ios::out (output mode) and ios::binary( binary mode)
• the function write takes two parameters. the first parameter is of type char * for the data to be written and the sec is of type int asking for the size of data to be written to the binary file.
• file has to be closed at the end.
// sample for c++ file i/o binary file write
void write_to_binary_file(websites p_data)
{
fstream binary_file("c:test.dat",ios::out|ios::binary|ios::app);
binary_file.write(reinterpret_cast(&p_data),sizeof(websites));
binary_file.close();
}
the above c++ file i/o binary sample function writes some data to the function. the file is opened in output and binary mode with ios::out and ios::binary. there is more specifier ios::app, which tells the operating system that the file is also opened in append mode. this means any new set of data will be appended to the end of file.
also the write function used above, needs the parameter as a character pointer type. so we use a type c reinterpret_cast to typecast the structure into char* type.

read operati in c++ binary file i/o:
this also has a similar flow of operati as above. the difference is to open the file using ios::in, which opens the file in read mode.
// sample for c++ file i/o binary file read
void read_from_binary_file()
{
websites p_data;
fstream binary_file("c:test.dat",ios::binary|ios::in);
binary_file.read(reinterpret_cast(&p_data),sizeof(websites));
binary_file.close();

cout< cout<<"rank :"<< p_data.rank<
}
most of the programs will usually go for ascii text mode files only. but there will be some occasi where the c++ file i/o with binary files will be very useful.
this " c++ file i/o using binary files" article gives some very basic samples for read and write. advanced c++ file i/o operati like seek, checking the file pointer validity etc., are also needed to be learnt while writing bigger programs.
cfile is the class used for handling files in mfc. this class can be used for creating, reading, writing and modifying files. it directly provides unbuffered, binary disk input/output services, and it indirectly supports text files and memory files through its derived classes.
cfile - creating a file:
there are two ways of creating files. one way is to instantiate the cfile object with the file path. this creates the file. the sec way is to call the open function. this also creates the file.
cfile cfile_object( "c:testcodersource_cfile_example.txt", cfile::modecreate|cfile:: modereadwrite);

cfile cfile_object;
cfile_object.open( "c:testcodersource_cfile_example.txt", cfile::modecreate|cfile:: modereadwrite);

the first parameter to both the functi (cfile() c and open()) is the physical path of the file in the disk. the sec parameter is an enumerated constant. this specifies the mode of opening the file object. the above c modecreate implies "create a new file" and modereadwrite means "open the file for both reading and writing".
if the file is opened without specifying the mode c sharedenyn this file can be opened in read mode by other programs. this feature will be necessary for text files, logs created by programs. for creating text files we use cfile::typetext and for binary files cfile::typebinary.
cfile - writing to a file:
the function write is used to write data to the files. the sample code is as follows.

cfile cfile_object;
cfile_object.open( "c:testcodersource_cfile_example.txt", cfile::modecreate|cfile::modewrite);

char szsampletext[100];
strcpy(szsampletext, "sample text for cfile write function example");
cfile_object.write (szsampletext,100);
if there is any need to write text line by line, it is better to use the class cstdiofile.
cfile - reading from a file:
the function read is used to read data from files. the sample code is,

cfile cfile_object;
cfile_object.open( "c:testcodersource_cfile_example.txt", cfile::modecreate|cfile::modewrite);

char szsampletext[100];
uint lbytesread = cfile_object.read (szsampletext,100);
the function returns the number of bytes read from the file. the maximum number of characters read will be the sec parameter of the read function.
cfile - closing the file:
the close function is used to close the file. but the close function need not be called, as the destructor will automatically call it if the file is open. so when the object goes out of scope, the destructor calls close function.
file handling is an important part of all programs. most of the applicati will have their own features to save some data to the local disk and read data from the disk again. c++ file i/o classes simplify such file read/write operati for the programmer by providing easier to use classes.
c++ file i/o classes and functi
there are 3 file i/o classes in c++ which are used for file read/write operations. they are
• ifstream - can be used for file read/input operati
• ofstream - can be used for file write/output operati
• fstream - can be used for both read/write c++ file i/o operati
the most important methods which will be used for any file operati are:
1. fstream::open method - to open the file
2. fstream::operator >> and fstream::operator << - for reading from or writing to the file.
3. fstream::close - flushes all buffer and close the file
reading a text file using fstream class:
there are several ways of reading the text from a file. but all of them have a common approach as follows.
1. open the file
2. read the data
3. close the file
this sample code snippet explains how to use the c++ file i/o stream operators to read data from a file. in all cases the header file fstream.h must be included.
________________________________________
#include
int main()
{
char str[2000];
fstream file_op("c:test_file.txt",ios::in);
while(file_op >> str)
cout < < str ;

file_op.close();

return 0;
}
________________________________________
the class fstream, which is used above is the which is comm used for c++ file i/o manipulations. the c of fstream takes 2 parameters. one is the file path and the sec is the file open mode. there are several open modes, each of them with a different purpose. some of them are ios::in for reading, ios::out for writing, ios::app for appending to the end of file, ios::binary for opening in binary mode etc.,
now for the purpose of this article, as the data is read from the file, the flag ios::in is used. after this, the read operation is c till the end of the file. the while loop ensures a c read till the end of the file or it encounters any abnormal break. if the program is built and run , it displays all the data read from the file. the c++ file i/o read job is done.
but if we look at the output closely, there is a draw back in using this stream operator read. the output misses the white spaces and the end of line characters. in order not to miss these characters we can either use fstream::get() or fstream::getline() methods. here is the example for using fstream getline method.
________________________________________
#include
int main()
{
char str[2000];
fstream file_op("c:test_file.txt",ios::in);
while(!file_op.eof())
{
file_op.getline(str,2000);
cout < } file_op.close();
cout <
return 0;
}

________________________________________
writing to a text file using fstream class:
writing to a text file can also be achieved with the stream operators. this also follows the same order of operati though with a slight difference.
1. open a file - in write mode
2. write to a file
3. close the file
look at the following sample code to see the difference.
________________________________________
#include
int main()
{
fstream file_op("c:codersource_file.txt",ios::out);

file_op<<"test write to file";
file_op.close();
return 0;
}
________________________________________
to modify the data or to seek to a different position inside the file, the c++ file i/o class fstream provides member functi like seekg() etc., these functi can be used to relocate the record insert position to the desired locations.
after all the c++ file i/o operati we do a fstream::close(), to close the file pointer. this is not mandatory. even if this function is not called by the applicati the destructor of the fstream class will close the file when the object goes out of scope.


学习中请遵守法律法规,本网站内容均来自于互联网,本网站不负担法律责任
c + + 中关于文件操作资料收集
#1楼
发帖时间:2016-7-9   |   查看数:0   |   回复数:0
游客组
快速回复