C++ values from struct creating newlines in CSV file - c++

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.

Related

timegm, mktime changing struct tm

I try to read some dates in format (d-m-Y) from a file. The dates are in pairs and if the second date is equal to '-' then I register that date as a nullptr. Here is the file:
30-05-2009 20-02-2020
12-06-2012 04-01-2017
22-06-2012 26-04-2017
15-03-2006 15-01-2012
25-09-2002 02-02-2006
15-07-2005 -
20-03-2000 23-01-2004
12-01-2012 19-03-2017
14-08-2008 15-08-2008
27-11-2006 18-06-2007
03-10-2006 24-01-2007
20-12-2010 23-04-2018
27-01-2007 02-03-2018
16-10-2013 11-10-2015
09-07-2003 09-10-2014
31-03-2007 28-12-2013
11-01-2012 06-09-2017
15-08-2007 18-03-2011
24-12-2004 19-08-2008
23-11-2009 -
10-05-2000 11-02-2012
26-12-2005 07-04-2007
12-01-2003 04-10-2008
24-03-2004 04-01-2016
31-05-2001 21-01-2002
27-11-2001 21-03-2013
29-09-2007 21-02-2020
13-10-2004 06-04-2015
12-06-2005 07-05-2008
14-03-2001 21-07-2005
23-11-2003 16-01-2017
13-05-2003 15-07-2016
02-12-2006 17-04-2013
20-03-2004 06-08-2014
18-12-2016 10-03-2017
22-01-2000 13-02-2004
02-03-2000 18-12-2015
01-12-2004 31-03-2019
29-04-2006 19-03-2012
14-04-2007 11-03-2015
02-03-2002 13-12-2015
03-12-2001 16-01-2013
10-12-2000 16-05-2015
08-04-2000 04-04-2018
01-02-2008 30-11-2009
30-03-2006 -
08-09-2010 21-02-2017
19-02-2002 15-03-2003
17-09-2007 18-09-2010
23-01-2007 -
Here is my code as well:
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main(int argc, char const *argv[]) {
//variables for reading file
string tempDate;
struct tm *entryDate, *exitDate;
ifstream file("dates.txt");
string input;
// read file
if (file.is_open()) {
cout << '\n';
entryDate = new struct tm;
while (getline(file, input)) {
istringstream inputStream(input);
inputStream >> get_time(entryDate, "%d-%m-%Y") >> tempDate;
if (!tempDate.compare("-")) {
exitDate = nullptr;
} else {
istringstream dateStream(tempDate);
exitDate = new struct tm;
dateStream >> get_time(exitDate, "%d-%m-%Y");
// timegm changes strut tm????
cout << "Before: " << exitDate->tm_mday << ' ' << exitDate->tm_mon << ' ' << exitDate->tm_year << "\n";
cout << timegm(entryDate) << ' ' << timegm(exitDate) << boolalpha << ' ' << (timegm(entryDate) > timegm(exitDate)) << "\n";
cout << "After: " << exitDate->tm_mday << ' ' << exitDate->tm_mon << ' ' << exitDate->tm_year << "\n";
if (timegm(entryDate) > timegm(exitDate)) { // if entryDate is after exitDate
cerr << "Error: entryDate is later than exitDate\n";
cout << put_time(entryDate, "%d-%m-%Y") << ' ' << put_time(exitDate, "%d-%m-%Y") << '\n';
continue;
}
}
entryDate = new struct tm;
}
file.close();
} else {
cerr << "Error: Cannot open file\n";
return 1;
}
return 0;
}
At some point I try to check if the first date is bigger than the second one because he first date is a hospital entry date and the second is an exit date. But when i tried to convert the struct tm to time_t so I can compare the dates, I realized that for some inputs the struct had changed after I called the timegm function (it happens with mktime as well). I've seen people having problems with these functions usually because of DST, but I can't see how that affects my program.
Found the solution after I came across an example in linux manpages that used a struct tm variable and had nothing to do with timegm. If anyone has the same problem just initialize the struct tm to all zero bytes like this:
memset(date, 0, sizeof(struct tm));

Reading or writing binary file incorrectly

