Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

File Generation

0 views
Skip to first unread message

skywalker skywalker

unread,
Oct 27, 2009, 11:37:10 AM10/27/09
to
Hi Guru,

I got following code, but the problem is it only generates one file.
I would want it to generate 5 files (filename in format
"sample_<number>_<timestamp>" , with each file contains 10 entries of
the text "This is sample".

What is wrong with the code ? I couldn't figure it out..

#include <fstream>
#include <iostream>
#include <sstream>
#include <string.h>
#include <string>

using namespace std;

void main (void)
{

std::stringstream stream;
struct tm *ptr;
time_t tm;
char timestamp[60];
string xstr = "This is sample";
for( int n = 1; n <= 5; ++n)
{
tm = time(NULL);
ptr = localtime(&tm);
memset(timestamp,0,sizeof(timestamp));

strftime(timestamp ,100 , "%Y%m%d_%H%M%S",ptr);

stream << "C:\\output\\sample_" << n << "_" << timestamp <<
".txt" ;

std::ofstream SaveFile(stream.str().c_str());

for( int j = 1; j <= 10; ++j)
{
SaveFile << xstr << endl;
}
SaveFile.close();
}

}

Thanks. appreciate your kind advise.

Skywalker

Victor Bazarov

unread,
Oct 27, 2009, 11:50:14 AM10/27/09
to
skywalker skywalker wrote:
> I got following code, but the problem is it only generates one file.

Which one? The first one? The last one? Does it give you a clue?

> I would want it to generate 5 files (filename in format
> "sample_<number>_<timestamp>" , with each file contains 10 entries of
> the text "This is sample".
>
> What is wrong with the code ? I couldn't figure it out..
>
> #include <fstream>
> #include <iostream>
> #include <sstream>
> #include <string.h>
> #include <string>
>
> using namespace std;
>
> void main (void)

'void main'?

> {
>
> std::stringstream stream;
> struct tm *ptr;
> time_t tm;
> char timestamp[60];

60?

> string xstr = "This is sample";
> for( int n = 1; n <= 5; ++n)
> {
> tm = time(NULL);
> ptr = localtime(&tm);
> memset(timestamp,0,sizeof(timestamp));
>
> strftime(timestamp ,100 , "%Y%m%d_%H%M%S",ptr);

100?

>
> stream << "C:\\output\\sample_" << n << "_" << timestamp <<
> ".txt" ;
>
> std::ofstream SaveFile(stream.str().c_str());

You don't even check whether the file has been opened. Try

if (SaveFile.good())

Also, try printing out the name of the file you're trying to open.
Maybe that will give you an idea.

>
> for( int j = 1; j <= 10; ++j)
> {
> SaveFile << xstr << endl;
> }
> SaveFile.close();
> }
>
> }

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Jonathan Lee

unread,
Oct 27, 2009, 12:47:38 PM10/27/09
to
On Oct 27, 11:37 am, skywalker skywalker <skywalker...@gmail.com>
wrote:

> I got following code, but the problem is  it only generates one file.
> I would want it to generate 5 files (filename in format
> "sample_<number>_<timestamp>" , with each file contains 10 entries of
> the text "This is sample".

In addition to what Victor said, you aren't clearing the variable
"stream"
with each loop iteration. So the filename grows longer and longer. On
my system (Linux) these are the filenames it makes:

C:\output\sample_1_20091027_124448.txt
C:\output\sample_1_20091027_124448.txtC:\output
\sample_2_20091027_124448.txt
C:\output\sample_1_20091027_124448.txtC:\output
\sample_2_20091027_124448.txtC:\output\sample_3_20091027_124448.txt
C:\output\sample_1_20091027_124448.txtC:\output
\sample_2_20091027_124448.txtC:\output\sample_3_20091027_124448.txtC:
\output\sample_4_20091027_124448.txt
C:\output\sample_1_20091027_124448.txtC:\output
\sample_2_20091027_124448.txtC:\output\sample_3_20091027_124448.txtC:
\output\sample_4_20091027_124448.txtC:\output
\sample_5_20091027_124448.txt

On a Windows machine the second file onward would be invalid
filenames,
and would never be created.

--Jonathan

Victor Bazarov

unread,
Oct 27, 2009, 1:21:26 PM10/27/09
to
Jonathan Lee wrote:
> On Oct 27, 11:37 am, skywalker skywalker <skywalker...@gmail.com>
> wrote:
>> I got following code, but the problem is it only generates one file.
>> I would want it to generate 5 files (filename in format
>> "sample_<number>_<timestamp>" , with each file contains 10 entries of
>> the text "This is sample".
>
> In addition to what Victor said, you aren't clearing the variable
> "stream"
> with each loop iteration. [..]

<sigh> You've spoiled all the fun for "skywalker"... I hoped the OP
would print those names and figure it out himself. It would be so much
more effective than reading the answer... Oh well.

Jonathan Lee

unread,
Oct 27, 2009, 4:08:31 PM10/27/09
to
On Oct 27, 1:21 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> <sigh>  You've spoiled all the fun for "skywalker"...  I hoped the OP
> would print those names and figure it out himself.

Ha. Sorry. Didn't mean to ruin the surprise :)

Though to be honest I don't get the whole "hinting at the answer"
thing. In person, OK. But on Usenet? I tend to think that when a
poster gets a riddle instead of an answer, what they learn is to
not bother asking questions here.

--Jonathan

Alf P. Steinbach

unread,
Oct 27, 2009, 4:26:22 PM10/27/09
to
* Jonathan Lee:

Google "give a man a fish".


Cheers & hth.,

- Alf

Victor Bazarov

unread,
Oct 27, 2009, 4:31:22 PM10/27/09
to
Jonathan Lee wrote:
> On Oct 27, 1:21 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
>> <sigh> You've spoiled all the fun for "skywalker"... I hoped the OP
>> would print those names and figure it out himself.
>
> Ha. Sorry. Didn't mean to ruin the surprise :)

LOL

> Though to be honest I don't get the whole "hinting at the answer"
> thing. In person, OK. But on Usenet? I tend to think that when a
> poster gets a riddle instead of an answer, what they learn is to
> not bother asking questions here.

It depends on the person asking the questions. If they are inquisitive,
naturally curious, they will keep looking, keep trying to solve the
puzzle. If hints supplied so far aren't enough, they will ask for more.
If they are enough, they will solve the problem. And that's what I
would like to see in a specialist. Judging from the question, the OP is
still learning, so it helps them if we don't just provide answers but
point them in the general direction of the solution and let them figure
out the details.

Speaking of details, your answer wasn't direct either, but rather just
"a bigger hint". So, while you say you "don't get the whole "hinting",
you still participate in it :-)

Jonathan Lee

unread,
Oct 27, 2009, 6:09:26 PM10/27/09
to
On Oct 27, 4:26 pm, "Alf P. Steinbach" <al...@start.no> wrote:
> * Jonathan Lee:

>
> > Though to be honest I don't get the whole "hinting at the answer"
> > thing. In person, OK. But on Usenet? I tend to think that when a
> > poster gets a riddle instead of an answer, what they learn is to
> > not bother asking questions here.
>
> > --Jonathan
>
> Google "give a man a fish".
>
> Cheers & hth.,
>
> - Alf

Sheesh. Couldn't you have provided a link?

http://lmgtfy.com/?q=give+a+man+a+fish

--Jonathan

James Kanze

unread,
Oct 28, 2009, 10:48:26 AM10/28/09
to
On Oct 27, 8:31 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> Jonathan Lee wrote:
> > On Oct 27, 1:21 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> >> <sigh> You've spoiled all the fun for "skywalker"... I hoped the OP
> >> would print those names and figure it out himself.

> > Ha. Sorry. Didn't mean to ruin the surprise :)

> LOL

> > Though to be honest I don't get the whole "hinting at the
> > answer" thing. In person, OK. But on Usenet? I tend to think
> > that when a poster gets a riddle instead of an answer, what
> > they learn is to not bother asking questions here.

> It depends on the person asking the questions. If they are
> inquisitive, naturally curious, they will keep looking, keep
> trying to solve the puzzle. If hints supplied so far aren't
> enough, they will ask for more.

> If they are enough, they will solve the problem. And that's
> what I would like to see in a specialist. Judging from the
> question, the OP is still learning, so it helps them if we
> don't just provide answers but point them in the general
> direction of the solution and let them figure out the details.

There's a difference between "hinting" and "answering with a
riddle". Had the original poster followed up on your hint, he
would have found the error immediately, and in the process,
learned a few other important points, like testing the results
of an operation, and displaying all relevant information in an
error message. And IMHO, if the original poster doesn't have
enough initiative to do that, then he's probably better off in a
different domain. If your answer had really been a riddle, I
could understand Jonathan's objection to it, but IMHO it was
more along the lines of: here's what you should do to find the
answer. IMHO, an excellent and educational response.

--
James Kanze

Jonathan Lee

unread,
Oct 28, 2009, 11:46:04 AM10/28/09
to
On Oct 28, 10:48 am, James Kanze <james.ka...@gmail.com> wrote:
> If your answer had really been a riddle, I
> could understand Jonathan's objection to it, but IMHO it was
> more along the lines of: here's what you should do to find the
> answer.  IMHO, an excellent and educational response.

I think you're taking me too literally about the "riddle" thing.
But in any event, I don't think Victor's response was poor in
any way. If I had a question of my own I would appreciate,
rather than begrudge, a response from Victor.

Still, I think that hints are obstacles for some, and obstacles
prevent people from using a resource. You say that "he [the OP]


would have found the error immediately, and in the process,

learned a few other important points". But that isn't
_necessarily_ true. It _might_ be the case that he got a more
direct answer somewhere else, and goes there instead of here.
Maybe that's his loss. But Maybe it's ours: it could be he is
learning about C++, but knows a great deal about hardware and
his insight would benefit users here.

When I couple this with the fact that I suspect he would have
"learned the lesson" either way, I don't get the point of
hinting.

*shrugs*

--Jonathan

0 new messages