C 如何从字符串中删除换行符
我有一个文本文件,下面显示了以下字符C 如何从字符串中删除换行符,c,C,我有一个文本文件,下面显示了以下字符 An investment in knowledge pays the best interest An investment in knowledge pays the best interest 我的代码我的代码应该读取文件并在一行中打印出来,如下所示 An investment in knowledge pays the best interest An investment in knowledge pays the best inte
An investment
in knowledge
pays the
best interest
An investment in knowledge pays the best interest
我的代码我的代码应该读取文件并在一行中打印出来,如下所示
An investment
in knowledge
pays the
best interest
An investment in knowledge pays the best interest
我的代码如下
int main()
{
FILE *fp = fopen("C:\\Users\\abiye\\Downloads\\abiye.txt", "r");
char c;
int d = 0;
char arr[200];
do
{
c = fgetc(fp);
printf("%c",c);
d = d + 1;
if (c == '\n') {
putchar(' ');
}
arr[d] = c;
}
while (c != EOF);
fclose(fp);
return 0;
}
但是没有给我想要的结果,我得到了下面的结果
An investment
in knowledge(A space is added at the beginning of this string and the rest that follow)
pays the
best interest
任何帮助都将不胜感激。这是因为您在检查之前正在打印字符 您要做的是检查然后打印
do
{
c = fgetc(fp);
d += 1;
if (c == '\n') {
putchar(' ');
}
else
putchar(c);
arr[d] = c;
}
while (c != EOF);
这是因为您在检查之前正在打印字符 您要做的是检查然后打印
do
{
c = fgetc(fp);
d += 1;
if (c == '\n') {
putchar(' ');
}
else
putchar(c);
arr[d] = c;
}
while (c != EOF);
@user3121023:但是标准IO库被迫将其转换为单个
'\n'
。顺便说一句,如果您不知道如何陷入严重问题,请摆脱字符arr[200]代码>和相应的arr[d]=c代码>一次又一次。@user3121023:但标准IO库被迫将其转换为一个'\n'
。顺便说一句,如果您不知道如何陷入严重问题,请摆脱字符arr[200]代码>和相应的arr[d]=c代码>一次并且非常好。也许还可以处理\r
和\f
和\t
也许还可以处理\r
和\f
和\t