'C++ values from struct creating newlines in CSV file

I am very new to C++, and currently am attempting to create a simple user interface which will do the following:

  • Check if a CSV file already exists with the given name
  • Create that file if it does not exists, and write the headers
  • Ask user for information, store in struct
  • Write data to CSV file when done

The issue I am facing is that when the program ends, and writes the data to the CSV file it creates newlines for each struct variable I am writing. I would like the program to just write all data on one line with comma separated format.

Example code: struct.h

#ifndef struct_h
#define struct_h

#include "string"
namespace RB
{

    struct Values
    {
        std::string event;
        std::string town;
        std::string state;
        std::string date;
    };
}; // namespace

#endif /* struct_h */

create.h

#ifndef create_h
#define create_h

#include <cstdio>
#include <ctime>
#include <iostream>
#include <fstream>

namespace RB
{

    int createFile()
    {
        time_t t = time(nullptr);    // the current time
        struct tm gmt = *gmtime(&t); // structured time in GMT

        const int year = 1900 + gmt.tm_year;
        static const char *filename = "results_";

        if (std::ifstream((filename + std::to_string(year) + ".csv").c_str()))
        {
            std::cout << "File Found, appending to file " << (filename + std::to_string(year) + ".csv").c_str() << std::endl;
        }
        else
        {
            std::cout << "No file with name " << (filename + std::to_string(year) + ".csv").c_str() << "\ncreating file...\n" << std::endl;
            std::ofstream ofile((filename + std::to_string(year) + ".csv").c_str());
            ofile << "Event" << ","
                  << "Town" << ","
                  << "State" << ","
                  << "Date" << "\n";
            ofile.flush();
            ofile.close();
        };
        return 0;
    };
}

#endif /* create_h */

main.cpp

#include <cstdio>
#include <stdlib.h>
#include <iostream>
#include "struct.h"
#include "create.h"

int main()
{
    // Run Create before continuing
    int c = RB::createFile();

    struct RB::Values revent;
    // Get Event Name
    std::cout << "Starting to enter data for race event...\n" << std::endl;
    std::cout << "What was the name of the event: ";
    std::cin >> revent.event;


    // Get Event Town
    std::cout << "What town was the race run in: ";
    std::cin >> revent.town;

    // Get Event City
    std::cout << "What state was the race run in: ";
    std::cin >> revent.state;

    // Get Event Date
    std::cout << "When was the race run: ";
    std::cin >> revent.date;


    time_t t = time(nullptr);    // the current time
    struct tm gmt = *gmtime(&t); // structured time in GMT

    const int year = 1900 + gmt.tm_year;
    static const char *filename = "results_";

    if (std::ifstream((filename + std::to_string(year) + ".csv").c_str()))
    {
        std::ofstream ofile((filename + std::to_string(year) + ".csv").c_str(), std::ofstream::out | std::ofstream::app);
        ofile << revent.event << ",";
        ofile << revent.town << ",";
        ofile << revent.state << ",";
        ofile << revent.date;
        ofile.close();
    };
    return 0;
}

Sample Output:

Event,Town,State,Date
CapRock
,Turkey
,Texas
,March 9

I assume this is something simple I am doing wrong, but cannot seem to figure it out on my own.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source