Creating a csv file in a particular folder

Hello all,

I am new to a c++ programing and currently working on my college projct. I have to create a csv file which take data from the CAN connected to the PC and store it into it. I wanted to create a CSV file which has Name as a current Timing and it has to store inside the particular Folder which has Name as a current date. I am able to create the Folder which has Name as a current date but not able to create a csv file.

In more clear way, For example: Today is 01/05/2017so the Folder Name should be "01052017" and I run the Code at 02:30:50 then CSV file should have the Name "023050.csv". and if I run the Code again for example at 03:30:10 then csv file should be created in the same Folder with the Name "033010.csv" but if the date Change then new Folder should be created.

void CreateFolder()
{
std::string Today;
SYSTEMTIME date;
GetSystemTime(&date);

time_t rawtime;
struct tm * timeinfo;
char Date[80];

time(&rawtime);
timeinfo = localtime(&rawtime);

strftime(Date, sizeof(Date), "%Y_%m_%d", timeinfo);
/// Creating a folder in a path where project is run
///
auto dirname = fs::current_path() / Date;

printf("creating directory\n", dirname);
if (create_directories(dirname))
{
printf("directory didn't exist yet\n");
}
}

void CreateCSVfile()
{
time_t rawtime;
struct tm * timeinfo;
char Time[80];

time(&rawtime);
timeinfo = localtime(&rawtime);

strftime(Time, sizeof(Time), "%H:%M:%S",timeinfo);
puts(Time); //print the time
/// Creating a CSV file in a current date folder
///

}

int main()
{

void CreateFolder();
void CreateCSVfile();

return 0;

}
Any help will be appriciated.

Thank you in advance.
Last edited on
but not able to create a csv file
What exactly is the problem?
To get the filename you need to get the current time and create the filename from it. To find the correct folder you need to get the current date and see if the folder exists, if not it means it's the first file of this day and you create the folder and safe the file into it.

Here is a tutorial for working with date and time:
https://www.tutorialspoint.com/cplusplus/cpp_date_time.htm
It's difficult to advise if we can't see the relevant section of code.
@kbw
I add my Code for creating Folder. And now i wanted to create a csv file which will be create in the Folder which named as a current date (for example: 01052017).
Last edited on
@Thomas1965
I know how to get the date and time. But creating a csv file which has a Name of current Timing is a Problem.
Last edited on
This how to create a .csv file with the current time in the current directory.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void CreateCSVfile()
{
  time_t rawtime;
  struct tm * timeinfo;
  char filename[80];

  time(&rawtime);
  timeinfo = localtime(&rawtime);

  sprintf(filename, "%02d%02d%02d.csv", 
          timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec );

  printf("\nFilename: %s", filename);

  FILE *dest = fopen(filename, "w");
  if (dest == NULL)
  {
    perror("Error creating file: ");
    return;
  }
  fclose(dest);
  printf("\nFile <%s> created.\n\n", filename);
}

To store the file in the right directory you can either prepend the directory to the filename or change the current working directory.
@Thomas1965
Thanks for your Reply. The Code runs perfectly for me. But is it possible to get this file into the current date Folder?
Last edited on
Try this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#define _CRT_SECURE_NO_WARNINGS

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <direct.h>

#define LOG(...)  printf(__VA_ARGS__);

bool DirectoryExists(const char *dirname)
{
  struct stat info = {0};

  int result = stat(dirname, &info);
  if(result != 0)
    return false;
  
  if ((info.st_mode & S_IFDIR) == S_IFDIR)
    return true;

  return false;
}

// assumes that dest is at least 13 chars long
void GetDirectoryName(char *dest)
{
  time_t rawtime;
  struct tm * timeinfo;
  
  time(&rawtime);
  timeinfo = localtime(&rawtime);

  sprintf(dest, "%02d%02d%04d",
    timeinfo->tm_mday, timeinfo->tm_mon + 1, timeinfo->tm_year + 1900);
}

void CreateCSVfile()
{
  LOG("\n** Entering %s", __FUNCTION__);
  time_t rawtime;
  struct tm * timeinfo;
  char filename[256];
  char dirname[256];

  time(&rawtime);
  timeinfo = localtime(&rawtime);

  GetDirectoryName(dirname);
  LOG("\n** Directory name: %s", dirname);
  if (!DirectoryExists(dirname))
  {
    LOG("\n**Directory %s didn't exist, creating it", dirname)
    _mkdir(dirname);
  }
  sprintf(filename, "%s/%02d%02d%02d.csv", dirname,
    timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);

  LOG("\n** Filename: %s", filename);

  FILE *dest = fopen(filename, "w");
  if (dest == NULL)
  {
    perror("Error creating file: ");
    return;
  }
  fclose(dest);
  LOG("\n**File %s created.\n\n", filename);
}
int main()
{
  CreateCSVfile();
  system("pause");
  return 0;
}
@Thomas1965
Hey, Thanks. It is working well. Thank you very much. I have one more question if you can help.... As per this Code, we will get Folder where Project is saved. Can we also Change the path of creating Folder?
Last edited on
Topic archived. No new replies allowed.