C ++ Reading double quotes from a file

So, I tried to read the csv file using C ++ and do some calculations and output to another csv file. Everything works fine, but when the program reads the line:

<a href="http://www.google.com" target="_blank">google</a>

and I want to see what the program has read, so I turned off this line and it shows:

<a href=""http://www.google.com"" target=""_blank"">google</a>

Basically, does this double every double quote? How can i solve this?

Changes:

Here is my code:

int main() 
{
    ifstream read;
    ofstream write;
    string line;
    string cell;
    int col = 0;
    string temp;
    string links;
    read.open("Book1.csv");
    write.open("output.csv");
    if (read.is_open())
    {
        cout << "opened" <<endl ;
        getline(read, line);
        while(getline(read,temp))
        {
            stringstream line(temp);
            while (getline(line, cell, ','))
            {
                if (col > 9)
                {
                    links.pop_back();
                    write << links<<endl;
                    col = 0;
                    links = "";
                    break;
                }
                else
                {
                    if (cell != "")
                    {
                        if (col == 0)
                        {
                            write << cell<<',';
                        }
                        else if (col == 1)
                        {
                            write << cell<<',';
                        }
                            else
                    {
                            cell.erase(0, 1);
                            cell.pop_back();
                            links += cell;

                            links += '/';
                        }
                        cout << cell << endl;
                    }
                    col += 1;
                }
            }
        }       
    }
    else 
    {
        cout << "failed" << endl;
    }       
    read.close();
    write.close();  
}
+4
source share
1 answer

This is completely normal. The quotes inside the field (inside your csv file) are escaped using another quote to generate valid csv.

Consider the csv data:

123,"monitor 27"", Samsung",456

a ,, . , .

, , , csv ( csv ).

csv, () , , .


( ):

-, , , , :

"<a href=""http://www.google.com"" target=""_blank"">google</a>"

csv.

csv, ,, .

, :

123
monitor 27", Samsung
456

csv, , . , . , :

123,"monitor 27"", Samsung",456

27" csv .

csv, . , , 2 ( , ):

read a line

bool bInsideQuotes = false

loop over chars
  if character == '"'
    bInsideQuotes = !bInsideQuotes
  if character == ',' and !bInsideQuotes
    found a field separator

, . , : bInsideQuotes false 27", (27"") bInsideQuotes true ( ).

, , . , , csv .

, 2 2 1 .

+3

Source: https://habr.com/ru/post/1628018/


All Articles