Solved

A very simple c++ question.

Posted on 1997-12-04
9
223 Views
Last Modified: 2010-04-10
I Need some easy functions.

1. void create_file()
// This function shall just crate a file. The name of the file shall be the date when I'ts created.

2. void write_to_file(string var1, string var2, double number)

This file shall add things to the file crated abow (in 1.)
I like the file to look like this:

XXX var1 var2 number
XXX var1 var2 number
.....................
.....................
XXX var1 var2 number

Where XXX is the time when the function is called
0
Comment
Question by:wqclatre
  • 6
  • 3
9 Comments
 
LVL 2

Accepted Solution

by:
anichini earned 150 total points
ID: 1174792
#include <time.h>
#include <string>
#include <fstream>
#include <strstream>
using namespace std;

ofstream g_file;

void create_file()
{
    time_t ltime;
    struct tm *today;    
    strstream file;    
 
    time(&ltime);
    today = localtime(&ltime);
    file << today->tm_mon << "-" << today->tm_yday << "-" << (today->tm_year+1900);

    g_file.open(file.str();
}

void write_to_file(string var1, string var2, double num)
{
  if(g_file.is_open())
  {
      time_t ltime;
      struct tm *today;
     
      time(&ltime);
      today = localtime(&ltime);    

      g_file << today->tm_hour << ":" << today->tm_min << ":"
             << today->tm_sec << " " << var1 << " " << var2
             << " " << number << endl;
}

}
0
 
LVL 2

Author Comment

by:wqclatre
ID: 1174793
I'll mark youre answer an A: Exellent, just a moment....

Could you please tell me what the row "using namespace std" are doing?
0
 
LVL 2

Author Comment

by:wqclatre
ID: 1174794
Some more thing. My compiler say that the files strstream and fstream don't exist. (I'm running Linux and compile with g++)
0
 
LVL 2

Expert Comment

by:anichini
ID: 1174795
1) namespaces are a way to cut down on the clutter of function, data, and type names in C++. The standard C++ library uses the std namespace. the line using namespace std; just tells the compiler to add all of those symbols to the global namespace

2) ah, I was assuming Microsoft VC++. Use <fstream.h> and <stdstream.h> instead

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Author Comment

by:wqclatre
ID: 1174796
If I add a .h to fstream and strstream I get those errors:
(A also added a ")" at row gfile.open(file.str(); )

logg.cc:5: warning: namespaces are mostly broken in this version of g++
logg.cc: In function `void write_to_file(class basic_string<char,string_char_traits<char> >, class basic_string<char,string_char_traits<char> >, double)':
logg.cc:29: `number' undeclared (first use this function)
logg.cc:29: (Each undeclared identifier is reported only once
logg.cc:29: for each function it appears in.)

0
 
LVL 2

Author Comment

by:wqclatre
ID: 1174797
I find the error my self. Thanks...
0
 
LVL 2

Author Comment

by:wqclatre
ID: 1174798
I find the error my self. Thanks...
0
 
LVL 2

Author Comment

by:wqclatre
ID: 1174799
Can I somehow change so the file is date.txt instead of the date ??? (add .txt at the end of the filename...
0
 
LVL 2

Expert Comment

by:anichini
ID: 1174800
file << today->tm_mon << "-" << today->tm_yday << "-" <<
                       (today->tm_year+1900) << ".txt";

that's all.
 
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now