0

How to get a string from user and add to a text file?(C/Cpp)

I want to get a string including blanks (spaces),numbers and letters from user and add them to a text file.I know about a function "gets()" but it doesn't have any result. Help,please:)

6th Jun 2020, 8:12 PM
dia
dia - avatar
4 Answers
+ 4
Onur For strings, the scanf() functions are just as dangerous as gets() unless you specify an input range. To safely use them for strings you'd have to rewrite it as: char s[256]; fscanf(fp, "%255[^EOF]s", s); ^^^ This doesn't stop at EOF (end of file) but stops reading if the characters E, O, or F is found in the file. The scanf() functions are quite inflexible for strings as they require you to keep track of the size in two places - the string and the format specifier. fgets(s, sizeof(s), fp) reads into a string of size bytes from file fp until it finds a newline or EOF and the size is determined at compile-time - which is much safer. https://code.sololearn.com/cniN68ESBDqI/#c
6th Jun 2020, 9:44 PM
Gen2oo
Gen2oo - avatar
+ 4
Onur Without an input range it has the same potential to overrun the buffer as gets() does. And having the size hard-coded into the format specifier seems like a problem waiting to happen - you now have to change the size in two places if you want to grow/shrink the buffer. Change only one of them you'll have either a bug or a possible buffer overflow. It's also easy to forget that the format specifier should accept size - 1 characters so there's space for a null-byte or you will probably overrun the buffer somewhere else. The Microsoft article you linked to handles the line-by-line problem by consuming the \n, but it doesn't handle it gracefully if your buffer can't fit all the characters on a line. It would then stop at 'size' and consume a valid character. (Usually this is not a problem if you know the layout of your file). So there is extra baggage that goes with using the scanf() functions safely that you don't have to worry about using fgets(). And I'm not saying there aren't any valid use cases for X-scanf, but they're especially bad for strings and dealing with overflows in general. Some follow-up articles: https://stackoverflow.com/questions/9278226/which-is-the-best-way-to-get-input-from-user-in-c https://bytes.com/topic/c/answers/511986-how-use-scanf-safely
8th Jun 2020, 5:07 PM
Gen2oo
Gen2oo - avatar
+ 2
Firstly, you'll need to use fgets or scanf functions instead of gets. gets is not safe to use because it doesn't have a buffer control. After you read the string from user input and stored it in a char string successfully, you can add it to a text file easily. File management isn't that hard, with a quick research you can get it done easily. If you have any troubles, you can post your code here. Here's an example code for writing some strings to a text file. https://code.sololearn.com/c7I1072GvKGI/?ref=app
6th Jun 2020, 8:48 PM
ogoxu
ogoxu - avatar
+ 1
Gen2oo I know that. However, we can still specify the range and use scanf, right? It's not as dangerous as gets. Also thanks for your warning about EOF. Didn't know that one since the code worked by chance. Edit: Wanted to use fscanf and find something: https://support.microsoft.com/en-sg/help/60336 Find this one is quite helpful. You can check this method too.
7th Jun 2020, 4:40 AM
ogoxu
ogoxu - avatar