The output of the code show gibberish values for all the variables of the Student struct. When the display function is ran.
I've include the relevant code in each of the add and display function for the binary file.
For the second function, does the seekg pointer automatically move to read the the next record each time the for loop runs?
//Student struct
struct Student
{
char name [30];
float labTest;
float assignments;
float exam;
};
//Writing function
afile.open(fileName,ios::out|ios::binary);
Student S;
strcpy(S.name,"test");
S.labTest = rand()%100+1;
S.assignments = rand()%100+1;
S.exam = rand()%100+1;
afile.write(reinterpret_cast<char*>(&S),sizeof(S));
afile.close();
//Reading function
afile.open(fileName,ios::in|ios::binary);
afile.seekg(0,ios::end);
int nobyte = afile.tellg();
int recno = nobyte / sizeof(Student);
Student S;
//Loop and read every record
for(int i = 0;i<recno;i++)
{
afile.read(reinterpret_cast<char*>(&S),sizeof(S));
cout << "Name of Student: " << S.name << endl
<< "Lab mark: " << S.labTest << endl
<< "Assignment mark: " << S.assignments << endl
<< "Exam mark: " << S.exam << endl << endl;
}
afile.close();
There are a lot of problems with your code:
Calling your write function will permanently overwrite the last written data set. You have to add: ios::append, so that new data will be written behind the last data you wrote before.
After you move with afile.seekg(0,ios::end); to get with tellg the file size, you have to go back to the start of the file before reading with afile.seekg(0,ios::beg)
It looks that you use a char array to store a string. This is not c++ style! And it is dangerous how you use it. If you use strcpy, you can copy a string which is longer than the space you reserved for it. So you should prefer std::string for that. But you can't simply write a struct which constains std::string as binary! To get checked copy you can use strncpy, but that is still not c++ ;)
For the second function, does the seekg pointer automatically move to read the the next record each time the for loop runs?
Yes, the file position moves which each successful read and write.
A general remark writing binary data by simply dumping memory content:
That is not a good idea, because you can only read that data back, if you use the same machine type and the same compiler options. That means: A machine with different endianness will read data totally corrupted. Also a different integer type ( 32 bit vs 64 bit ) will break that code!
So you should invest some time how to serialize data in a portable way. There are a lot of libraries around which can be used to read/write also complex data types like std::string or container types.
A hint using SO:
Please provide code which everybody can simply cut and paste and compiled. I did not know what your Student struct is. So I take a lot of assumptions! Is your struct really using char[]? We don't know!
#include <iostream>
#include <fstream>
#include <cstring>
const char* fileName="x.bin";
struct Student
{
char name[100]; // not c++ style!
int labTest;
int assignments;
int exam;
};
// Writing function
void Write()
{
std::ofstream afile;
afile.open(fileName,std::ios::out|std::ios::binary|std::ios::app);
Student S;
strcpy(S.name,"test"); // should not be done this way!
S.labTest = rand()%100+1;
S.assignments = rand()%100+1;
S.exam = rand()%100+1;
afile.write(reinterpret_cast<char*>(&S),sizeof(S));
afile.close();
}
void Read()
{
//Reading function
std::ifstream afile;
afile.open(fileName,std::ios::in|std::ios::binary);
afile.seekg(0,std::ios::end);
int nobyte = afile.tellg();
int recno = nobyte / sizeof(Student);
afile.seekg(0, std::ios::beg);
Student S;
//Loop and read every record
for(int i = 0;i<recno;i++)
{
afile.read(reinterpret_cast<char*>(&S),sizeof(S));
std::cout << "Name of Student: " << S.name << std::endl
<< "Lab mark: " << S.labTest << std::endl
<< "Assignment mark: " << S.assignments << std::endl
<< "Exam mark: " << S.exam << std::endl << std::endl;
}
afile.close();
}
int main()
{
for ( int ii= 0; ii<10; ii++) Write();
Read();
}
EDIT. Apparently, I was a bit too late in responding. Klaus has compiled a better, more comprehensive response dwelling into other problems regarding C-style char [], std::string and the endianness of the platform.
You should append to the file opened for every record. In your code you don't have this, at all. Please write the code in a way we can copy and paste, and test. As a working example, you should write some code that can be compiled and run as below:
#include <algorithm>
#include <cstring>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
// Student struct
struct Student {
char name[30];
float labTest;
float assignments;
float exam;
};
// Serializer
void serialize_student(const Student &s, const std::string &filename) {
// Append to the file, do not overwrite it
std::ofstream outfile(filename, std::ios::binary | std::ios::app);
if (outfile)
outfile.write(reinterpret_cast<const char *>(&s), sizeof(Student));
}
// Deserializer
std::vector<Student> deserialize_students(const std::string &filename) {
std::ifstream infile(filename, std::ios::binary);
std::vector<Student> students;
Student s;
while (infile.read(reinterpret_cast<char *>(&s), sizeof(Student)))
students.push_back(std::move(s));
return std::move(students);
}
int main(int argc, char *argv[]) {
// Generate records
std::vector<Student> mystudents;
std::generate_n(std::back_inserter(mystudents), 10, []() {
Student s;
std::strcpy(s.name, "test");
s.labTest = rand() % 100 + 1;
s.assignments = rand() % 100 + 1;
s.exam = rand() % 100 + 1;
return s;
});
// Print and write the records
for (const auto &student : mystudents) {
std::cout << student.name << ": [" << student.labTest << ','
<< student.assignments << ',' << student.exam << "].\n";
serialize_student(student, "students.bin");
}
// Read and print the records
auto records = deserialize_students("students.bin");
std::cout << "===\n";
for (const auto &student : records)
std::cout << student.name << ": [" << student.labTest << ','
<< student.assignments << ',' << student.exam << "].\n";
return 0;
}

