Home > Back-end >  Reading numbers from a text file with letters and whitespace
Reading numbers from a text file with letters and whitespace

Time:03-10

I am having trouble with the formatting of my C assignment. I am working on a File I/O assignment and I have a text file with letters, numbers and whitespace. I want my program to read the numbers from the text file and display in the same format as the text file. Right now, my code is outputting the numbers but as a line instead of number values.

Here is the text file:

dsfj  kdf     87  98.5 4vb
jfkd 9            jfkds000    94.3hjf
       98.4    jd84.    kfd

Here is the desired output:

We found the following magic numbers in test2.txt:
87 98.5 4 9 0 94.3 98.4 84 

Here is my code:

#include <iostream>
#include <fstream>

using namespace std;

int main() {
    char ans;
   
    cout << "Welcome to Number Finder!\n";
    cout << "============================\n";
    do{
        char in_file_name[20];
        char c;
        ifstream in_stream;
        
        cout << "Which file do you want us to find numbers at?\n";
        cin >> in_file_name;
        cout << endl;
        cout << "We found the follow magic numbers in " << in_file_name << ": \n";

        in_stream.open(in_file_name);
        if(in_stream.fail()){
            cout << in_file_name << " is not found.\n";
        }
        in_stream.get(c);
        while (!in_stream.eof()){
            if(isdigit(c)){
                cout << fixed;
                cout.precision(2);
                cout << c << " ";
              }
             in_stream.get(c);
         }
        double num;
        while(in_stream >> num){
            cout << fixed;
            cout.precision(2);
            cout << num << " ";
        }
        cout << endl;
        cout << "-----";
        cout << endl;
        cout << "Do you want to process another file? \n";
        cin >> ans;
        in_stream.close();
    } while (ans== 'Y' || ans=='y');
    cout << "Thank you for using the Number Finder! Bye for now!";
    return 0;
}

CodePudding user response:

Assuming that your goal is not to retain the accuracy of the double values in the file, one way to do this is to read the line in as a string, and remove any characters that are not digits, except whitespace and the ..

Once you do that, then it's very simple using std::istringstream to output the values:

#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <cctype>

int main()
{
    std::string line;
    // Read the entire line
    while (std::getline(std::cin, line))
    {
        // erase all non-digits, except for whitespace and the '.' 
        line.erase(std::remove_if(line.begin(), line.end(), 
                   [](char ch) { return !std::isdigit(ch) && !std::isspace(ch) && ch != '.'; }), line.end());

        // Now use the resulting line and read in the values
        std::istringstream strm(line);
        double oneValue;
        while (strm >> oneValue)
           std::cout << oneValue << " ";
    }
}

Output:

87 98.5 4 9 0 94.3 98.4 84 

Live Example

  • Related