C++fstream file read/write (compared to Java)

Source: Internet
Author: User
Tags processing text

One: Cause

(1) Before processing text data, all kinds of cleaning data are used in Java File,filereader/filewriter,bufferedreader/bufferedwriter class, see Java Read-write text file

(2) Java is used because the map in Java is very flexible, the Eclipse compiler is more force, and CTRL can trace functions, see Java map sort

(3) Another reason to apply Java is that the string processing of strings within Java is very flexible, and that functions should be exhausted.

(4) The above two points is your own misunderstanding, because C + + also has the corresponding FStream class, C++map container class, see C + + Map Introduction

(5) C + + There are relatively mature string class, inside the function is also most flexible, no can also be easily implemented Split,strim, etc., see c++string implementation

(6) Recently from the Internet, saw a very classic words, C + + wind FStream class + String class can also be very good processing text files, let us together to witness

Second: FStream's past life

(1) Introduction

Three file streams in C + +           &NBS P                          ,         &NB Sp          
    a----ofstream ofs ("filename", open mode),         and nbsp      B----ifstream IFS ("filename", open mode);     C----fstream FS ("filename", input open Mode | output open mode);    Three file streams are used for writing files, reading files, read and write files, generally in a B two way, because a file read and write at the same time using C mode.

Three file streams can be defined first, then open the file, take FStream as an example
FStream FS; Fs.open ("filename", input open Mode | output open mode);
where "open mode" can not be given. If not given, the default for Oftream is ios::out,iftream default to Ios::in

(2) File Open function

In C + +, the operation of the file is implemented through the subclass FStream (file stream) of the stream, so to manipulate the file in this way, the header file fstream.h must be added. The following is the process of this type of file operation one by one.
Open the file in the FStream class, there is a member function open (), which is used to open the file, and its prototype is:
void Open (const char* filename,int mode,int access);
Parameters:
FileName: The file name to open
Mode: How to open a file
Access: Open the properties of a file

(2) Open mode                                                                   

ios::out output Data Overwrite existing file (default write-in mode, file does not exist, created; if present, overwrite original content)
Ios::app output Data
to the end of the existing file (append the end of the write-on method, not overwrite the original content)
Ios::ate Open the file and move the file pointer to the end
ios::in Open file for input (default read open mode)
Ios::trunc existing content in the output file (default action for ios::out)
Ios::binary binary open for read and write

(3) file pointer positioning

Unlike the C file operation, the C + + I/O system manages two pointers that are associated with a file. One is a read pointer, which shows the position of the input operation in the file, and the other is the position of the write pointer, which is the next write operation. Each time the input or output is executed, the corresponding pointer changes automatically. therefore, the C + + file positioning is divided into read position and write location positioning,

The corresponding member functions are SEEKG () and SEEKP (), SEEKG () is the set read position, and SEEKP is the set write position. Their most common form is as follows:

IStream &SEEKG (Streamoff offset,seek_dir origin);
Ostream &SEEKP (Streamoff offset,seek_dir origin);

Streamoff, defined in iostream.h, defines the maximum value that offset offset can achieve, Seek_dir represents the base position of the move, and is an enumeration with the following values:

Ios::beg: File Start
Ios::cur: File Current Location
Ios::end: End of File


These two functions are typically used in binary files because the text file may be different from the expected value because the system interprets the characters.

Cases:
FILE1.SEEKG (1234,ios::cur);//Move the read pointer of the file backward from the current position by 1234 bytes
FILE2.SEEKP (1234,ios::beg);//Move the file's write pointer back 1234 bytes from the beginning of the file
FILE1.SEEKG ( -128,ios::end); Move the read pointer of a file forward 128 bytes from the end of the file

Note: A Chinese character occupies two bytes, and one letter occupies one byte.

(4) The relationship between FStream, stream, Ifstream,istream;ofstream,ostream, etc.



Three: the actual combat chapter

(1) Read Word by word; no write

READ: read by word, between the words with a space between the distinction (encountered space to think the end of the read), the output after the next read//read data from the file, Word by word//when used in this manner, we ' ll Get space-delimited bits of text from the file//but all of the whitespace it separated words (including newlines) was Lost.void READDATAFROMFILEWBW () {    ifstream fin ("data.txt");    string S;    cout << "*****start*******" << Endl;    While (Fin >> s)    {          cout << ' Read from File: ' << s << endl;    }    cout << "*****over*******" << Endl;}

(2) Read by line Fin.getline (Char*,n)

//read: Read by line, Reads the line into the character array, the lines are separated by carriage return//if we were interested in preserving whitespace,//we could read the file in Line-by-line using the    I/O getline () function.void Readdatafromfilelblintochararray () {ifstream fin ("data.txt", ios::in);//The default open mode is Ios::in    Ofstream fout ("OUT.txt", ios::out);//The default generation mode is ios::out const int line_length = 100;    Char Str[line_length];    Fout << "****chararray start******" << Endl;    cout << "****chararray start******" << Endl; FIN.SEEKG ( -20,ios::end);//-20 means moving forward 20 bytes from end, kanji two bytes; 20 means moving the pointer backwards while (Fin.getline (str,line_length)) {Fout &        lt;< str << Endl;    cout << "Read from File:" << str << "..." << endl;//****str inside itself contains a newline, what it looks like, what it is to save now}    Fout << "*****over*******" << Endl; cout << "*****over*******" << Endl;} 
(3) Read by line Fin.getline (fin,string)

Read by: Row by line read, the line read into the string, line with carriage return line to differentiate//if you want to avoid reading into character arrays,//you can use the C + + string Getli NE () function to read lines into Stringsvoid readdatafromfilelblintostring () {    ifstream fin ("data.txt", ios::in);// The default open mode is Ios::in    ofstream fout ("OUT.txt", ios::app);//Append to end of file to open    string s;    cout << "****start******" << Endl;    while (Getline (fin,s))    {        fout << s << endl;//ofstream is the default, if the file does not exist, the file is created first, and the newline is no longer present in the process of writing to the file. This is the same as the cout console output Oh ...        cout << "Read from File:" << s << endl;//****s with STR itself has no newline, which is the same as the original getline () function; What is the data like, What is now saved is what it looks like    }    fout << "*****over*******" << Endl;    cout << "*****over*******" << Endl;    Fout.close ();}
(4) Main function

#include <iostream> #include <fstream> #include <string> #include <cstdlib>//exit () function using namespace Std; Output empty line void Outputanemptyline () {      cout<< "\ n";}//Read with error detection//simply evaluating an I/O object in a Boolean Contex T return false//if any errors has occurredvoid readdatawitherrchecking () {    string filename = "DataFUNNY.txt"; 
   ifstream Fin (FILENAME.C_STR ());    if (!fin)    {          cout << "Error opening" << filename << "for input" << Endl;          Exit ( -1);}    } int main () {      READDATAFROMFILEWBW ();//Word-reading into string      outputanemptyline ();//output blank line      Readdatafromfilelblintochararray (); Read word-by-phrase into the character array      outputanemptyline ();//output blank line      readdatafromfilelblintostring ();//Read-in string      by word Outputanemptyline (); Output blank line      readdatawitherrchecking ();//Read     return 0 with detection;}
The following is the data format



(5) Summary

The first article, (write so much, with two sentences summed up it) recently from the Internet, see a very classic words, C + + wind FStream class + String class can also be very good processing text files;

The second, language is only a tool, itself does not have the merits and demerits of the points.


C++fstream file read/write (compared to Java)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.