Problem with reading/writing on the terminal line

49 Views Asked by At

I am trying to write a function that reads an input from the terminal using getchar() and stores it in a string and then display it in 2 different formats.

  • The read line should be stored in the string followed by the null-character ('\o').
  • The newline character shouldn't be stored in the string.

The problem I'm encountering is the fact that when the user inputs less characters than the assigned length, the enter key must be pressed twice, and then a newline character also gets stored in the string. No solutions regarding the buffer, which I suppose to be the culprit, found in other locations were of any help. Any suggestion and explanation is welcomed. Thanks in advance.

This is my last version of the code:

#include <stdio.h>
#include <ctype.h>
#include <string.h>


#define LENGTH 4//30// i will try a shorter lenth for troubleshooting

void flushBuffer(void){
    while((getchar()) != '\n' );// consume the rest of a partially-read line
}
void readLine(char str[], int length){
    for(int j = 0; j < length - 1; j++){ 
        if(str[j] == '\n'){
            str[j] = '\0';
        }
    }
    int i = 0;
    do {
        str[i] = getchar();
        i++;
    } while (i < length - 1  && str[i - 1] != '\n');
    str[i] = '\0';
}
void printArray(char a[], int length){
  printf("[");
  for(int i = 0; i < length; i++){
    if(a[i] == '\n'){
      printf("\\n");
    }else if(a[i] == '\0'){
      printf("\\0");
    }else{
      printf("%c", a[i]);
    }
    if(i != length - 1){
      printf(",");
    }
  }
  printf("]\n");
}
void printChar (char c) {
  putchar(c);
  fflush(NULL);
}
void printString (char s[]) {
  int currentChar = 0;
  printf("\"");
  do{
  printChar(s[currentChar]);
  currentChar++;
  }while (s[currentChar] != '\0');
  printf("\"\n");
}
int main (void) {
  char inputString[LENGTH];
  do{
    printf("String? ");
    readLine(inputString, LENGTH);
    flushBuffer();
    printArray(inputString, LENGTH);
    printString(inputString);
    flushBuffer();
  }while(1);
}

I tried writing a funtion that should flush my buffer ( flushBuffer() ) and in the function that has the task of reading the input line (readLine()) I tried getting rid of any stored '\n'.

1

There are 1 best solutions below

2
Alex On

This is the version of my code that is working properly. As suggested by @Barmar, the flushBuffer(), function that consumes all the characters including the newline that didn't have room in the string, was called online in the case when the '\n' wasn't detected by the while loop.

#include <stdio.h>
#include <ctype.h>
#include <string.h>


#define LENGTH 30

void flushBuffer(void){
    while((getchar()) != '\n' );//^part of Question: consume the rest of a partially-read line
}
void readLine(char str[], int length){
    int i = 0;
    while (i < length - 1){
        str[i] = getchar();
        if (str[i] == '\n'){
          str[i] = '\0';
            return;
        }
        i++;
    }
    str[i] = '\0';
    flushBuffer();
}
void printArray(char a[], int length){
  printf("[");
  for(int i = 0; i < length; i++){
    if(a[i] == '\n'){
      printf("\\n");
    }else if(a[i] == '\0'){
      printf("\\0");
    }else{
      printf("%c", a[i]);
    }
    if(i != length - 1){
      printf(",");
    }
  }
  printf("]\n");
}
void printChar (char c) {
  putchar(c);
  fflush(NULL);
}
void printString (char s[]) {
  int currentChar = 0;
  printf("\"");
  while (s[currentChar] != '\0'){
  printChar(s[currentChar]);
  currentChar++;
  }
  printf("\"\n");
}
int main (void) {
  char inputString[LENGTH] = "";
  do{
    printf("String? ");
    readLine(inputString, LENGTH);
    printArray(inputString, LENGTH);
    printString(inputString);
  }while(1);
}