C + + print files -- files and streams

Posted by scottb1 on Thu, 23 Sep 2021 09:12:41 +0200

Files and streams

Due to the needs of the project, it is required to output the image data to the file after processing. In the file, different kinds of data will be displayed in table layout. At first, it was not easy to do, and then try it

1. Basic knowledge


To process files in C + +, you must include header files and in the C + + source code.

Open file
Before reading information from or writing information to a file, the file must be opened. Both ofstream and fsstream objects can be used to open files for write operations. If you only need to open files for read operations, use i fstream objects.
The following is the standard syntax for the open() function, which is a member of fsstream, i fstream, and ofstream objects.

void open(const char *filename,ios::openmode mode);

Here, the first parameter of the open() member function specifies the name and location of the file to be opened, and the second parameter defines the mode in which the file is opened.

Combine the above two or more modes. For example, if you want to open a file in write mode and want to truncate the file in case the file already exists, you can use the following syntax:

ofstream outfile;
outfile.open("file.dat",ios::out|ios::trunc);

Similarly, if you want to open a file for reading and writing, you can use the following syntax:

ifstream afile;
afile.open("file.dat",ios::out|ios::in);

Close file
When the C + + program terminates, it will automatically close, refresh all streams, free all allocated memory, and close all open files. But programmers should make it a good habit to close all open files before the program terminates.

The following is the standard syntax for the close() function, which is a member of fsstream, i fstream, and ofstream objects.

void close();

write file
In C + + programming, we use the stream insertion operator (< <) to write information to the file, just as we use this operator to output information to the screen. The only difference is that here you use ofstream or fsstream objects instead of cout objects.

read file
In C + + programming, we use the stream extraction operator (> >) to read information from a file, just as we use this operator to enter information from the keyboard. The only difference is that here you use i fstream or fsstream objects instead of cin objects.

2. Read & write instance

The following C + + program opens a file in read-write mode. After writing the user input information to the file afile.dat, the program reads the information from the file and outputs it to the screen:

#include<iostream>
#incldue<fstream>
using namesapce std;;

int main()
{
    char data[100];
    
    //Open file in write mode
    ofstream outfile;
    outfile.open("afile.dat");
    cout<<"writing to the file"<<endl;
    cout<<"Enter your name:";
    cin.getline(data,100);
    
    //Write user input data to the file
    outfile<<data<<endl;

    cout<<"Enter your age:";
    cin>>data;;
    cin.ignore();

    //Write the user input data to the file again
    outfile<<data<<endl;

    //Close open files
    outfile.close();

    //Open file in read-only mode
    ifstream infile;
    infile.open("afile.dat");
    cout<<"Reading from the file"<<endl;
    infile>>data;

    //Write data on screen
    cout<<data<<endl;

    //Read the data from the file again and display it
    infile>>data;
    cout<<data<<endl;

    //Close open files
    infile.close();

    return 0;
}

The above code output is:

$./a.out
Writing to the file
Enter your name: Zara
Enter your age: 9
Reading from the file
Zara
9

In the above example, additional functions of the cin object are used. For example, the getline() function reads a line from the outside, and the ignore() function ignores the redundant characters left by the previous read statement.

File location pointer
Both istream and ostream provide member functions for relocating file location pointers. These member functions include seekg ("seek get") for istream and seekp ("seek put") for ostream.

The parameters of seekg and seekp are usually a long integer. The second parameter can be used to specify the lookup direction. The search direction can be ios::beg (by default, locate from the beginning of the stream), ios::cur (locate from the current position of the stream), or ios::end (locate from the end of the stream).

The file location pointer is an integer value that specifies the number of bytes from the start of the file to the location of the pointer. The following is an example of locating the "get" file location pointer:

// Locate the nth byte of fileObject (assuming ios::beg)
fileObject.seekg( n );
 
// Move the file read pointer back n bytes from the current position of fileObject
fileObject.seekg( n, ios::cur );
 
// Move the read pointer of the file back n bytes from the end of fileObject
fileObject.seekg( n, ios::end );
 
// Navigate to the end of fileObject
fileObject.seekg( 0, ios::end );

Topics: C++ iOS data structure Visual Studio Code