3

I am writing text to a file using the following program.

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int ch;
    FILE *fp;
    fp = fopen("myfile.txt", "w");

    if(fp == NULL)
    {
        printf("Error opening file\n");
        exit(1);
    }

    printf("Press Ctrl+D to stop \n\n");

    printf("Enter text: ");

    while( (ch=getchar()) != EOF )
    {
        fputc(ch, fp);
    }

    fclose(fp);

}

Let's say the input is:

Press Ctrl+D to stop \n\n

Enter text: this is a test
^Z

My question is the end of file character (ASCII value 26) will be written to the file or not ?

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
Cody
  • 2,480
  • 5
  • 31
  • 62
  • Your question appears to assume Windows. Not even that, but DOS. Should adjust your tags. – Zan Lynx Sep 26 '16 at 06:05
  • 1
    How did you conclude that `^Z` is the ASCII character represented by 26? – R Sahu Sep 26 '16 at 06:07
  • @RSahu It is actually Ctrl+Z – Cody Sep 26 '16 at 06:07
  • 4
    You could simply view the file in a hex editor / hexdump utility. – Jan Sep 26 '16 at 06:09
  • @Cody, I understood that. On what platform does `Ctrl+Z` translate to the ASCII character represented by 26? – R Sahu Sep 26 '16 at 06:10
  • @RSahu Window of course – Cody Sep 26 '16 at 06:10
  • Usually shells convert these key combination to provide certain actions ctrl-z means SIGSTOP is sent to your program on Linux at least. Ctrl-D is [EOF](http://www.cplusplus.com/reference/cstdio/getchar/) – JamesWebbTelescopeAlien Sep 26 '16 at 06:17
  • No Ctrl-Z here http://www.asciitable.com/ nor here https://en.wikipedia.org/wiki/ASCII. – alk Sep 26 '16 at 06:17
  • @NulledPointer so in Linux nothing will be written after "this is a test" ? – Cody Sep 26 '16 at 06:21
  • 1
    As per your program it will quit without writing anything in the file because ctrl-D means EOF as i said before – JamesWebbTelescopeAlien Sep 26 '16 at 06:23
  • OT: It should be `int main(void)` at least, BTW. – alk Sep 26 '16 at 06:34
  • @NulledPointer i verified using a hex editor that it is not writing ^Z to the file. But there is another problem. Let's say i entered `abc^Z` followed by a newline. In this case, the character ^Z is written to the file. Since getchar() stops reading as soon as it encountered ^Z it should stop reading character but here it is not stopping. Why ? – Cody Sep 26 '16 at 06:38
  • @alk Actually there is: (Wikipedia page) Dec 26, `SUB`, `^Z` - Follow [the link](https://en.wikipedia.org/wiki/Substitute_character) and you get *"Standard keyboards transmit this code when the Ctrl and Z keys are pressed simultaneously (...)."*. – Holt Sep 26 '16 at 06:42
  • The answer is here: http://stackoverflow.com/a/18582991/694576 or even more detailed here: http://stackoverflow.com/q/7373386/694576 – alk Sep 26 '16 at 06:46
  • @RSahu basically every platform with a control key. Z is 0x5A (decimal 90). Clear the 0x40 bit and you get 0x1A (decimal 26). Or just remember that Z is the 26th letter. :) – hobbs Sep 26 '16 at 06:49

1 Answers1

0

I verified using a hex editor in Windows ^Z character is not written to the file.

Cody
  • 2,480
  • 5
  • 31
  • 62