C/C++ reads the float data in the txt file and stores it with a pointer

Read data in txt file in C language

The following is a simple sample code to demonstrate how to read data in a txt file in C language:

#include <stdio.h>

int main() {
    
    
    FILE *fp;
    char buffer[100];

    // 打开文件
    fp = fopen("example.txt", "r");

    // 如果文件打开失败,则输出错误信息并退出程序
    if (fp == NULL) {
    
    
        printf("无法打开文件!\n");
        return 1;
    }

    // 读取文件内容并输出到屏幕上
    while (fgets(buffer, 100, fp) != NULL) {
    
    
        printf("%s", buffer);
    }

    // 关闭文件
    fclose(fp);

    return 0;
}

In the above code, we have used fopen()the function to open the example.txt file and store it in fpthe pointer. If the file is opened successfully, use fgets()the function to read each line of data from the file, store it in a buffer buffer, and finally output it to the console.

After reading all the data, we use fclose()the function to close the file and release resources.

Note that fopen()the function will return if the file does not exist or cannot be opened NULL. In this case, we use a conditional statement to output an error message and exit the program.

C++ reads the data in the txt file

The following is a simple sample code that demonstrates how to read data in a txt file using C++:

#include <iostream>
#include <fstream>

int main() {
    
    
    std::ifstream file("data.txt");  // 打开文件
    int num;

    while (file >> num) {
    
      // 使用流提取运算符 >> 逐个读取整数
        std::cout << num << " ";  // 输出读取的整数
    }

    file.close();  // 关闭文件
    return 0;
}

In the above code, we used a variable of std::ifstreamtype fileto open a file called "data.txt". whileThe stream extraction operator is used in the loop >>to read one integer at a time until the file is read. Finally, file.close()close the file with .

Note that the above code assumes that the file contains only integers, and that each integer is separated by a space or newline. If the file contains other types of data or uses a different delimiter, the way it is read needs to be modified accordingly.

C++ reads the float data in the txt file and stores it in vector

Here is a basic sample code that can help you read floating point data stored in a text file:

#include <iostream>
#include <fstream>
#include <vector>

int main()
{
    std::ifstream infile("input.txt"); // 打开名为input.txt的文件

    std::vector<float> floatVector; // 创建一个vector,并将浮点数存储在其中

    float value;
    while (infile >> value) // 循环读取文件中的浮点数
    {
        floatVector.push_back(value);
    }

    infile.close(); // 关闭文件

    // 输出读取的浮点数
    for (int i = 0; i < floatVector.size(); i++)
    {
        std::cout << floatVector[i] << std::endl;
    }

    return 0;
}

In this example, we open input.txtthe file named , and create a vector to store floating point numbers. In the while loop, we read the floats from the file and add them to the vector. Finally, we close the file and use a for loop to output the float we read.

C++ reads the float data in the txt file and stores it with a pointer

The following is a sample code that reads float data from a txt file and stores it using a pointer:

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

int main()
{
    // 打开文件
    ifstream file("input.txt");
    if (!file.is_open())
    {
        cout << "无法打开文件" << endl;
        return 1;
    }

    // 统计文件中浮点数的数量
    int count = 0;
    float tmp;
    while (file >> tmp)
    {
        count++;
    }

    // 分配内存
    float* data = new float[count];

    // 回到文件开头并读取数据
    file.clear();
    file.seekg(0, ios::beg);
    int i = 0;
    while (file >> tmp)
    {
        data[i] = tmp;
        i++;
    }

    // 关闭文件
    file.close();

    // 输出读取的数据
    for (int i = 0; i < count; i++)
    {
        cout << data[i] << endl;
    }

    // 释放内存
    delete[] data;

    return 0;
}

In the above code, first open the txt file and check whether it is successfully opened, and then count the number of floating-point numbers in the file. Then newallocate memory by keyword, and then go back to the beginning of the file for actual data reading. Finally output the data, close the file and free the memory.

Guess you like

Origin blog.csdn.net/qq_39506862/article/details/130893599