LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-21-2011, 01:59 PM   #1
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Rep: Reputation: 16
write in c++ file


I want to write in a file that has the name of the curent date and time.

Code:
std::string date ()
{
  char date [10];
  char time[10];
  std::ostringstream os;
  _strdate(date);
  _strtime(time);
  os << date << " " << time << ".txt";
  return os.str();
}
const std::string PATH = "e:\\" + date();
std::ofstream out(PATH.c_str(), std::ios::out);
Am i doing something wrong with the PATH? because it doesnt create any file.
 
Old 01-21-2011, 03:40 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Does this help?
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <time.h>

using namespace std;

std::string
mkname ()
{
  char buf[80];
  time_t ltime = time (NULL);
  struct tm *today = localtime (&ltime);
  strftime (buf, sizeof (buf), "%y%m%d%H%M", today);
  std::string s = std::string(buf) + std::string(".txt");
  return s;
}

int
main (int argc, char *argv[])
{
  std::string sPath = mkname ();
  std::ofstream out;
  out.open(sPath.c_str(), std::ios::out);
  out << "Hello world!\n";
  return 0;
}
Quote:
01/21/2011 01:38 PM 14 1101211338.txt

type 1101211338.txt =>
Hello world!
 
Old 01-21-2011, 03:51 PM   #3
z1p
Member
 
Registered: Jan 2011
Location: the right coast of the US
Distribution: Ubuntu 10.04
Posts: 80

Rep: Reputation: 23
vbx_wx as you can see the code that paulsm4 posted is more or the same as yours. The issue with yours was the use of strdate and strtime resulted in you having illegal characters in your file name.
 
Old 01-21-2011, 04:19 PM   #4
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Original Poster
Rep: Reputation: 16
I guess.This code prints e:\1101220016.txt, my code printed somehting like e:\01/01/2011 22.45.txt, but paulsm4 code is good too. Thanks
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Prompt the user for a file to open, extract the XML and write to another text file. richiep Linux - Newbie 7 10-22-2010 03:34 PM
grip : no write access to write encoded file bidouilleur Linux - Software 5 10-09-2010 09:23 PM
forrtl: severe (47): write to READONLY file, unit 5, file /dev/pts/1 terrence Programming 1 10-01-2005 10:22 PM
Grip- "no write access to write encoded file" Alvis Linux - Software 4 01-06-2004 04:18 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration