Strings and stdio

so im using cstdio library (instead of iostream)
i am trying to input the line entered like "Hello world"

1
2
3
4
5
  #include <cstdio>
  #include <string>
...
string input_name;
scanf("%s",input_name.c_str());

it seems that using c_str() makes it a constant or something so i know i cant use that but what do i use?
in addition, i cant get whitespace with this as well.

Do i really have to use iostream? (thats.. kinda lame though)
AND why when i am looking around, i dont see any strings, only character arrays
(i know that cpp doenst acutally have strings but id still rather mess with "strings" and not directly with the character arrays)
What do you want to know? Are you giving information or do you need information. Elaborate please
<cstdio> is the "C" library (hence <C stdio>) and therefore is more C-oriented.

C did not have the string class. It only had char arrays. Therefore nothing in <cstdio> is built to handle strings. Everything is built to handle char arrays.

This:
scanf("%s",input_name.c_str());

Will not work for many reasons. The main one being that c_str() gives you a read-only version of the string. scanf needs to write to the given buffer.

Furthermore, at this point, 'input_name's buffer has a length of 0 because the string object is empty. So even if that were legal, you would overflow the buffer and corrupt memory. This is one of the fundamental dangers of char arrays and C-style string functions: you must always be wary of buffer overflows. ALWAYS.

This is why string usage (over char arrays) is generally preferred in modern C++. It's also why <iostream> is generally preferred over <cstdio> in modern C++. Since iostream is built to work with strings, there is no risk of buffer overflows and you never have to worry about allocating enough memory or any of that. It's all done automatically.

makes it a constant or something so i know i cant use that but what do i use?


You have to use a char array. Then once it's loaded in the char array you can move it to the string.

1
2
3
4
char buffer[100];
scanf("%s",buffer);

string input_name = buffer;


But again note, you must be wary of buffer overflows. If the user inputs a string longer than 99 characters, you are SCREWED and this code will break. It is very unsafe & is a security risk.

in addition, i cant get whitespace with this as well.


There's probably a way to get whitespace with scanf, but honestly I don't have the %codes memorized so I couldn't tell you how to do it.

That's another reason why cstdio is often avoided.

Do i really have to use iostream?


iostream works much better with strings, so you should welcome it. Effective use of iostream is easier and tons safer than trying to mix and match strings with cstdio.

The desired effect with iostream:
1
2
string input_name;
getline( cin, input_name );



id still rather mess with "strings" and not directly with the character arrays


Good for you. Now take the same logic you used to form that preference and apply it to iostream

char arrays : strings :: cstdio : iostream
OH so iostream would be better i guess?

i was trying to use cstdio because i thought it was faster(... but then again i was only checking hte performance of cout vs printf lol)
http://stackoverflow.com/questions/2872543/printf-vs-cout-in-c

well since im going back to iostream, should i stick with printf or just forget mixing libraries and stick with cout
The top rated answer in that SO thread you linked to summarizes it quite well.

In addition to listing various reasons why using iostream is benefitial, it goes on to say that the speed advantage of cstdio is only benefitial in "very specific and limited cases".

So unless you are really hurting for speed and are sure iostream is causing slowdown, I would recommend you stick with iostream.


well since im going back to iostream, should i stick with printf or just forget mixing libraries and stick with cout


Up to you. Personally, for consistency, I would use iostream everywhere rather than mix-and-matching.
Last edited on
alright! thanks for all the quick help (:

UPDATE: i feel like i should points this out... WOW iostream is HUGE!! my program went from ~300kb to ~1.4MB
Last edited on
Topic archived. No new replies allowed.