C + + foundation -- read the file line by line and match the characters

Technical background

Online shopping coupons www.cqfenfa.com com

If you are used to python, you are unfamiliar with other languages. However, Python is often limited in performance. Here we try to realize the function of file IO through C + + to see if it can perform better than python. For the implementation of similar functions of python, you can refer to this blog.

C + + read file

First, we construct a txt file for testing. For example, the following file is called mindspore txt file (the reason for this name is that we are studying mindspot recently, so the most convenient data is the api document of mindspot):

MindSpore Python API
MindSpore Python API
mindspore
mindspore.common.initializer
mindspore.communication
mindspore.compression
mindspore.context
mindspore.dataset
mindspore.dataset.config
mindspore.dataset.text
mindspore.dataset.transforms
mindspore.dataset.vision
mindspore.explainer
mindspore.mindrecord
mindspore.nn
mindspore.numpy
mindspore.nn.probability
mindspore.ops
mindspore.profiler
mindspore.train
MindArmour Python API
mindarmour
mindarmour.adv_robustness.attacks
mindarmour.adv_robustness.defenses
mindarmour.adv_robustness.detectors
mindarmour.adv_robustness.evaluations
mindarmour.fuzz_testing
mindarmour.privacy.diff_privacy
mindarmour.privacy.evaluation
mindarmour.privacy.sup_privacy
mindarmour.utils
MindSpore Hub Python API
mindspore_hub
MindSpore Serving Python API
mindspore_serving
MindQuantum Python API
mindquantum

Then construct a C + + code to read the file line by line, save the obtained line string to strline through the getline function, and output one line each time. Since the while loop is used here, the index scheme is adopted to set a condition for jumping out of the loop, and only a specific line range is read:

// iofile.cpp
#include <iostream>
#include <fstream>
#include <string>

int main()
{
    using namespace std;
    string filename="mindspore.txt";
    ifstream fin(filename.c_str());
    int index = 0;
    string strline;
    while (getline(fin, strline) && index < 20)
    {
        cout << strline << endl;
        index ++;
    }
    fin.close();
    cout << "Done!
";
    return 0;
}

After reading, remember to use close() to close the file. The execution result of the above code is as follows:

dechin@ubuntu2004:~/projects/gitlab/dechin/$ g++ iofile.cpp 
dechin@ubuntu2004:~/projects/gitlab/dechin/$ ./a.out
MindSpore Python API
MindSpore Python API
mindspore
mindspore.common.initializer
mindspore.communication
mindspore.compression
mindspore.context
mindspore.dataset
mindspore.dataset.config
mindspore.dataset.text
mindspore.dataset.transforms
mindspore.dataset.vision
mindspore.explainer
mindspore.mindrecord
mindspore.nn
mindspore.numpy
mindspore.nn.probability
mindspore.ops
mindspore.profiler
mindspore.train
Done!

The g + + version we use here is 9.3.0:

dechin@ubuntu2004:~/projects/gitlab/dechin/$ g++ --version
g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Because the above case reads the contents of the first 20 lines, under Linux, we can also view the contents of the first 20 lines through head:

dechin@ubuntu2004:~/projects/gitlab/dechin/$ head -n 20 mindspore.txt 
MindSpore Python API
MindSpore Python API
mindspore
mindspore.common.initializer
mindspore.communication
mindspore.compression
mindspore.context
mindspore.dataset
mindspore.dataset.config
mindspore.dataset.text
mindspore.dataset.transforms
mindspore.dataset.vision
mindspore.explainer
mindspore.mindrecord
mindspore.nn
mindspore.numpy
mindspore.nn.probability
mindspore.ops
mindspore.profiler
mindspore.train

After comparison, it is found that the two results are consistent.

C + + string matching

Let's imagine such a test case. In the above txt text, we want to mark the line with the character context to make it different from other lines. At this time, you need to use the string matching function of C + +, and its format is string Find ("context") returns an identification code, which is used to mark whether or where the character exists. If the character does not exist, the returned result is equivalent to string::npos. According to this idea, we define a Boolean value. If we encounter the context character in the retrieval process, we output 1, otherwise 0. The specific code is as follows:

// iofile.cpp
#include <iostream>
#include <fstream>
#include <string>

int main()
{
    using namespace std;
    string filename="mindspore.txt";
    ifstream fin(filename.c_str());
    int index = 0;
    string strline;
    while (getline(fin, strline) && index < 20)
    {
        bool exists = strline.find("context") == string::npos;
        cout << strline << '	' << !exists << endl;
        index ++;
    }
    fin.close();
    cout << "Done!
";
    return 0;
}

The execution result of the above code is as follows:

dechin@ubuntu2004:~/projects/gitlab/dechin/$ g++ iofile.cpp && ./a.out
MindSpore Python API    0
MindSpore Python API    0
mindspore       0
mindspore.common.initializer    0
mindspore.communication 0
mindspore.compression   0
mindspore.context       1
mindspore.dataset       0
mindspore.dataset.config        0
mindspore.dataset.text  0
mindspore.dataset.transforms    0
mindspore.dataset.vision        0
mindspore.explainer     0
mindspore.mindrecord    0
mindspore.nn    0
mindspore.numpy 0
mindspore.nn.probability        0
mindspore.ops   0
mindspore.profiler      0
mindspore.train 0
Done!

We can notice that a 1 is output at the end of the line containing context, and 0 is output at the end of other lines

C + + runtime statistics

One of the functions we often use in python is to import time Time () to record the time, and then calculate the difference between the two times, you can get the exact running time of a program. A similar usage in C + + is clock_t, to facilitate testing, we encapsulate the above code into a reader function, then call it in the main function and run the statistics time:

// iofile.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <time.h>
using namespace std;
int reader()
{
    string filename="mindspore.txt";
    ifstream fin(filename.c_str());
    int index = 0;
    string strline;
    while (getline(fin, strline) && index < 20)
    {
        bool exists = strline.find("context") == string::npos;
        cout << strline << '	' << !exists << endl;
        index ++;
    }
    fin.close();
    cout << "Done!
";
    return 0;
}
int main()
{
    clock_t start, end;
    start = clock();
    reader();
    end = clock();
    cout << "The time cost is: " << double(end-start)/CLOCKS_PER_SEC << "s" << endl;
}

The execution result of the above code is as follows:

dechin@ubuntu2004:~/projects/gitlab/dechin/$ g++ iofile.cpp && ./a.out
MindSpore Python API    0
MindSpore Python API    0
mindspore       0
mindspore.common.initializer    0
mindspore.communication 0
mindspore.compression   0
mindspore.context       1
mindspore.dataset       0
mindspore.dataset.config        0
mindspore.dataset.text  0
mindspore.dataset.transforms    0
mindspore.dataset.vision        0
mindspore.explainer     0
mindspore.mindrecord    0
mindspore.nn    0
mindspore.numpy 0
mindspore.nn.probability        0
mindspore.ops   0
mindspore.profiler      0
mindspore.train 0
Done!
The time cost is: 0.000245s

The output time indicates that the total running time of this function is 0.2ms.

Summary summary

This paper briefly introduces three basic operations in C + +: reading file content line by line, string matching and running time statistics, and realizes these three basic functions through a simple example. Compared with python, the amount of code written in C + + must be more, but considering the efficiency gain that C + + may bring, we should also understand its basic usage and function implementation.

Copyright notice

The first link of this article is: https://www.cnblogs.com/dechinphy/p/cppio.html
Author ID: DechinPhy
For more original articles, please refer to: https://www.cnblogs.com/dechinphy/

Added by namasteaz on Wed, 09 Feb 2022 13:05:23 +0200