pastebin - collaborative debugging tool
polynomial.kpaste.net RSS


C ReadLine
Posted by Anonymous on Thu 28th Apr 2011 18:40
raw | new post

  1. char* readLine()
  2. {
  3.         int n = 0;
  4.         int sz = 1024;
  5.         char* buffer = (char*)malloc(sz);
  6.         memset(buffer, 0, sz);
  7.         while((buffer[n]=getc(stdin)) != '\n')
  8.         {
  9.                 n++;
  10.                 if(n == (sz - 1)) {
  11.                         // extend buffer
  12.                         char* newbuffer = (char*)malloc(sz + 1024);
  13.                         memset(newbuffer, 0, sz + 1024);
  14.                         memcpy(newbuffer, buffer, sz);
  15.                         sz += 1024;
  16.                         free(buffer);
  17.                         buffer = newbuffer;
  18.                 }
  19.         }
  20.         if(n < 1) ExitProcess(0);
  21.         buffer[n] = 0; // strip \n from it
  22.         return buffer;
  23. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}





All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at