c - 如何在 #include<stdio.h> 和 int main() { } 之间添加换行符

标签 c pointers io getchar

我正在编写一个程序,该程序使用 getchar() 将输入作为字符序列,并使用 fopen() 和 putc() 函数创建 pro.cpp 源代码文件。

当我编译该程序创建的源代码文件时,它会生成错误。 如果我从新行开始 int main{} ,可以解决此错误。所以我需要在 #include 和 int main{ } 之间输入换行符

我不知道该怎么做。

#include<stdio.h> 
int main()
{
    FILE *fp;
    char pgm[100];
    char *p_pgm;

 p_pgm=pgm;

 printf("Enter the program as a input");

 while((*p_pgm=getchar())!=10)
     p_pgm++;

 *p_pgm ='\0';


 fp =fopen("e:\\pro.cpp","w");
 while(*p_pgm!='\0')
     putc(*p_pgm++,fp);  


 fclose(fp);


}

最佳答案

当您找到换行符(ascii 代码 10)时,您将停止读取字符,然后将其替换为“\0”。因此,如果将其写入输出文件,它显然不包含任何换行符。您的程序获取的输入源是否没有换行符?

关于c - 如何在 #include<stdio.h> 和 int main() { } 之间添加换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16837583/

相关文章:

c - 不使用双指针交换字符串

c - 如何使用数组指针打印结构成员?

c++ - 在以下上下文中,指针和数组之间有什么区别?

c++ - 无法成功比较字符串

c - 'fread()' 、 'fwrite()' 和 'fseek()' 的意外行为

c - 为什么我们不能在结构中初始化成员?

C 结构 : Initialized Strings become invalid

c - 将包含 N 个十进制数字的 uint64_t 数据分成两个 uint32_t 并修剪 C 中的输出

scala - Scala 分配评估 Unit 而不是分配的值的动机是什么?

c - 不需要的输出 - 打印出两个数字