Cross referencing data from file

Cant figure this out....I got two txt files, one is encoding1.txt which contains a set of hexadecimals and the ID number that is assigned to them, such as
61 1
C3A4 2
C990 4
C991 5
C991CC83 6
C992 7
CA8C 9
The other file source.txt is the source file which contains a bunch of combined hex and the students' names
CA8CC992 Jack
C991C3A4 Amy
C991CC83 Sam
61C991 Tom
I want to output a txt file for each students with their name as their filenames and inside the file should be the assigned numbers of their hex. e.g. for jack.txt, inside it should be
9
7
Below is the code that I've tried it generates all the test file but inside its jack.txt theres only 9 and other files are empty. Whats the correct way to do this and how do I get the results right?
#include "stdafx.h"
#include <iostream>
#include <string>
//#include<map>
//#include<unordered_map>
#include <fstream>
#include<array>
//#include<vector>
#include <algorithm>
using namespace std;
int main()
{
std::ifstream file;
file.open("encoding1.txt");
if (file.is_open())
{
std::cout << "opened encoding1 file";
}
std::ifstream file2;
file2.open("source.txt");
if (file.is_open())
{
std::cout << "opened source file" << std::endl;
}
std::ofstream outfile;
const int hexencosize = 7;
std::string hexenco[hexencosize] = {};
int id[7] = {};
std::string hexsource;
string * p;
std::string name;
for (int i = 0; i < 6; i++)
while (file >> hexenco[i] >> id[i]) {
std::cout << "hexenco is " << hexenco[i] << std::endl;
std::cout << "id is " << id[i] << std::endl;
};
for (int o = 0; o < 6; o++)
while (file2 >> hexsource >> name) {
outfile.open(name + ".txt");
std::size_t found = hexsource.find(hexenco[o]);
if (found != std::string::npos)
p = std::find(hexenco, hexenco + hexencosize, hexenco[o]);
if (p >= hexenco + hexencosize) std::cout << "Not found" <<
std::endl;
else outfile << id[p - hexenco] << '\n';
outfile.close();
};
system("pause");
return 0;
};

Naming a log file using date and time in C++

