#include
#include
int getline(int s[], int lim)
{
int c, i, j;
j = 0;
for (i = 0; (c = getchar()) != EOF && c != '\n'; ++i)
if (i < lim - 2)
{
s[j] = c;
++j;
}
if ('\n' == c)
{
s[j] = c;
++i;
++j;
}
s[j] = '\0';
return i;
}
int remove(int s[])
{
int i;
i = 0;
while (s[i] != '\n')
{
++i;
}
--i;
while ((i >= 0) && (' ' == s[i]) || ('\t' == s[i]))
{
--i;
}
if (i >= 0)
{
++i;
s[i] = '\n';
++i;
s[i] = '\0';
}
return i;
}
int main()
{
int i;
int line[1000];
while (getline(line, 1000) > 0)
{
// if (remove(line) > 0)
printf("%s", line);
//用这个可以打印全部。。。
/*for ( i = 0; line[i] != '\0'; i++)
{
printf("%c", line[i]);
}*/
putchar('\n');
}
system("pause");
return 0;
}
s[i] == '\0'; 改成一个=号吧
既然想要输入输出字符 建议line改成char类型 getline传递的参数也改成char类型
这个是我拷贝代码时候敲错的原因。。。 我int只是为了放其他字符,还是谢谢了,应该不是我想要的
不知道你的程序目的是什么,现在看来line数组里存的都是用户输入字符的ascii码,然后用%s格式输出这个int数组应该是做不到的
http://bbs.csdn.net/topics/380230102
确实是int 和char的问题 但是为什么会输出一个h。。
// ConsoleApplication6.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include
#include
int getline(char s[], int lim)
{
int c, i, j;
j = 0;
for (i = 0; (c = getchar()) != EOF && c != '\n'; ++i)
if (i < lim - 2)
{
s[j] = c;
++j;
}
if ('\n' == c)
{
s[j] = c;
++i;
++j;
}
s[j] = '\0';
return i;
}
int remove(int s[])
{
int i;
i = 0;
while (s[i] != '\n')
{
++i;
}
--i;
while ((i >= 0) && (' ' == s[i]) || ('\t' == s[i]))
{
--i;
}
if (i >= 0)
{
++i;
s[i] = '\n';
++i;
s[i] = '\0';
}
return i;
}
int main()
{
int i;
char line[1000];
while (getline(line, 1000) > 0)
{
// if (remove(line) > 0)
printf("%s", line);
/*for ( i = 0; line[i] != '\0'; i++)
{
printf("%c", line[i]);
}*/
putchar('\n');
}
system("pause");
return 0;
}
上面的新代码已经可以运行了,或者可以试试用C++的读入
#include "iostream"
//#include "cstring"
using namespace std;
int main(){
string line;
while (getline( cin,line))
{
cout<<line<<endl;
cout<<endl;
//putchar('\n');
}
// system("pause");
return 0;
}