|
发表于 2016-11-3 09:46:14
|
显示全部楼层
- #include <cstdio>
- #define BUFFER_SIZE 1024
- unsigned int get_line(char* out,const unsigned int size);
- int main()
- {
- char str[BUFFER_SIZE] = {0};
- unsigned int n;
- while(true)
- {
- printf("$>");
- n = get_line(str,BUFFER_SIZE);
- if(n == 1 && str[0] == 'q')
- {
- break;
- }
- printf("strlen = %u strval= %s\n",n,str);
- }
- return 0;
- }
- unsigned int get_line(char* out,const unsigned int size)
- {
- unsigned int sum = 0;
- while(true)
- {
- if(sum + 1 == size)
- {
- break;
- }
- out[sum] = getchar();
- if(out[sum++] == '\n')
- {
- break;
- }
- }
- out[sum] = 0;
- return sum - 1;
- }
复制代码 |
|