So, I want to create a log file for an app I am trying to create and I don't know how to name the log to something like "log/date&time"
Anyway, here is my code:
#include <iostream>
#include <fstream>
#include <time.h>
#include <stdio.h>
#include <sstream>
using namespace std;
int main (int argc, char *argv[])
{
time_t t = time(0);
struct tm * now = localtime( & t );
char buffer [80];
strftime (buffer,80,"%Y-%m-%d.",now); //i think i can't just put "log/%Y-%m-%d." there.
ofstream myfile;
myfile.open ("log/" + buffer); // this is my problem, i can't put the ' "log/" + ' part there
if(myfile.is_open())
{
cout<<"Success"<<std::endl;
}
return 0;
}
You should use std::string which supports concatenation via the overloaded operator+.
std::string buffer(80, '\0');
strftime( &buffer[0], buffer.size(), "some format string", now);
/* ... */
std::ofstream myfile( ("log/" + buffer).c_str() );
// Remove the (..).c_str() part when working with a C++11 conforming
// standard library implementation
you actual question is "why doesnt this work"
myfile.open ("log/" + buffer);
answer - because c++ doesnt support what you want - concatenate a string literal with a char * and return another char *.
do
std::string filetime(buffer);
std::string filename = "log/" + filetime;
open(filename.c_str());
Consider using std:: facilities instead (std::string and std::ostringstream come to mind):
std::ostream& time_digits(std::ostream& out, unsigned int digits)
{ // convenience function: apply width and fill for the next input
return out << std::setw(digits) << std::setfill('0');
}
std::string unique_log_name()
{ // generate unique log name, depending on local time
// example output: "log/2014-04-19.log"
auto now = time(0);
tm *ltm = localtime(&now);
std::ostringstream buffer;
buffer
<< "log/" << time_digits(4) << ltm.tm_year
<< "-" << time_digits(2) << ltm.tm_mon
<< "-" << time_digits(2) << ltm.tm_day;
// could also add these to the name format:
// buffer
// << "-" << time_digits(2) << ltm.dm_hour
// << "-" << time_digits(2) << ltm.tm_min
// << "-" << time_digits(2) << ltm.tm_sec;
buffer << ".log"; // add extension
return buffer.str();
}
void client_code()
{ // construct log stream on unique file name
ofstream myfile{ unique_log_name() };
if(myfile)
{
cout << "Success" << std::endl;
}
}

fixed amount of digits on creating .txt file

I need to create file with specific file name format (on windows). the format is:
Name_nodeNum_frequency.txt
nodeNum is int and frequency is float.
those two variables should be written with fixed digits:
if nodeNum is 8 --> 008
if frequency is 4.88421 --> 4.884
this is the function:
create_file(int nodeNum, double frequency)
{
char buffer [50];
//convert int to string
itoa(nodeNum, buffer, 10);
string sNodeNum = string(buffer);
//not sure about the double
//tried to_string but I got:
// more than instance of overloaded function matches the argument list
string fileName = ("Name_" + sNodeNum + "_" + sfreq + "MHZ");
FILE* pFile = OpenFile(fileName);
}
I tried to use %d, but it seems like I should not do that:
string fileName = ("Delay_" + "%3d" + "_" + sfreq + "MHZ" , sNodeNum);
I will be happy for some guidance.
thanks!
You seem to be mixing C and C++ here. A simple way to do this in C would be:
#include <stdio.h>
int main()
{
int sNodeNum = 8;
double sfreq = 4.88421;
char filename[50];
sprintf(filename, "Delay_%03d_%.3fMHZ.txt", sNodeNum, sfreq);
FILE* pFile = fopen(filename, "w");
return 0;
}
If on the other hand, if you want to use C++, you should make a few changes:
#include <iomanip>
#include <fstream>
#include <sstream>
#include <iostream>
int main()
{
int sNodeNum = 8;
double sfreq = 4.88421;
std::ostringstream ss;
ss << "Delay_" << std::setfill('0') << std::setw(3) << sNodeNum
<< "_" << std::setprecision(4) << sfreq << "MHZ.txt";
std::string filename(ss.str());
std::ofstream fout(filename.c_str());
return 0;
}
Each of these two approaches opens a file for writing, with the name Delay_008_4.884MHZ.txt.
Live demo link
#include <string>
#include <iomanip>
#include <iostream>
int nodeNum = 8;
float frequency = 4.88421f;
std::ostream& out = std::cout; // put your std::ofstream file or std::ostringstream
std::ios::fmtflags flags = out.flags();
out.width(3);
out.fill('0');
out.setf(std::ios::right, std::ios::adjustfield);
out << nodeNum << std::endl;
flags = out.flags(flags);
out.precision(3);
out.setf(std::ios::fixed, std::ios::floatfield);
out << frequency;
out.flags(flags);
Or even simpler:
out << std::setw(3) << std::setfill('0') << std::right << nodeNum << std::endl;
out << std::setprecision(3) << std::fixed << frequency;
Output:
008
4.884