C: Use the FGETC function to enter characters from the keyboard one by one, write to the disk file with the FPUTC function

Source: Internet
Author: User

Use the fgetc function to enter characters from the keyboard one by one, using the fputc function to write to the disk file.

Solution: Program:

#include <stdio.h>

#include <stdlib.h>//exit to terminate the program

int main ()

{

file* FP;

Char ch, filename[10];

printf ("Please enter the file name used:");

scanf ("%s", &filename);

if (fp = fopen (filename, "w")) = = = NULL)

{

printf ("Cannot open this file \ n");

Exit (0);

}

ch = getchar ();

printf ("Please enter a string ready to be stored to disk (end with #):");

ch = getchar ();

while (ch!= ' # ')

{

FPUTC (CH, FP);

Putchar (CH);

ch = getchar ();

}

Fclose (FP);

Putchar (10);//output a line break to the screen

return 0;

}

Results:

Please enter the name of the file used : File1.txt

Please enter a string to be stored to disk (end of # ): Yaoyao and xiaoxiao#

Yaoyao and Xiaoxiao

Please press any key to continue ...


This article is from the "Rock Owl" blog, please be sure to keep this source http://yaoyaolx.blog.51cto.com/10732111/1748102

C: Use the FGETC function to enter characters from the keyboard one by one, write to the disk file with the FPUTC function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.