informaticage

get lines

Jul 31st, 2021
1,098
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. size_t getlines(char* s, size_t lim) {
  5.   size_t n = 0;
  6.   char c;
  7.  
  8.   while(n < lim && (c = getchar()) != EOF && c != '\n') {
  9.     *s = c;
  10.     n++;
  11.     s++;
  12.   }
  13.  
  14.   if (c == '\n') *s = '\n';
  15.   return n;
  16. }
  17.  
  18. int main() {
  19.   char* str = malloc(sizeof(char) * 15);
  20.   getlines(str, 15);
  21.   printf("%s", str);
  22.   return 0;
  23. }
RAW Paste Data