-2

i'm writing a program that takes input as sequence of characters using getchar() and create pro.cpp source code file using fopen() and putc() functions.

when i compile this source code files created by this program it generate error. this error can be resolved if i start int main{} from new line. so i need to enter new line character between #include and int main{ }

i don't know how to do it.

#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);


}
siddstuff
  • 1,215
  • 4
  • 16
  • 37
  • 4
    is there a reason you can't use putc('\n',fp); – Andrew W May 30 '13 at 13:32
  • 1
    Surely, because of the `*p_pgm ='\0'`, the bottom `while` loop can never execute because `*p_pgm` **is** `\0`? – slugonamission May 30 '13 at 13:42
  • 1
    But doesn't the program you take as input contain newlines characters? Because otherwise you will obtain only the first line, as in the while loop you stop as you find a '\n', that is equivalent to 10 in ascii representation, as I said in my answer. – user2302436 May 30 '13 at 13:54
  • Change `10` to `'\n'`; they're (probably) the same value, but `'\n'` is much clearer. – Keith Thompson May 30 '13 at 15:12

1 Answers1

0

You stop reading characters when you find a newline (ascii code 10), then replace it with '\0'. So if you write this to the output file, it obviously doesn't contain any newline. Is the input source taken by your program without newlines?

user2302436
  • 518
  • 2
  • 9
  • Why a down vote? If the input you pass to your program contains the newlines ('\n'), you shouldn't add it manually, as getchar reads the newlines, too. But in your while loop you stop when you find a newline. So you should specify how is your input, is it all on one line, or you pass it correctly with all the newlines? – user2302436 May 30 '13 at 13:46