Read and write for "C + +" files

Source: Internet
Author: User

C + + file read and write feel is particularly difficult compared to other languages, compared to the "Python" file read and write operations (click the Open link), "PHP" to make Notepad your control variable of the console (click to open the link) and "Java" Input and output and JDK1.5 after the new string StringBuilder "(Click to open the link), C + + is the biggest feature is the file input stream and output stream separate, and the most critical is that there is no string variable, in its file read method variable limited to the character array char[], Need to take advantage of a two-dimensional character array char[][] to get it done.

At the same time, because of the different compiler, C + + in the case you do not initialize the variable will not be a warning, you must also use the Memset method, the two-dimensional character array to initialize, so that the determination of NULL conditions consistent.

Below, use one of the following procedures to illustrate the problem:


If the f:\1.txt does not exist, the file is created by itself, and then the user continuously enters data until the exit is entered and ends.

The difficulty of the program is mainly to read the contents of the file into a two-dimensional array of C + +, the basic idea is as follows:


The code is as follows:

#include <iostream> #include <fstream>using namespace Std;int main () {char input[255];//used to accept user-entered content Char An array of filecontent[100][255];//used to pick up the contents of a file, equivalent to a one-dimensional array of strings. memset (filecontent, ' filepath= ', sizeof (filecontent));//The entire array must be initialized, otherwise the different compilers initialize the values differently, causing the output below to be judged char* " F:\\1.txt ";//This avoids the introduction of the string header file, using the string type while (1) {//reading the file to C++ifstream infile (filepath,ios::in|ios::_nocreate); /vs written ios::_nocreate,vc6 is Ios::nocreateif (!infile) {//if the input stream fails to initialize or the file does not exist cout<<filepath<< "does not exist and has been created for you! "<<endl;ofstream filecreate (Filepath,ios::_noreplace);//used to create a file-specific output stream, vs written ios::_noreplace,vc6 is iOS:: Noreplaceif (filecreate) {filecreate.close ();//people Walking with doors}else{cerr<< "something went wrong! "<<endl;exit (1);}} else{for (int i=0;! Infile.eof (); i++) {//the end condition of this loop is that the input stream encounters the end of the file, that is, the data infile.getline (filecontent[i],255) in the F:\1.txt is read;// Use the Getline method to read one line of a row, each line as a string, placed in the second dimension of Filecontent}cout<<filepath<< "the present content is as follows:" &LT;&LT;ENDL;} Infile.close ();//After reading, the person walks with the door for (int i=0;filecontent[i][0]!= ' s; i++) {//print end condition is encountered in the filecontent array withoutThe No. 0 field is not filled with a blank line Cout<<filecontent[i]<<endl;} cout<< "Enter something to" <<filePath<< ", enter exit; "<<endl;cin>>input;if (!strcmp (Input," exit; ")) {//If the user enters a string with exit; equal, strcmp (input, "exit;") Then return 0, enter the conditional structure, terminate the program return 0;//This is the main function with the int type as the return value, return 0 of the benefits, to set the terminal of the program, of course, if void is the return value of the main function exit (0); You can do this}//c++ Output to File Ofstream outfile (filepath,ios::ate|ios::_nocreate);//ios::ate can place the pointer directly at the end of the file. Ios::app bad, to cooperate with OUTFILE.SEEKP (0,ios::end) to get the file to the end of the file, the default is the file header if (!outfile) {cerr<< "Error! "<<endl;exit (1);} else{outfile<<input<< ' \ n ';//The output stream of the file output by the user input}outfile.close ();//people Walking with doors}}

The comments have been made more clear. Don't repeat it here.

The only thing I want to say

Involved in file operations, must be introduced at the beginning <fstream>

Using the Getline method, C + + can put the content of the specified length in a row, here is 255, read into the one-dimensional array you specify, encounter enter and other line characters are automatically terminated, regardless of whether the length of the read line is accumulated to 255, while you move the cursor to the next line.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Read and write for "C + +" files

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.