stdio.h vs iostream.h noob question

which is best and why most examples use iostream which i read was rela

Page 1 of 1

14 Replies - 15804 Views - Last Post: 04 April 2008 - 05:54 AM Rate Topic: -----

#1 Cambrion74   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-March 08

stdio.h vs iostream.h noob question

Post icon  Posted 28 March 2008 - 01:36 AM

hi everybody,
this is my first post on this forum, so if these questions are a bit cheesey......
please forgive me.

I have been trying to learn C++ on my own for a while now, and keep hitting stumbling blocks. i have a number of free e-books on programming (3 Ithink) but the one i use the most for trying to learn uses stdio.h and not iostream.h in its examples.

my questions are:
which is more useful in a modern game stdio or iostream?

what are the significant differences (other than majorly different syntax)?

Is it ok to use them both in the same programme?

Does the use of both cause any significant conflicts in terms of call methods, function calls etc.?

are both of these headers ANSII?

Is This A Good Question/Topic? 0
  • +

Replies To: stdio.h vs iostream.h noob question

#2 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,235
  • Joined: 14-September 07

Re: stdio.h vs iostream.h noob question

Posted 28 March 2008 - 04:43 AM

stdio was from the 'C' era and iostream is 'C++' area

modern games would use the latter I imagine
Was This Post Helpful? 0
  • +
  • -

#3 Sepanto   User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 20-March 08

Re: stdio.h vs iostream.h noob question

Posted 28 March 2008 - 05:38 AM

The stdio input/output(scanf,printf,fscanf,fprintf...) are simply functions.
cin and cout are objects of classes (cin of istream and cout of ostream). cin and cout have many methods which they can use being objects of classes (cin.fail(),cin.eof(),cout.width(),cout.good()... and so on and so on). cin and cout have much more usabillity , but for pure input and output scanf and printf are superior, since they use far less memory.
Was This Post Helpful? 0
  • +
  • -

#4 Cambrion74   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-March 08

Re: stdio.h vs iostream.h noob question

Posted 30 March 2008 - 10:45 PM

so in game programming the best one would probalbly be iostream?

ok, seeing as the stdio is just functions and iostream deals with objects, there should be no real conflicts using the two of them in the same program, correct?

can i take the simple returns from stdio functions and use them as the inputs for iostream objects? what problems would this cause if any? would there be any use in doing this? i know that because of the object nature of iostream then it would probably have inbuilt type checks, yes? but i could manipulate raw data with th stdio functions and then use the results as input to the stream objects. or would it be better just to use the raw data and make my own classes that use this data and send the output to stream functions for output?
Was This Post Helpful? 0
  • +
  • -

#5 Sepanto   User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 20-March 08

Re: stdio.h vs iostream.h noob question

Posted 30 March 2008 - 11:55 PM

the best for waht purpose? for pure input and output printf and scanf are superioir, but if yo uneed the more complex parts, cout and cin are better.
P.S
when dealing with strings, it's considered (i'm not 100% sure y) better to use gets() instead of scanf
Was This Post Helpful? 0
  • +
  • -

#6 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,235
  • Joined: 14-September 07

Re: stdio.h vs iostream.h noob question

Posted 31 March 2008 - 12:25 AM

There are actual Strings in C++, in C they are an array of chars
Was This Post Helpful? 0
  • +
  • -

#7 gabehabe   User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1433
  • View blog
  • Posts: 11,006
  • Joined: 06-February 08

Re: stdio.h vs iostream.h noob question

Posted 31 March 2008 - 04:08 AM

Quote

can i take the simple returns from stdio functions and use them as the inputs for iostream objects?

Yea, they can be used along side each other with no errors, the following code is 100% fine:
char sentence[255];
scanf("%s", sentence);
cout << sentence;



Quote

when dealing with strings, it's considered (i'm not 100% sure y) better to use gets() instead of scanf

That's because when you use gets(), it accepts spaces too :)
Was This Post Helpful? 0
  • +
  • -

#8 born2c0de   User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 187
  • View blog
  • Posts: 4,673
  • Joined: 26-November 04

Re: stdio.h vs iostream.h noob question

Posted 31 March 2008 - 04:36 AM

Quote

when dealing with strings, it's considered (i'm not 100% sure y) better to use gets() instead of scanf

gets() is vulnerable to buffer overflow attacks.
gets_s() is used in place of gets().
Was This Post Helpful? 0
  • +
  • -

#9 red_4900   User is offline

  • Code T(h)inkers
  • member icon

Reputation: 21
  • View blog
  • Posts: 1,120
  • Joined: 22-February 08

Re: stdio.h vs iostream.h noob question

Posted 31 March 2008 - 05:04 AM

Quote

gets() is vulnerable to buffer overflow attacks.


how?

Quote

gets_s() is used in place of gets().


is there any difference than gets()? or its a carbon copy of gets()?
Was This Post Helpful? 0
  • +
  • -

