Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /* practice file for learning to program in C */
  5.  
  6. int getLine(char line[], int max);
  7.  
  8. int main(int argc, char **argv)
  9. {
  10. char line[256];
  11.  
  12. while(getline(line, 256) != EOF)
  13. printf("you typed \"%s\"\n", line);
  14.  
  15. return 0;
  16. }
  17.  
  18.  
  19.  
  20. // function for reading one line of input at a time
  21. int getLine(char line[], int max)
  22. {
  23. int nch = 0;
  24. int c;
  25. max = max - 1; /* leaves room for '\0' */
  26.  
  27. while ((c = getchar()) != EOF) {
  28. if (c == '\n')
  29. break;
  30. if (nch < max) {
  31. line[nch] = c;
  32. nch++;
  33. }
  34. }
  35. if (c == EOF && nch == 0)
  36. return EOF;
  37.  
  38. line[nch] = '\0';
  39. return nch;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement