Unformatted I/O functions อ่านอินพุตเดี่ยวจากผู้ใช้ และแสดงเอาต์พุตที่คอนโซล
Unformatted Input Functions
getchar()
อ่านตัวอักษรจากแป้นพิมพ์
Variablename = getchar();
เช่น char a; a = getchar();
ตัวอย่างโปรแกรม getchar()
#include<stdio.h> int main(){ char ch; FILE *fp; fp=fopen("file.txt","w"); printf("enter the text then press cntrl Z:\n"); while((ch = getchar())!=EOF){ putc(ch,fp); } fclose(fp); fp=fopen("file.txt","r"); printf("text on the file:\n"); while ((ch=getc(fp))!=EOF){ if(fp){ char word[100]; while(fscanf(fp,"%s",word)!=EOF){ printf("%s\n", word); } fclose(fp); }else{ printf("file doesnot exist"); } } return 0; }
Output
enter the text then press cntrl Z: This is an example program on getchar() ^Z text on the file: This is an example program on getchar()
gets()
อ่านสตริงจากแป้นพิมพ์
gets(variablename);
ตัวอย่าง
#include<stdio.h> #include<string.h> main(){ char str[10]; printf("Enter your name: \n"); gets(str); printf("Hello %s welcome to Tutorialspoint", str); }
Enter your name: Madhu Hello Madhu welcome to Tutorialspoint
Unformatted Output Functions
putchar()
แสดงตัวอักษรบนหน้าจอ
putchar(variablename);
เช่น putchar('a');
puts()
แสดงสตริงบนหน้าจอ
puts(variablename);
เช่น puts("tutorial");
ตัวอย่างโปรแกรม putc และ getc
#include <stdio.h> int main(){ char ch; FILE *fp; fp=fopen("std1.txt","w"); printf("enter the text.press cntrl Z:\n"); while((ch = getchar())!=EOF){ putc(ch,fp); } fclose(fp); fp=fopen("std1.txt","r"); printf("text on the file:\n"); while ((ch=getc(fp))!=EOF){ putchar(ch); } fclose(fp); return 0; }
Output
enter the text.press cntrl Z: This is an example program on putchar() ^Z text on the file: This is an example program on putchar()