Simple program

G

Ground21

Hello. I'm new // sorry for my english :)

I have to write simple program i c++.
But I don't know how to code src - program should know it's name
(unit1.exe so the name is "unit1" or "unit1.exe" and it's full path
(with volume name - c:\programs).

Which commends of c++ should I use?
(please do not redirect me to 'help', because I must have some "start
point" ! :)
 
V

Victor Bazarov

Ground21 said:
Hello. I'm new // sorry for my english :)

You're doing great so far!
I have to write simple program i c++.
But I don't know how to code src - program should know it's name
(unit1.exe so the name is "unit1" or "unit1.exe" and it's full path
(with volume name - c:\programs).

Which commends of c++ should I use?
(please do not redirect me to 'help', because I must have some "start
point" ! :)

#include <iostream>
int main(int argc, char *argv[])
{
std::cout << "If available, my name is [" << argv[0] << "]\n";
return 0;
}

V
 
G

Ground21

Victor Bazarov napisa³(a):
#include <iostream>
int main(int argc, char *argv[])
{
std::cout << "If available, my name is [" << argv[0] << "]\n";
return 0;

Thanks a lot!
Now I'm trying to write to an external .txt file current date via
structure but I have problems (writing date without structure was ok)...

I have something like that:

struct T_run
{
int hr;
int min;
int sec;
};

....

SYSTEMTIME st;
GetSystemTime(&st);
int second, minute, hour;
second = st.wSecond;
minute = st.wMinute;
hour = st.wHour;

T_run myinfo={hour,minute,second};

....

outfile.write((char*)&myinfo,sizeof(T_run));

but the time is not property written... :(
 
I

IR

Ground21 said:
Now I'm trying to write to an external .txt file current date via
structure but I have problems (writing date without structure was
ok)...

I have something like that:

struct T_run
{
int hr;
int min;
int sec;
};

...

SYSTEMTIME st;
GetSystemTime(&st);

GetSystemTime is not standard C++. But I guess I get your point.
(FYI this group is supposed to deal only with *standard* C++,
everything nonstandard should be avoided if possible)
int second, minute, hour;
second = st.wSecond;
minute = st.wMinute;
hour = st.wHour;

T_run myinfo={hour,minute,second};

...

outfile.write((char*)&myinfo,sizeof(T_run));

but the time is not property written... :(

I'm afraid we might not be able to help you with so little
information...

What output are you expecting? What are you obtaining? What
*exactly* is outfile?


BTW, you should look at the FAQ
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8
for how to post code that doesn't work correctly.
Following those guidelines would make it easier for us to help you.


Cheers,
 
G

Ground21

IR napisa³(a):
What output are you expecting? What are you obtaining? What
*exactly* is outfile?

I'll paste the whole source file...
But from the beginning: program shoud show where is placed (with it's
..exe name) & write into file current time and date via structure. Also
program should write into file some type of counter (how many times was
executed (but we can count this without counter because we've got as
many lines with date in file as many times we had executed program)).
output file is .txt file (ios::binary or app?).

exaple outoput (?):
date: 15-12-2006 time 22:04
date: 16-12-2006 time 12:32
date: 16-12-2006 time 12:35...

source:
#include <fstream.h>
#include <iostream.h>
#include <vcl.h>
#include <time.h>
#pragma hdrstop

struct T_run
{
int hrS;
int minS;
int secS;
};
int main(int argc, char *argv[])
{
char buff[100];

SYSTEMTIME st;
GetSystemTime(&st);
int sec, min, hr;
sec = st.wSecond;
min = st.wMinute;
hr = st.wHour;

T_run myinfo={hr,min,sec};
cout << "Time: hour: " << hr <<":"<<min<<":"<<sec<< endl;

cout << "My name: [" << argv[0] << "]\n";
cout << "Creating file..." << endl;

ifstream infile("log.txt", ios::app);
if(!infile) return 0;
cout << "reading file..." << endl << endl;
while (!infile.eof())
{
infile.getline(buff,sizeof(buff));
cout << buff << endl;
}
infile.close();

ofstream outfile("log.txt", ios::app);
if(!outfile) return 0;
cout << "Writing info..."<< endl;
outfile.write((char*)&myinfo,sizeof(T_run));
outfile.close();

system("pause");
return 0;
}
 
J

Jim Langston

Ground21 said:
IR napisa³(a):
What output are you expecting? What are you obtaining? What *exactly* is
outfile?

I'll paste the whole source file...
But from the beginning: program shoud show where is placed (with it's .exe
name) & write into file current time and date via structure. Also program
should write into file some type of counter (how many times was executed
(but we can count this without counter because we've got as many lines
with date in file as many times we had executed program)).
output file is .txt file (ios::binary or app?).

exaple outoput (?):
date: 15-12-2006 time 22:04
date: 16-12-2006 time 12:32
date: 16-12-2006 time 12:35...

source:
#include <fstream.h>
#include <iostream.h>
#include <vcl.h>
#include <time.h>
#pragma hdrstop

struct T_run
{
int hrS;
int minS;
int secS;
};
int main(int argc, char *argv[])
{
char buff[100];

SYSTEMTIME st;
GetSystemTime(&st);
int sec, min, hr;
sec = st.wSecond;
min = st.wMinute;
hr = st.wHour;

T_run myinfo={hr,min,sec};
cout << "Time: hour: " << hr <<":"<<min<<":"<<sec<< endl;

cout << "My name: [" << argv[0] << "]\n";
cout << "Creating file..." << endl;

ifstream infile("log.txt", ios::app);
if(!infile) return 0;
cout << "reading file..." << endl << endl;
while (!infile.eof())
{
infile.getline(buff,sizeof(buff));
cout << buff << endl;
}
infile.close();

ofstream outfile("log.txt", ios::app);
if(!outfile) return 0;
cout << "Writing info..."<< endl;
outfile.write((char*)&myinfo,sizeof(T_run));

Okay... Your output is supposed to look like:
date: 15-12-2006 time 22:04
you said. What you are writing, however, is the POD (Plain old data) in the
structure.
Instead of using .write, I would use <<
outfile << "date: " << myinfo.hrS << "-" << myinfo.minS << "-" <<
myinfo.secS << " time " //... you finish it

Using the << on an ostream for an integer will actually output the ASCII
representation (I.E. 15 instead of the binary value xF ).

Play around with it a little, get the output to look at you need using what
I've shown so far.
 
G

Ground21

Jim Langston napisa³(a):
Play around with it a little, get the output to look at you need using what
I've shown so far.

thanks Jim! I am newbie in c++ (everyone can see :) )
okay, I'll recode my src like You said...& post how it works :)
 
G

Ground21

Ground21 napisa³(a):

OK, program works fine (I modified outfile.write to outfile <<)
But I have one more question...
struct T_run
{
int hrS;
int minS;
int secS;
};

I added char* pathS; to T_run structure

char buff[100];

And here I've got lengh of buff (100). ok, if I have line like that:
date 12-12-2005 time 12-24-12 it's simple to count how long should be
buff, but when I'll add to my structure char* pathS I don't know how
long could be path to the program! The whole line could be longer than
100, 200 or 300 sings! so I decided to solve this problem like that:

string lineF, insideF;
while (getline(infile, lineF))
{
insideF+=lineF + "\n";
}
cout << insideF;

will it be ok to creative so long strings? I think yes...
outfile << myinfo.hrS.....

here I have outfile << myinfo.pathS too.

but what about problem with sort the output file log.txt by date? (my
program should do this). I think that program does not have to sort
lines by date (because every next date is later than previuos and to
sort output file, we can simply swap the first line with the last
one...), but I have to code the sort method (radix sort). So, it would
be easier (I think) to write file using whole structure
outfile.write((char*)&myinfo(sizeof(T_run)) & after that we could read
the file using structure properties too.
 
G

Ground21

Ground21 napisa³(a):

OK, program works fine (I modified outfile.write to outfile <<)
But I have one more question...
> struct T_run
> {
> int hrS;
> int minS;
> int secS;
> };

I added char* pathS; to T_run structure

> char buff[100];

And here I've got lengh of buff (100). ok, if I have line like that:
date 12-12-2005 time 12-24-12 it's simple to count how long should be
buff, but when I'll add to my structure char* pathS I don't know how
long could be path to the program! The whole line could be longer than
100, 200 or 300 sings! so I decided to solve this problem like that:

string lineF, insideF;
while (getline(infile, lineF))
{
insideF+=lineF + "\n";
}
cout << insideF;

will it be ok to create so long strings? ...
> outfile << myinfo.hrS.....

here I have outfile << myinfo.pathS too.

but what about problem with sort the output file log.txt by date? (my
program should do this). I think that program does not have to sort
lines by date (because every next date is later than previuos and to
sort output file, we can simply swap the first line with the last
one...), but I have to code the sort method (radix sort). So, it would
be easier (I think) to write file using whole structure
outfile.write((char*)&myinfo(sizeof(T_run)) & after that we could read
the file using structure properties too.
 
J

Jim Langston

Ground21 said:
Ground21 napisa³(a):

OK, program works fine (I modified outfile.write to outfile <<)
But I have one more question...


I added char* pathS; to T_run structure

Much, much, much better to use std::string instead of char*. std::string
will handle growing as needed.
char buff[100];

And here I've got lengh of buff (100). ok, if I have line like that:
date 12-12-2005 time 12-24-12 it's simple to count how long should be
buff, but when I'll add to my structure char* pathS I don't know how
long could be path to the program! The whole line could be longer than
100, 200 or 300 sings! so I decided to solve this problem like that:

string lineF, insideF;
while (getline(infile, lineF))
{
insideF+=lineF + "\n";
}
cout << insideF;

will it be ok to creative so long strings? I think yes...
outfile << myinfo.hrS.....

here I have outfile << myinfo.pathS too.

but what about problem with sort the output file log.txt by date? (my
program should do this). I think that program does not have to sort lines
by date (because every next date is later than previuos and to sort output
file, we can simply swap the first line with the last one...), but I have
to code the sort method (radix sort). So, it would be easier (I think) to
write file using whole structure
outfile.write((char*)&myinfo(sizeof(T_run)) & after that we could read the
file using structure properties too.

Okay, I'm not quite sure of all your requirements, do you need to sort the
text output, or the instances?

One thing you can do to make life a little easier is to overload the <<
operator for ostream.

( I'm copying this from some of my source and modifying it here, hopefully I
won't have any errors)

struct T_run
{
int hrS;
int minS;
int secS;

friend std::eek:stream& operator<<( std::eek:stream& os, const T_run&
time );
};

std::eek:stream& operator<<( std::eek:stream& os, const T_run& time )
{
os << "Date:" << time.month << "-" << time.day << "-" << time.year << "
" <<
"Time:" << time.hour << ":" << time.minute << ":" <<
time.secnd << "\n";

return os;
}

This allows you to output your class directly.

outfile << myinfo;

I really don't know if that's going to help you with your problem or not,
but with what you're doing you may want to look into it.

Okay, now, why are you wanting to store a char* into your structure? For
what purpose? Your class doesnt' have to know what the program name is does
it?

Now, if you are wanting to sort you class, then I think you want to overload
the < operator (less than) which is used for the sort algorithms. I don't
quite understand what you are trying to sort though.
 
G

Ground21

Jim Langston napisa³(a):
Okay, I'm not quite sure of all your requirements, do you need to sort the
text output, or the instances?

I want to sort the output file:

For example, I've changed the system date and run my program. Text
output is:

2006 12 17 12 46 14 c:\program.exe (YYYY MM DD HH MM SS execute path)
2006 12 17 10 47 15 c:\program.exe (YYYY MM DD HH MM SS execute path -
with changed time!).

So, my output file shoud be:
2006 12 17 10 47 15 c:\program.exe
2006 12 17 12 46 14 c:\program.exe

(I thik I sort lines by: year first, then months, days... )
 
G

Ground21

Ground21 napisa³(a):
So, my output file shoud be:
2006 12 17 10 47 15 c:\program.exe
2006 12 17 12 46 14 c:\program.exe

(I thik I sort lines by: year first, then months, days... )

but maby It will be ok for me to reverse the lines simply... :)
 

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

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top