📄 ccc.txt
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int getline(char s[], int lim);
void copy(char to[], char from[]);
/* 打印用户允许的最长输入行 */
int main(void)
{
int iMaxLen; /* 最大允许输入的长度 */
int iLen; /* 当前行的长度 */
int iMax; /* 当前的最长行的长度 */
char *stLine; /* 当前行的内容 */
char *stLongest; /* 保存最长的行 */
/* 要求用户输入允许的最大行长度 */
while (1)
{
printf("Please input the max line length: ");
scanf("%d", &iMaxLen);
if (iMaxLen <= 0)
{
printf("\n\nInput error! \n\n");
continue;
}
else
printf("\n\n");
break;
}
//分配内存
if ((stLine = (char *)malloc(iMaxLen)) == NULL)
{
printf("Not enough memory to allocate buffer\n");
exit(1);
}
if ((stLongest = (char *)malloc(iMaxLen+1)) == NULL) //加1防止溢出
{
printf("Not enough memory to allocate buffer\n");
exit(1);
}
//初始化内存空间
iMax = 0;
memset(stLine, '\0', iMaxLen);
memset(stLongest, '\0', iMaxLen+1);
while ((iLen = getline(stLine, iMaxLen)) > 0) //当有输入时
if (iLen > iMax)
{
iMax = iLen; //保存最大行的长度
strcpy(stLongest, stLine);
}
if (iMax > 0)
printf("%s\n", stLongest);
free(stLine);
free(stLongest);
return 0;
}
/* getline: 将一行读入到s中,并返回其长度 */
int getline(char s[], int lim)
{
int c, i;
for (i = 0; i < (lim -1) && (c = getchar()) != EOF && c != '\n'; i++)
{
s[i] = c;
}
if ('\n' == c)
{
s[i] = c;
i++;
}
s[i] = '\0';
return i;
}
/* copy: 从from拷贝到to */
void copy(char to[], char from[ ])
{
int i;
i = 0;
while ((to[i] = from[i]) != '\0')
i++;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -