0

I want to write a text and save it in .txt using <stdio.h> and <stdlib.h>. But with this way, I only could save one line, no more.

int main()
{
   file*pf;
   char kar;

   if ((pf = fopen("try.txt","w")) == NULL)
   {
      printf("File couldn't created!\r\n");
      exit(1);
   }

   while((kar=getchar()) != '\n')
      fputc(kar, pf);

   fclose(pf);
}
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
Embek
  • 27
  • 1
  • 2
  • 11
  • 2
    Note that C is a case-sensitive language. `file *pf` is incorrect and `FILE *pf` is correct. Also, as I pointed out in a comment to one of the answers, the return type of `getchar()` is `int`, not `char`. It has to be able to encode any valid character and also EOF, which is distinct. You can't do that in a `char`, so it returns `int`. If your plain `char` is a signed type, someone typing character code 0xFF (usually ÿ, U+00FF, LATIN SMALL LETTER Y WITH DIAERESIS) will be treated as typing EOF; if plain `char` is unsigned, no value will match EOF. – Jonathan Leffler Jun 15 '15 at 05:43

3 Answers3

3

Instead of

char kar;

...

while((kar=getchar()) != '\n')
   fputc(kar, pf);

use

int kar;
// Use int for type of kar

...

while((kar=getchar()) != EOF )
                     //  ^^^
   fputc(kar, pf);
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • the main problem is you can not input a line break. check [this][1] [1]: http://stackoverflow.com/questions/421860/capture-characters-from-standard-input-without-waiting-for-enter-to-be-pressed – Jiang YD Jun 15 '15 at 05:35
  • 2
    This would be great advice, but the conversion of `int` to `char` transforms the `EOF` to a character value... even though *`EOF` isn't a character value*. – autistic Jun 15 '15 at 05:42
  • 2
    Note that the return value from `getchar()` is an `int`, not a `char`, so storing it in a `char` will cause confusion one way or another. – Jonathan Leffler Jun 15 '15 at 05:42
2

'\n' means end of line. Here, you are looking for end of file. So, use macro EOF instead of '\n' in your code.

saurabh agarwal
  • 2,124
  • 4
  • 24
  • 46
  • Let assume that I want to input 3 lines, then how I make it stop in 3rd line? @saurabh-agarwal – Embek Jun 15 '15 at 05:41
  • 1
    @Embek: Take your prior solution and count the number of lines, and adapt the while loop to break at != \n && numberOfLines <= 3 – ckruczek Jun 15 '15 at 05:43
  • @Embek , Or enter CTRL+D (if you are using linux/unix/osx) or else press CTRL+Z (windows/DOS) – Spikatrix Jun 15 '15 at 05:48
  • @ckruczek and how to use that? i don't really understand. – Embek Jun 15 '15 at 06:01
  • Well @Embek, just add a counter to your program and increment it every time a '\n' was recognized. Not too hard to manage I guess – ckruczek Jun 15 '15 at 06:02
  • @Embek , In that case, use ckruczek's suggestion `int counter = 0;` and then `while( counter < 3 && (kar=getchar()) != EOF) { fputc(kar, pf); if(kar == '\n') counter++;}` and make sure `kar` is an `int`, not a `char`. – Spikatrix Jun 15 '15 at 06:09
0

Full Working Code which puts multiple line into your text file. To end the input from terminal just press Ctrl + Z

#include <stdio.h>
#include <stdlib.h>

int main()
{
   FILE *pf;
   char kar;

   if ((pf = fopen("try.txt","w")) == NULL)
   {
      printf("File couldn't created!\r\n");
      exit(1);
   }

   while((kar=getchar()) != EOF)
      fputc(kar, pf);

   fclose(pf);

   return 0;
}