Skip to content

Instantly share code, notes, and snippets.

@dasbluehole
Created July 25, 2022 04:34
Show Gist options
  • Save dasbluehole/d603ca617c1d9d862079df1f4594ced6 to your computer and use it in GitHub Desktop.
Save dasbluehole/d603ca617c1d9d862079df1f4594ced6 to your computer and use it in GitHub Desktop.
Scanner class kind of input for C
/*
* reader.c
*
* Copyright 2022 Ashok Shankar Das <ashok@localhost.localdomain>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INITIAL_SIZE 512
/**
* Read data from stdin and return the input
*/
char* read_input() {
size_t offset = 0;
size_t size = INITIAL_SIZE;
char* buffer = (char*) malloc(size);
int ch = -1;
while ((ch = getchar()) != '\n') {
if (offset == size - 1) {
buffer = (char*)realloc(buffer, size *= 2);
if(buffer == NULL) {
perror("Call to realloc() failed");
abort();
}
}
buffer[offset++] = (char)ch;
}
return buffer;
}
#define readString read_input
int readInt(char *buffer)
{
int ret;
//buffer should not be null
if(buffer != NULL)
{
//now check buffer length should not be 0
if(strlen(buffer)>0)
{
ret= atoi(buffer);
if(buffer!=NULL) free(buffer);
return(ret);
}
}
fprintf(stderr,"Unable to parse input");
if(buffer!=NULL) free(buffer);
exit(EXIT_FAILURE);
}
float readFloat(char *buffer)
{
float ret;
//buffer should not be null
if(buffer != NULL)
{
//now check buffer length should not be 0
if(strlen(buffer)>0)
{
ret= atof(buffer);
if(buffer!=NULL) free(buffer);
return(ret);
}
}
fprintf(stderr,"Unable to parse input");
if(buffer!=NULL) free(buffer);
exit(EXIT_FAILURE);
}
double readDouble(char *buffer)
{
double ret;
char *endpointer;
//buffer should not be null
if(buffer != NULL)
{
//now check buffer length should not be 0
if(strlen(buffer)>0)
{
ret= strtod(buffer,&endpointer);
if(buffer!=NULL) free(buffer);
return(ret);
}
}
fprintf(stderr,"Unable to parse input");
if(buffer!=NULL) free(buffer);
exit(EXIT_FAILURE);
}
long readLong(char *buffer)
{
long ret;
int base =0;
char *endpointer;
//buffer should not be null
if(buffer != NULL)
{
//now check buffer length should not be 0
if(strlen(buffer)>0)
{
ret= strtol(buffer,&endpointer,base);
if(buffer!=NULL) free(buffer);
return(ret);
}
}
fprintf(stderr,"Unable to parse input");
if(buffer!=NULL) free(buffer);
exit(EXIT_FAILURE);
}
int main(int argc, char **argv)
{
printf("Enter a string ");
char *str = readString();
printf("%s\n",str);
printf("enter an integer: ");
int x= readInt(read_input());
printf("integer x = %d\n",x);
printf("enter a floating point number: ");
float y= readFloat(read_input());
printf("float y = %f\n",y);
printf("enter a double: ");
double z = readDouble(read_input());
printf("double z = %lf\n",z);
printf("enter a long: ");
long l = readDouble(read_input());
printf("long l = %ld\n",l);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment