473,320 Members | 1,993 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Simple program

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" ! :)
--
Ground21____________\\
ground21@pocztaKROPKAfm
Dec 15 '06 #1
11 1966
Ground21 wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 15 '06 #2
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... :(

--
Ground21____________\\
ground21@pocztaKROPKAfm
Dec 15 '06 #3
IR
Ground21 wrote:
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-lit...t.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,
--
IR
Dec 15 '06 #4
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;
}

--
Ground21____________\\
ground21@pocztaKROPKAfm
Dec 15 '06 #5
"Ground21" <gr******@poczta.onet.plwrote in message
news:el**********@news.onet.pl...
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.
outfile.close();

system("pause");
return 0;
}

--
Ground21____________\\
ground21@pocztaKROPKAfm

Dec 15 '06 #6
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 :)

--
Ground21____________\\
ground21@pocztaKROPKAfm
Dec 15 '06 #7
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.
--
Ground21____________\\
ground21@pocztaKROPKAfm
Dec 16 '06 #8
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.

--
Ground21____________\\
ground21@pocztaKROPKAfm
Dec 16 '06 #9

"Ground21" <gr******@poczta.onet.plwrote in message
news:em**********@news.onet.pl...
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
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::ostream& operator<<( std::ostream& os, const T_run&
time );
};

std::ostream& operator<<( std::ostream& 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.
Dec 17 '06 #10
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... )
--
Ground21____________\\
ground21@pocztaKROPKAfm
Dec 17 '06 #11
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... :)

--
Ground21____________\\
ground21@pocztaKROPKAfm
Dec 18 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Patchwork | last post by:
Hi Everyone, Please take a look at the following (simple and fun) program: //////////////////////////////////////////////////////////////////////////// ///////////// // Monster Munch, example...
17
by: savesdeday | last post by:
In my beginnning computer science class we were asked to translate a simple interest problem. We are expected to write an algorithm that gets values for the starting account balance B, annual...
11
by: JKop | last post by:
Take the following simple function: unsigned long Plus5Percent(unsigned long input) { return ( input + input / 20 ); } Do yous ever consider the possibly more efficent:
5
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files,...
10
by: serge calderara | last post by:
Dear all, I need to build a web application which will contains articles (long or short) I was wondering on what is the correct way to retrive those article on web page. In orther words, when...
10
by: Andrew | last post by:
Sorry about this but I'm new to ADO.NET (finally coming from simple ADO, bless it) and I'm trying to create a simple three tier program. Ie, User interface Layer / Business object layer / Database...
4
by: Break2 | last post by:
I am trying to write a simple program which asks a user to enter 5 integers after which the average will be computed and written to the screen. That simple. However I want to do it according to a...
30
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
1
by: astrogirl77 | last post by:
I'm new to C++ and am hoping to find help with coding a simple C program, am wanting to obtain code and functioning exe's. I code in an old version of Visual Basic 4.0, I have a simple app that...
17
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /*...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.