0

So I have a list

{"ID":"55e5f0c8ace3e","nombre":"Jacqueline ","apellido":"Charlet ","sobrenombre":"","edad":"30","caracteristicas":"","comentario":"",

I need to change that list and put it in a correct way like: ID: 55e5f0c8ace3e Nombre: Jacqueline Apellido: ... etc..

Tried this:

#include <iostream>
#include <fstream>
using namespace std;

ifstream datos("datos.txt");
ofstream final("final.txt");

int main(){
    char valor;
    if(!datos)
    {
        cout << "error";
    }
    else
    {
        while(!datos.eof())
        {
            datos.get(valor);
            if(valor == 'I' && datos.peek() == 'D')
            {
                 cout << "I can read" << endl;
            }
        }
    }
}

I´m trying to do this with C++, which is the correct way to do it? I´ve tried some ways, but i dont know how to read from one point to another, by this i mean read from the double comas the ID and finish on the other double comas. Thanks in advance

MiGu3X
  • 119
  • 1
  • 15
  • ` while(!datos.eof())` <== https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – Galik Sep 12 '15 at 02:55

1 Answers1

0

Introduce a integer state variable that describes what part of file you're in. Then interpret every character in accordance with that. For example, initially it's 0. Once you encounter a ", it becomes 1 - that means you're reading a name. If state is 1, and a character is not a "", you accumulate characters in some string variable. Once you encounter a quote character and state is 1, that means the name is over. And so on.

If this is not homework, Google for a C++ JSON parser. This is a stock problem, solved a thousand times already.

Also, please don't edit the answer - place a comment instead.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281