GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 19-Jun-2005, 11:58
kai85 kai85 is offline
...is NOT a boy!
 
Join Date: Jan 2005
Posts: 58
kai85 is on a distinguished road

Type casts ?


hi,
i have a few questions relating to the two programs written below,
i'd be greatful if u couuld reply as soon as possible.
(i have an exam in less than 12 hours !)
CPP / C++ / C Code:
#include<iostream.h>
#include<fstream.h>

main(int argc,char *argv[])
{
	if(argc!=4)
	{
		cout<<"Usage:change<filename><byte><char>\n";
		return 1;
	}

	fstream out(argv[1],ios::in|ios::out);
	if(!out)
	{
		cout<<"cannot open output file to write in"<<endl;
		return 1;
	}
	out.seekp(atoi(argv[2]),ios::beg);// this is the line  i don't 
                                                     //   understand    !!!!                                             
	out.put(*argv[3]);
	out.close();
	return 0;
}
what does" atoi " do?

here's the second program:
CPP / C++ / C Code:
#include<iostream.h>
#include<fstream.h>



main()
{
	float fnum[4]={99.76,45.87,-3454.34,545.1};
	int i;

	ofstream out("numbers");
	if(!out){
		cout<<"cannot open output file to write in"<<endl;
		return 1;
	}
	out.write([b](unsigned char*)[/b]&fnum,sizeof fnum);//and the other line i 
                                                                      //don't understand!!!!
	out.close();
	for( i=0;i<4;i++)
		fnum[i]=0;


	ifstream in("numbers");
	in.read((unsigned char*)&fnum,sizeof fnum);
	cout<<in.gcount()<<" bytes read"<<endl;
	for(i=0;i<4;i++)
		cout<<fnum[i]<<" "<<endl;
	in.close();
	return 0;
}
i don't understand the bold part, why do these kinda programs that use the read and write functions use type casts???

anyone?simple ,easy to understand explenation????
thanks.
Last edited by kai85 : 19-Jun-2005 at 12:00. Reason: fo
  #2  
Old 19-Jun-2005, 13:21
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
Quote:
Originally Posted by kai85
CPP / C++ / C Code:
out.seekp(atoi(argv[2]),ios::beg);// this is the line  i don't 
                                                     //   understand    !!!!                                            
what does" atoi " do?
It converts a string to an integer.

Quote:
Originally Posted by kai85
CPP / C++ / C Code:
out.write((unsigned char*)&fnum,sizeof fnum);//and the other line i 
                                                                      //don't understand!!!!
i don't understand the bold part, why do these kinda programs that use the read and write functions use type casts???
Because write expects a pointer to a char; fnum is not a char.
  #3  
Old 19-Jun-2005, 19:35
kai85 kai85 is offline
...is NOT a boy!
 
Join Date: Jan 2005
Posts: 58
kai85 is on a distinguished road
Quote:
Originally Posted by Dave Sinkula
It converts a string to an integer.


but i thought we were supposed to enter an integer on the command line.
like 12 0r ... ???
i'm not sure because for some reason(don't ask me) the program doesn't compile

Quote:
Because write expects a pointer to a char; fnum is not a char.

so this line of code would have to compile fine ,right?
CPP / C++ / C Code:
:
:
:
char characterarray[5]={'a','s','d','f''}
out.write(&characterarray,sizeof characterarray);
:
:
:

again ,i can't be sure because the programs don't compile.
thanks,
kai85
BTW what does atol do?
  #4  
Old 19-Jun-2005, 22:21
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
Quote:
Originally Posted by kai85
but i thought we were supposed to enter an integer on the command line.
like 12 0r ... ???
You do enter text, right? Even though the text does represent a number.

Quote:
Originally Posted by kai85
BTW what does atol do?
Same as atoi, different data type.

Google works quite well for such questions. Its answer arrive more quickly.
  #5  
Old 20-Jun-2005, 10:15
kai85 kai85 is offline
...is NOT a boy!
 
Join Date: Jan 2005
Posts: 58
kai85 is on a distinguished road

google?


ur right , i shouldve searched in google , but i ... nope no excuses.. i just didn't think of google.(this place is so good it just pops into mind when coming across problems)
thanx anyway.
  #6  
Old 20-Jun-2005, 23:30
leetster leetster is offline
New Member
 
Join Date: Jun 2005
Posts: 15
leetster is on a distinguished road
In your second program you should change
Code:
float fnum[4]={99.76,45.87,-3454.34,545.1};
to:

Code:
float fnum[5]={99.76,45.87,-3454.34,545.1};
. Because it always holds one value less then what its array number is =)
(or so i remember)
  #7  
