0

I need to read with C++ a file with extension .wdx (Mathematica provided). I've been able to get the file as a .dat and .txt obtaining something like this minimal example (dummyValues.dat):

{5.12, 0.1, 0.25}   {{0.10, 4, 3, 2, 1, 1.44, 10.2}}       {11.1, 12.2, 13.3, 14.4, 15.5, 16.6} X

As you can see it's a mess with blank spaces and {, } symbols. I'm only interested on getting a file with numbers (decimal is indicated with dot, '.'). How can I do this through the .wdx file or the .dat?


I have tried this

#include<iostream>
#include<cmath>
#include<string>
#include"complejos.h"//'using namespace std;' command is in here
#include<fstream>
#include<iomanip>

int main() {

double dato;
char aux;
ifstream benchmarks;

benchmarks.open("dummyValues.dat");

if (benchmarks.is_open()) {//first if

  benchmarks.get(aux);

  while (aux != "X") {

    if (aux == "{" || aux == "}" || aux == "," || aux == " ") {
      benchmarks.get(aux);
    } else {
      benchmarks>>dato;
      cout<<dato;
      benchmarks.get(aux);
    };
  };//end of while

} else {
  cout<<"ERROR: couldnt open the file"<<endl;
};//end of first if



benchmarks.close();

return 0;
}; //end of main

But I get this error:


error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
   while (aux != "X") {
                 ^~~
 error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
     if (aux == "{" || aux == "}" || aux == "," || aux == " ") {
                ^~~
 error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
     if (aux == "{" || aux == "}" || aux == "," || aux == " ") {
                              ^~~
 error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
     if (aux == "{" || aux == "}" || aux == "," || aux == " ") {
                                            ^~~
error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
     if (aux == "{" || aux == "}" || aux == "," || aux == " ") {

Vicky
  • 105
  • 4
  • 3
    It seems that the `{` and `}` are used to group the numbers. Are you sure that you want to throw away this information? If you are, then you can go through the file one character at a time using [`fgetc`](https://en.cppreference.com/w/c/io/fgetc) until you encounter a numerical digit, then read all characters into a memory buffer until you encounter a character that is not part of the number, and then use the functon [`strtod`](https://en.cppreference.com/w/c/string/byte/strtof) on that memory buffer. Repeat until you reach the end of the file. At least that is how I would do it in C. – Andreas Wenzel May 07 '20 at 23:55
  • @AndreasWenzel I'm not getting hoe to use those functions. I have tried something new, as you can see in the edited post, but I got a weird error. Can you tell me why that is happening and how to solve it? – Vicky May 08 '20 at 00:24
  • 1
    There is a big difference between writing `'X'` and `"X"`. Writing `'X'` will give you the character code for the character `X`, which is 88 ([see ASCII table](http://www.asciitable.com/)). However, writing `"X"` will give you a pointer to a string literal. This is not what you want. You just want to compare the character codes, so you must use `'X'`. The same applies for `'{'`, `'}'`, `','` and `' '`. – Andreas Wenzel May 08 '20 at 00:44
  • 1
    I'm afraid that the line `benchmarks>>dato;` will not work, because when you reach that line, you will already have extracted the first character of the number from the input stream. Therefore, you may want to consider calling [`unget`](https://en.cppreference.com/w/cpp/io/basic_istream/unget) to put that character back onto the input stream before calling `benchmarks>>dato;` – Andreas Wenzel May 08 '20 at 00:58
  • @AndreasWenzel Thank you very much for your help, now it works perfectly fine. I changed ```" "``` by ```' '``` and introduced ```benchmarks.unget();``` command before ```benchmarks>>dato;``` Nonetheless, I have one more question. I obtain ```5.12 0.1 0.25 0.1 4 3 2 1 1.44 10.2 11.1 12.2 13.3 14.4 15.5 16.6``` and as you can see, the second 0.1 should be 0.10 (with the last 0 at the right). Why is that zero at the right lost? – Vicky May 08 '20 at 01:01
  • The number `0.10` is mathematically the same as `0.1`. If you want to preserve the exact representation used in the file, then you must store the numbers as strings instead of using the data type `double`. However, if all you want is for your program to always print more digits, then you can change the line `cout< – Andreas Wenzel May 08 '20 at 01:37

0 Answers0