1

Based on ifstream I am using the following function to read filename.txt file

//reading from text file
static std::vector<double> vec;
double a[18]; //values got read from txt file
int i = 0;
void readDATA()
{
    double value;
    std::ifstream myFile;
    myFile.open("filename.txt", std::ios::app);
    if (myFile.is_open())
    {
        std::cout << "File is open." << std::endl;
        while (myFile >> value)
        {
            vec.push_back(value);
            std::cout << "value is " << value << std::endl;
            a[i] = value;
            std::cout << "a" << i << "=" << a[i] << std::endl;
            i = i + 1;
        }
        myFile.close();
    }
    else
        std::cout << "Unable to open the file";
}

it works correctly Below is the content of filename.txt file:

0 0 40 45 15
0 1 40 -45 10
0 0 180 90 15

Could you please help me in modifying the function such that I can read the same .txt file with comma separation between the elements?

crackaf
  • 492
  • 2
  • 11
Iko-SOF
  • 77
  • 1
  • 7

2 Answers2

1

You want to read number followed optionally by colon?

Define class that will handle this "task":

struct Number 
{
   double value;
   operator double() const { return value; }
}; 
std::istream& operator >>(std::istream& is, Number& number)
{
     is >> number.value;
     // fail istream on anything other than ',' or whitespace
     // end reading on ',' or '\n'
    for (char c = is.get();; c = is.get()) {
        if (c == ',' or c == '\n')
            break;
        if (std::isspace(c))
            continue;

        is.setstate(std::ios_base::failbit);
        break;
    }
     return is;
}

Then in your code - replace double value; with Number value;

Demo

PiotrNycz
  • 23,099
  • 7
  • 66
  • 112
  • I want to read number followed by comma from the txt file – Iko-SOF Dec 11 '20 at 15:05
  • 0, 0, 40, 45, 15 \n 0, 1, 40, -45, 10 \n 0, 0, 180, 90, 15 \n – Iko-SOF Dec 11 '20 at 15:05
  • @Iko-SOF - I wrote the code w/o compiling, running or verifying it. Now it should work. – PiotrNycz Dec 11 '20 at 15:36
  • Thanks for the demo. Actually, what I mean by \n is new line. To be more clear plz find in the link the txt file which I am trying to read https://filebin.net/18fqrqlx6421o5vv It's important to me to modify the code which I already provided in the question to let it read the file uploaded in the link – Iko-SOF Dec 11 '20 at 15:42
  • Everything is in my answer. Add the code I provided - and "Then in your code - replace double value; with Number value;" – PiotrNycz Dec 11 '20 at 17:39
  • Two things to clarify from your comments. ifstream is a istream. So, if I wrote function for istream - it will work for ifstream as well. And, yes I know you mean \n - \n in strings mean the same as in txt file. Once again use my code in yours. – PiotrNycz Dec 12 '20 at 09:03
  • Could you please help me in other two things: First- having the ability to assign manual value to a[i] if i was not available in the .txt file (e.g. first line in text file is 0, 0, 40, , 15 the forth element in nothing and in this case I want to assign it manually in the code to be for instance 70). Second- letting the code read not only numbers but also strings (dealing with numbers and strings for example 0,0,8,"turn_right",4) – Iko-SOF Dec 12 '20 at 12:26
  • If you have another problem - ask another question. On SO comments section cannbot be too long or replace Q/A. You might refer to this Q in your new Q. And - if you found this my answer resolwing your current problem - you can accept/upvote. – PiotrNycz Dec 12 '20 at 22:41
  • Thanks, I will refer it in a new question – Iko-SOF Dec 13 '20 at 11:25
  • https://stackoverflow.com/questions/65275129/using-ifstream-to-assign-default-value-if-it-was-not-declared-in-txt-file – Iko-SOF Dec 13 '20 at 11:37
  • could you please take a look at this question it's a continue to this one @PiotrNycz https://stackoverflow.com/questions/65645056/how-to-read-n-values-from-txt-file-using-c – Iko-SOF Jan 09 '21 at 16:40
0

something like this you can try

std::string data;
while(std::getline(myFile, data)) {
    std::istringstream ss(data);
    std::string token;

    while(std::getline(ss, token, ','))
        double d = std::stod(token);
}
Harry
  • 2,177
  • 1
  • 19
  • 33