Old 21-Jun-2005, 01:21
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by leetster
In your second program you should change
Code:
float fnum[4]={99.76,45.87,-3454.34,545.1};
to:

Code:
float fnum[5]={99.76,45.87,-3454.34,545.1};
. Because it always holds one value less then what its array number is =)
(or so i remember)

No; if you declare an array of four floats, it holds four floats. (The index values for such an array go from fnum[0] to fnum[3] --- maybe that's what you were thinking of.)

Regards,

Dave
  #8  
Old 21-Jun-2005, 06:37
kai85 kai85 is offline
...is NOT a boy!
 
Join Date: Jan 2005
Posts: 58
kai85 is on a distinguished road
Quote:
In your second program you should change
Generic Code:
float fnum[4]={99.76,45.87,-3454.34,545.1};
to:
float fnum[5]={99.76,45.87,-3454.34,545.1};
Because it always holds one value less then what its array number is =)
(or so i remember)


yes thats what u do when u declare an array of characters ,one extra for the null character .
and thats what ive done in the other program where ive declared an array of chars .
anyway has anyone tried compiling the 2 first programs?
do they compile? i know im doing sth wronge. i just have no clue what it is!!!
  #9  
Old 21-Jun-2005, 08:12
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by kai85
yes thats what u do when u declare an array of characters ,one extra for the null character .
and thats what ive done in the other program where ive declared an array of chars .

When you declare an array of, say, 7 chars. If you want to put a "string" in it, the maximum string length is 7, including the terminating null.

Quote:
Originally Posted by kai85
anyway has anyone tried compiling the 2 first programs?
do they compile? i know im doing sth wronge. i just have no clue what it is!!!

Well, we have no clue as to what you tried and what happened. Here are a few observations:

Program number 1 needs to include the stdlib header if you are using Microsoft Visual C version 6 (that's where atoi() is prototyped).

Of course, program number 2 needs to lose the [ B ] and [ /B ] thingies on the out.write() line. If you are using GNU g++, you will find that it is very picky about argument types in function calls. The first argument to the out.write() function should be cast as (const char *), not (unsigned char *), and the first argument to the out.read() function should be cast as (char *) not as (unsigned char *).


With these tweaks, the programs compile and execute on my Windows XP box with Borland, Microsoft, and GNU compilers.

As a matter of style, you should get into the habit of including C++ standard headers. <iostream> instead of <iostream.h>, for example. This also requires some consideration of namespace issues, as has been covered numerous times in threads on this forum. Even though the old style headers still work on all C++ compilers that I have seen, that won't necessarily always be the case.

In a similar vein, the main() program should be defined as "int main" instead of using the legacy implicit "int" declaration. That is:

CPP / C++ / C Code:
int main(int argc, char *argv[])


There are some other style issues, but basically, they seem to work (that is, they do not crash the system with the compilers that I used, and they do produce expected output --- if you know what to expect).

Regards,

Dave
  #10  
Old 23-Jun-2005, 02:52
kai85 kai85 is offline
...is NOT a boy!
 
Join Date: Jan 2005
Posts: 58
kai85 is on a distinguished road
Quote:
Originally Posted by davekw7x
When you declare an array of, say, 7 chars. If you want to put a "string" in it, the maximum string length is 7, including the terminating null.

this is what i ment too.
Ok I did what u said , the second one compiles fine.
with the first program this is what I do:
In command line I locate the directory I’ve saved the programs in, then type:
Cd debug, then type this line:hs458.exe change out 1 z .
I’ve only just learnt I/O so I’m not sure ... after that I open the debug directory
and open the Out file .. in that there’s some numbers and at the end there’s two z’s.
Is this what I’m supposed to see?
sorry if my questions seem stupid ...
 
 

Recent GIDBlogNARMY by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ PhoneBook marita CPP / C++ Forum 46 12-Jun-2005 12:10
Error C2146: syntax error : missing ',' before identifier 'C4' mattchew008 CPP / C++ Forum 2 19-Dec-2004 06:06
type* identifier or type *identifier for pointers? BobbyMurcerFan C Programming Language 3 07-Dec-2004 07:41
How Do i get php to find out the file type of a file for me? viperman95833 MySQL / PHP Forum 2 08-Mar-2003 09:48

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:26.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.