File Generation

  • Thread starter skywalker skywalker
  • Start date
S

skywalker skywalker

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::eek:fstream SaveFile(stream.str().c_str());

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

}

Thanks. appreciate your kind advise.

Skywalker
 
V

Victor Bazarov

skywalker said:
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::eek:fstream 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
 
J

Jonathan Lee

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
 
V

Victor Bazarov

Jonathan said:
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.

V
 
J

Jonathan Lee

<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
 
A

Alf P. Steinbach

* Jonathan Lee:
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

Google "give a man a fish".


Cheers & hth.,

- Alf
 
V

Victor Bazarov

Jonathan said:
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 :)

V
 
J

James Kanze

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.
 
J

Jonathan Lee

 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Crossword 2
TF-IDF 1
Diffrerent output of the same program on HP aCC and Linux (Intel/g++) 2
ofstream - Save to File 3
SIGKILL 18
Crossword 14
surprise 0
std::stringstream 1

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top