Writing .csv files from C++

前端 未结 6 770
走了就别回头了
走了就别回头了 2020-12-23 11:18

I\'m trying to output some data to a .csv file and it is outputting it to the file but it isn\'t separating the data into different columns and seems to be outputting the da

6条回答
  •  滥情空心
    2020-12-23 11:39

    If you wirte to a .csv file in C++ - you should use the syntax of :

    myfile <<" %s; %s; %d", string1, string2, double1 <

    This will write the three variables (string 1&2 and double1) into separate columns and leave an empty row below them. In excel the ; means the new row, so if you want to just take a new row - you can alos write a simple ";" before writing your new data into the file. If you don't want to have an empty row below - you should delete the endl and use the:

    myfile.open("result.csv", std::ios::out | std::ios::app);
    

    syntax when opening the .csv file (example the result.csv). In this way next time you write something into your result.csv file - it will write it into a new row directly below the last one - so you can easily manage a for cycle if you would like to.

提交回复
热议问题