I need to find an alternative way to read command line arguments, and validate them , without using scanf .
Any suggestions would be appreciated
Im keen to learn more about getchar() , would that be of use hre or not ?C programming scanf Alternative?
There are quite a few functions that can be used as well as a few variations of the scanf function
The gets function.
- char *gets(char *s);
wchar_t *_getws(wchar_t *s); // Unicode version
Description
Gets a string from stdin.
gets collects a string of characters terminated by a new line from the standard input stream stdin and puts it into s. The new line is replaced by a null character (\0) in s.
gets allows input strings to contain certain whitespace characters (spaces, tabs). gets returns when it encounters a new line; everything up to the new line is copied into s.
The gets function is not length-terminated. If the input string is sufficiently large, data can be overwritten and corrupted. The fgets
function provides better control of input strings.
The getch function
int getch(void);
Description
Gets character from keyboard, does not echo to screen.
getch reads a single character directly from the keyboard, without echoing to the screen.
Return Value
getch returns the character read from the keyboard.
There is getc function.
int getc(FILE *stream);
wint_t getwc(FILE *stream);
Description
Gets character from stream.
getc returns the next character on the given input stream and increments the stream's file pointer to point to the next character
Return Value
On success, getc returns the character read, after converting it to an int without sign extension.
On end-of-file or error, it returns EOF.
The getchar functionC programming scanf Alternative?
Well Jim seems to be a bit confused as he was using C++ instead of C.
The scanf function is the most flexible the getch() function takes in a character at a time, the only way you can have is to take in a character at a time, the gets() function takes in a string input which is a bit useful.
and from the conio.h collection there is getch() and getche() and cscanf()
if you want a real change, use C++'s iostream
#include %26lt;string%26gt;
#include %26lt;iostream%26gt;
using namespace std;
int a, b, c;
string s;
cout%26lt;%26lt;';Enter 3 integers and a string:';;
cin%26gt;%26gt;a%26gt;%26gt;b%26gt;%26gt;c%26gt;%26gt;s;
cout%26lt;%26lt;';a=';%26lt;%26lt;a%26lt;%26lt;';, b=';%26lt;%26lt;b%26lt;%26lt;';, c=';%26lt;%26lt;c%26lt;%26lt;';, s=\';';%26lt;%26lt;s%26lt;%26lt;';\';';%26lt;%26lt;endl;
Subscribe to:
Post Comments (Atom)
c++ language examples for beginner coders
ReplyDeletec++ code data encoding techniques