#10 Amadeus   User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 253
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: stdio.h vs iostream.h noob question

Posted 31 March 2008 - 05:20 AM

View Postred_4900, on 31 Mar, 2008 - 08:04 AM, said:

Quote

gets() is vulnerable to buffer overflow attacks.


how?

gets does not let you specify a limit on how many characters are to be read, so you must be careful with the size of the array to avoid buffer overflows.

As for the stdio vs iostream debate, stdio is a c Library, while iostream is a C++ header. they are two different, yet related languages. Most compilers (if not all) will allow for a mix of the two, but it is considered poor programming practice.

It should also be noted that the old style .h headers (where applicable) have been deprecated in C++ in favour of using the standard namespaces. It should be #include<iostream> as opposed to #include<iostream.h> .

http://www.cplusplus...namespaces.html
Was This Post Helpful? 0
  • +
  • -

#11 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,235
  • Joined: 14-September 07

Re: stdio.h vs iostream.h noob question

Posted 31 March 2008 - 06:52 AM

If the memory overflows it will start overwriting other stuff and that is BAD!

get_s() takes more parameters then gets()
Was This Post Helpful? 0
  • +
  • -

#12 Cambrion74   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-March 08

Re: stdio.h vs iostream.h noob question

Posted 03 April 2008 - 10:38 PM

hey Amadeus,
thanks heaps for that link. It's sorted out an issue i was having and a blank spot in my understanding......
Got to go and try something with that. you'll probably see me lots in the game programming section as well. i'll be looking for advise on how to do graphics output next, so if you see me there drop me a line.
thanks for everyone's help here. this is the type of stuff i was hoping for when i joined up :^:
Was This Post Helpful? 0
  • +
  • -

#13 maca   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 03-April 08

Re: stdio.h vs iostream.h noob question

Posted 03 April 2008 - 11:06 PM

Hi!
When i started programming i had the same problem, actually i started writing with IOSTREAM
it was a big problem, i was using c++ method on c programs (which is valid and works)
the big deal was that most teachers and books, teach c or c++ apart which is difficult if you mix em.

Also if you are really a begginer i suggest that you use the stdio functions for a while, it will help you a lot on your learning (managing string on c with pointers teach you a lot about pointers and memory and in c++ is already done the dirty work on the class String).

Also remember that C is THE programming language and C++ its extension, learn C and C++ will be a piece of cake!
Was This Post Helpful? 0
  • +
  • -

#14 Cambrion74   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-March 08

Re: stdio.h vs iostream.h noob question

Posted 03 April 2008 - 11:56 PM

good point maca.
I've read a good deal about the development of c++ from c so i know the deal there, but if you learn c++, aren't you by extension also learning about c? i realize there were some fundamental changes in some areas of the language but there is quite a bit that stayed the same......or am i getting totally the wrong picture here?

I must admit that the programming i've done in c++ has mostly dealt with the stdio functions because i want to try to keep the number of #includes down and i want to keep track of what i'm doing mysefl and haven't been too worried about the look of the output (until now). but i kept hearing about streaming when looking at new graphics cards so it started to make me wonder. then of course when i started looking at the opengl tutorials for graphics programming and got totally confused.

oh well.... back to the drawing board......seems i've been doing that for the last 2 - 3 years :blink:

i've been starting to use the iostream stuff but am a bit wary because i know that these are essentially their own objects (the streams i mean), and i would like to know EXACTLY what was going on before i used them (although this would probably take me the next 2 - 3 years :P).
Was This Post Helpful? 0
  • +
  • -

#15 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: stdio.h vs iostream.h noob question

Posted 04 April 2008 - 05:54 AM

Quote

Also remember that C is THE programming language and C++ its extension, learn C and C++ will be a piece of cake!


Not quite. C++ is not merely an "extension" of C. It is a new language which just happens to have the "feature" that C is a subset. Though C is not a proper subset (there are various esoteric examples of C code that will not work in C++ -- most of these examples are really poor programing and are considered to be "fixed" in C++).

Some people think it is good to learn C and then C++. That is what I did. However there are arguments against this. Mostly because when you learn C you learn a bunch of bad habits for a C++ programmer. You have to sort of "unlearn" some habits. For example C programmers tend to use character arrays very heavily, but C++'s String class is a much safer and robust way to deal with strings.

Quote

i was using c++ method on c programs (which is valid and works)
You are wrong. You were fooled. You were using C methods on a C++ program!!! A strictly C compiler (or even a C++ compiler in C mode) will not compile using the C++ libraries. These headers would contain a TON of syntax errors. Nope you must have been in C++ mode and just thought you were getting away with something.


In the end though it really does not matter. As you mature the stdio library tends to fade out because it has some security/stability issues. Then you begin to learn the wonders of OOP and overloading operators and you find that you can do wonders with the iostreams.

As a beginner if you are following a book that starts out in C++ I say more power to you. But, most people I think take the road C to C++.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1