Home > Backend Development > C++ > body text

Understanding file streams in C++

王林
Release: 2023-08-21 23:15:16
Original
1471 people have browsed it

The file stream in C is a convenient data input and output method. Data in the file can be read and written through the file stream. In C, file streams mainly involve the iostream library and the fstream library.

The iostream library is mainly responsible for console input and output, while the fstream library is responsible for file input and output. The fstream library is included in the iostream library, so we only need to include one of the header files or .

In C, you need to use a stream object to operate a file, and a file stream is a stream object that is associated with a file. You can read or write to the file through the file stream object. Enter data. There are two types of file streams: reading file streams (ifstream) and writing file streams (ofstream), both of which are derived from the base file stream (fstream).

It should be noted that if you want to perform file stream read and write operations, the file must be opened first. Files can be opened using the open() function. Generally speaking, after the file is opened successfully, we need to perform a read or write operation. After the operation is completed, we need to use the close() function to close the file stream object.

Next, let’s take a look at several common operations of file streams:

  1. Open a file

In C, the ways to open a file are: Two types: one is to use the fstream library object, and the other is to use the C language function library.

Use fstream library:

#include <fstream>
using namespace std;

int main() {
  ofstream fileOut; //写入文件流对象
  fileOut.open("test.txt"); //以写模式打开文件
  fileOut.close(); //关闭文件
  ifstream fileIn; //读取文件流对象
  fileIn.open("test.txt"); //以读模式打开文件
  fileIn.close(); //关闭文件
  return 0;
}
Copy after login

Use C function library:

#include <stdio.h>

int main() {
  FILE* fp; //文件指针
  fp = fopen("test.txt", "w"); //以写模式打开文件
  fclose(fp); //关闭文件
  fp = fopen("test.txt", "r"); //以读模式打开文件
  fclose(fp); //关闭文件
  return 0;
}
Copy after login
  1. Write files

You can use objects of the ofstream class Implement writing data to the file. The "<<" operator can be used to write data. When writing, you can add strings, characters, values ​​or variables to the left or right of the operator.

#include <fstream>
using namespace std;

int main() {
  ofstream fileOut; //写入文件流对象
  fileOut.open("test.txt"); //打开文件
  if (fileOut.is_open()) { //判断文件是否成功打开
    fileOut << "Hello world"; //写入数据
    fileOut.close(); //关闭文件
    return 0;
  } else {
    return -1;
  }
}
Copy after login
  1. Reading files

Using objects of the ifstream class can read data from files. The "<<" operator can also be used to read data. When reading, the data can be read into already defined variables.

#include <fstream>
#include <iostream>
using namespace std;

int main() {
  ifstream fileIn; //读取文件流对象
  fileIn.open("test.txt"); //打开文件
  if (fileIn.is_open()) {
    char ch; //定义变量用来存放读取的字符
    while (fileIn >> ch) { //逐个读取字符
      cout << ch; //输出读取的字符
    }
    fileIn.close(); //关闭文件
    return 0;
  } else {
    return -1;
  }
}
Copy after login

The above is the basic knowledge of file streams in C. Through the read and write operations of the file stream, we can process the data in the file more conveniently to meet actual programming needs.

The above is the detailed content of Understanding file streams in C++. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template