C++ Read in From File Int and String

Read Int From a File in C++

  1. Utilize while Loop and >> Operator to Read Int From File in C++
  2. Apply while Loop and >> Operator Combined With push_back Method to Read Int From File
  3. Don't Use while Loop and eof() Method to Read Int From File

This commodity will explain several C++ methods of how to read int information from a file.

In the post-obit instance programs, nosotros assume a text file is named input.txt, which contains space-separated integer numbers on multiple lines. Note that each sample lawmaking checks if this filename gets associated with the actual file stream and prints the corresponding failure message in case of error.

  • Content of input.txt:
              123 178 1289 39 90 89 267 909 23 154 377 34 974 322                          

Use while Loop and >> Operator to Read Int From File in C++

This method uses the while loop to iterate the process until the EOF (end of the file) is reached and store each integer in the number variable. And so, we tin can print each number to console in the loop trunk.

              #include <iostream> #include <fstream>  using std::cout; using std::cerr; using std::endl; using std::string; using std::ifstream;  int primary() {     string filename("input.txt");     int number;      ifstream input_file(filename);     if (!input_file.is_open()) {         cerr << "Could not open the file - '"              << filename << "'" << endl;         return EXIT_FAILURE;     }      while (input_file >> number) {         cout << number << "; ";     }     cout << endl;     input_file.shut();      return EXIT_SUCCESS; }                          

Output:

              123; 178; 1289; 39; 90; 89; 267; 909; 23; 154; 377; 34; 974; 322;                          

Employ while Loop and >> Operator Combined With push_back Method to Read Int From File

Equally another alternative, we can become each integer from the file, shop it in the number variable like in the previous case, and then push them to the int vector in each iteration. Notice that this scenario includes another for loop to imitate the more than practical system where the elements of the stored vector numbers demand to be manipulated.

              #include <iostream> #include <fstream> #include <vector>  using std::cout; using std::cerr; using std::endl; using std::cord; using std::ifstream; using std::vector;  int main() {     string filename("input.txt");     vector<int> numbers;     int number;      ifstream input_file(filename);     if (!input_file.is_open()) {         cerr << "Could non open up the file - '"              << filename << "'" << endl;         return EXIT_FAILURE;     }      while (input_file >> number) {         numbers.push_back(number);     }      for (const auto &i : numbers) {         cout << i << "; ";     }     cout << endl;     input_file.shut();      render EXIT_SUCCESS; }                          

Output:

              123; 178; 1289; 39; 90; 89; 267; 909; 23; 154; 377; 34; 974; 322;                          

Don't Use while Loop and eof() Method to Read Int From File

One might consider using the eof() member function as a while loop condition to approach the same problem. Unfortunately, that may lead to an extra iteration. Since the part eof() just returns truthful if the eofbit is set, it might lead to the iteration where uninitialized is modified. This scenario is demonstrated past the code sample below:

              #include <iostream> #include <fstream> #include <vector>  using std::cout; using std::cerr; using std::endl; using std::string; using std::ifstream; using std::vector;  int main() {     cord filename("input.txt");     vector<int> numbers;      ifstream input_file(filename);     if (!input_file.is_open()) {         cerr << "Could not open up the file - '"              << filename << "'" << endl;         return EXIT_FAILURE;     }      while (!input_file.eof()) {         int tmp;         input_file >> tmp;         numbers.push_back(tmp);     }      for (const auto &i : numbers) {         cout << i << "; ";     }     cout << endl;     input_file.close();      return EXIT_SUCCESS; }                          

Output:

              123; 178; 1289; 39; ninety; 89; 267; 909; 23; 154; 377; 34; 974; 322;                          

Write for us

DelftStack articles are written past software geeks like you. If you also would similar to contribute to DelftStack by writing paid articles, you tin check the write for us page.

Related Commodity - C++ File

  • Read File Into String in C++
  • Write to File in C++
  • Ezoic

    snellmanis1986.blogspot.com

    Source: https://www.delftstack.com/howto/cpp/read-int-from-file-cpp/

    0 Response to "C++ Read in From File Int and String"

    Post a Comment

    Iklan Atas Artikel

    Iklan Tengah Artikel 1

    Iklan Tengah Artikel 2

    Iklan Bawah Artikel