Home > Net >  C check is any character in a floating point array from txt file
C check is any character in a floating point array from txt file

Time:10-19

I am working on a program to read floating point number from an txt file and store in array, and I need to check are there any invalid input like character. My code is:

int main() {
    string line;
    ifstream myfile("data.txt");
    int size;
    float* result;
    if (myfile.is_open()) {
        getline(myfile, line);
        size = stoi(line);
        result = new float[size];
        for (int i = 0; i < size; i  ) {
            myfile >> result[i];
            /*if ( (isdigit(arr[i])==0) ){
                    cout << "Invaild input." << endl;
                    return 0;
            }*/
        }
        myfile.close();
    }
    else {
        return 0;
    }
}

The first line of the txt file is the size of the array and the second line is the contents like

5 //size
1 -2 9.2 4.7 -5.2  //content

How can I check that is there any character exist in the array like 1 -2 B 4.7 -5.2 //Invalid input ? I try the isdigit function but it fail.

CodePudding user response:

If you get an invalid input, reading will fail, and you can check this in the usual manner.

if (myfile >> result[i])
{
    // Handle success.
}
else
{
    // Handle failure.
}

CodePudding user response:

I have given 2 solutions. One uses built in arrays and other uses std::vector.

Solution 1: Using built in array

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
int main()
{
    
    std::string line;

    std::ifstream inFile("input.txt");
   
   //in case of using array, size must be fixed and predetermined 
   int arr[120] = {0}; //you can choose size according to your needs
    if(inFile)
    {
        int i = 0;//this variable will be used to add element into the array
        int count = 0;
        while(getline(inFile, line, '\n'))        
        {
            
            
            
            std::istringstream s(line);
            
            
            while(s >> i || !s.eof()) {
                if(s.fail()) 
                {
                    s.clear();
                    std::string temp;
                    s >> temp;
                    continue;
                 }
                else 
                {
                    arr[count] = i;
                      count;
                }

}
            
            
        }
    }
    else 
    {
        std::cout<<"file could not be read"<<std::endl;
    }
    inFile.close();
    
    for(int i: arr)
    {
        std::cout<<"elem: "<<i<<std::endl;
    }
   
    return 0;
}

              

The output of solution 1 can be seen here.

Solution 2: Using std::vector

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
int main()
{
    
    std::string line;;

    std::ifstream inFile("input.txt");
    
   std::vector<int> vec;
    if(inFile)
    {
        int i = 0;//this variable will be used to add element into the vector
        
        while(getline(inFile, line, '\n'))        
        {
            
           
            
            std::istringstream s(line);
           
            while(s >> i || !s.eof()) {
                if(s.fail()) 
                {
                    s.clear();
                    std::string temp;
                    s >> temp;
                    continue;
                 }
                else 
                {
                    vec.push_back(i);
                }

            }
            
            
        }
    }
    else 
    {
        std::cout<<"file could not be read"<<std::endl;
    }
    inFile.close();
    
    for(int i: vec)
    {
        std::cout<<"elem: "<<i<<std::endl;
    }
   
    return 0;
}

The ouput of solution 2 can be seen here.

Important Note

The advantage of using std::vector over built in array(in this case) is that you don't have know the size of the vector beforehand. So it is preferable because you don't know how many integers are there in the input.txt file. std::vector can handle this correctly. But when using built in arrays you must know/specify the size of the array beforehand. This in turn means you must know beforehand how many integers are there in the input.txt, which is not practical.

  • Related