-5

How do I read in a c-string in to a character array without knowing the size of the string that the user will enter ?

Varaquilex
  • 3,447
  • 7
  • 40
  • 60
Amber Roxanna
  • 1,665
  • 4
  • 24
  • 30
  • 4
    So far what did you write? (a resize on demand char array? something?) – Adriano Repetti Dec 02 '13 at 18:58
  • do you know the difference of a c-string and character array? – behzad.nouri Dec 02 '13 at 19:00
  • @runnerup no not really. I thought a c-string was an array of characters will a null termination – Amber Roxanna Dec 02 '13 at 19:02
  • A common approach is to declare/allocate it to a beyond-reasonable length, (4096, say). then terminate/realloc it after the input is completed. – Martin James Dec 02 '13 at 19:03
  • @AmberRoxanna so you already know how to do this – behzad.nouri Dec 02 '13 at 19:03
  • @AmberRoxanna Well, it is. Not sure what runnerup is referring to. –  Dec 02 '13 at 19:03
  • Right! Then start allocating a fixed size array (initialized to zero), keep counter of last inserted character and resize array (realloc) when end is reached. – Adriano Repetti Dec 02 '13 at 19:04
  • Investigate fgets from stdio – master_latch Dec 02 '13 at 19:04
  • 2
    So now we should decide. Is this C **XOR** C++? –  Dec 02 '13 at 19:12
  • @H2CO3 0x++? I think that XOR just blew my brain. Note: in my book `fgetc(stdin)` is probably the easiest way to get a handle on putting it all together... Of course, the trick comes in knowing when things have/haven't already been typed. `getch()` `kbhit()` etc in conio.h, but alas not standard. –  Dec 02 '13 at 19:19
  • 2
    @ebyrob If that XOR blew your brain, it's time to [blow it even more](http://david.tribble.com/text/cdiffs.htm). C and C++ are very different and incompatible languages. C code should be written in C style and it should be compiled with a C compiler. C++ code should be written in C++ style and it should be compiled with a C++ compiler. The two languages sharing a narrow common subset has historical reasons, but that doesn't mean that one could/should/attempt to write "C/C++" code (i. e. mix the two languages). –  Dec 02 '13 at 19:23
  • @H2CO3, Ooh, I don't think I've ever seen that. Thanks! :) I mean they've changed a bit since 2011, but a list is great. – chris Dec 02 '13 at 19:24
  • @chris You're welcome. :) I've only discovered it recently, but I'm soooo glad I've found it. A great way to enlighten all those "C/C++" programmers out there. –  Dec 02 '13 at 19:25
  • See http://stackoverflow.com/questions/3536153/c-dynamically-growing-array on how to implement a dynamic growing array – Fredrik Pihl Dec 02 '13 at 19:30
  • @H2CO3 More like C/C++/CLI/C#/SQL... But if you think Visual Studio has much to do with standards well. You're probably lucky enough to never have used it. –  Dec 02 '13 at 19:37
  • 1
    @ebyrob Huh... Wat? What's that whining about VS? How come, even? I've said nothing about it. At all. I don't know/understand why you are suggesting I did. And I know it doesn't respect the C++ standards, it doesn't surprise me in fact (Microsoft are pretty bad at implementing standards in general). –  Dec 02 '13 at 19:42
  • @H2CO3 No, you pointed me to a link about theoretical problems between ISO C and ISO C++... I just pointed out which compiler I use and the fact that, this is the tip of the iceberg man. –  Dec 02 '13 at 19:46
  • 1
    @H2CO3 yes but less than a new GCC release... – Adriano Repetti Dec 02 '13 at 20:16
  • @Adriano This isn't asking about a problem with written code, but rather just "asking for code" - so that reason would likely be more appropriate, but I've rather started to give up on that reason recently (too many interesting questions qualify (this *not* being one of them) and I don't think I'd ever convince enough people to change that - and just voting to close most interesting questions will get me fed up with this site fairly quickly). (I, for one, try to make my decisions on voting independent of the gender (/ age / experience / command of the English language) of the asker.) – Bernhard Barker Dec 02 '13 at 22:05

1 Answers1

0

Without any code or further description of your issue it's hard to know what you're trying to achieve, but, one of the following might be appropriate for your needs:

  1. Use a preallocated array of some maximum size you know is greater than the number of characters that will be entered.

  2. Create an empty std::string, and then use the string "+=" operator for each character entered. Then you can convert back to an array using the c_str() method.

MikeD
  • 300
  • 1
  • 7
  • The second option would be much better just reading into the `std::string`, as it supports that. However, I'm willing to bet it would defeat the purpose of what the OP is writing. – chris Dec 02 '13 at 19:05
  • @chris And it's not even C. –  Dec 02 '13 at 19:12
  • @Chris, I think you're making an assumption I wasn't willing to make, that being the manner in which the input arrives. Per-character input (with variable latency between inputs) is common in many systems you may not be thinking of, so I don't think just reading into the string covers everything. Given the vagueness of the question, I kept my response general enough to be useful in such cases. – MikeD Dec 02 '13 at 19:14
  • @MikeD, You've piqued my interest. – chris Dec 02 '13 at 19:17
  • Also, I came to this question because it was tagged C++, though I'm not seeing that now :/ – MikeD Dec 02 '13 at 19:17
  • @chris In some embedded systems, say, operator control of some remote vehicle or other machinery, encrypted commands are sent in chunks as little as one byte in size. You have to store them up and wait for special terminal characters before you know you have the complete string. Yes that's a pretty specific case, but it's what drove my answer. – MikeD Dec 02 '13 at 19:37