200 likes | 212 Views
Learn about C's printf, scanf, sprintf, sscanf functions, different I/O functions, and format specifiers. Master I/O classification and File Operations in C.
E N D
char name[5][100]; C Input / Output? • C only has no provision for I/O. • Then printf and scanf possess what ? • So these functions are actually are standard library funcions and there are many as well. • The numerous library functions available for I/O can be classified into as the below. • Console I/O functions • Disk I/O functions • Port I/O functions
Console Input and Output Functions Unformatted Functions Formatted Functions
char name[5][100]; C Format Specifiers
C Input / Output? main() { printf(“\n %f%f%f”,5.0,13.5,133.9); printf(“\n %f%f%f”,305.0,1200.9,3005.3); } main() { printf(“\n %10.1f%0.1f%0.1f”,5.0,13.5,133.9); printf(“\n %0.1f%0.1f%0.1f”,305.0,1200.9,3005.3); } main() { char str1[] = “IIIT”; char str2[] = “Sricity”; char str3[] = “Delhi” printf(“\n %20sf%20sf”,str1,str2); printf(“\n %20sf%20sf”,str1,str3); } main() { int weight=63; printf(“\n weight is %d kg”,weight); printf(“\n weight is %2d kg”,weight); printf(“\n weight is %4d kg”,weight); printf(“\n weight is %6d kg”,weight); printf(“\n weight is %-6d kg”,weight); }
char name[5][100]; C Format Specifiers main() { char ch=‘a’; inti=125; float a = 12.44; char s[] = “hello world”; printf(\n%c%d%f”,ch,ch,ch); printf(\n%s%d%f”,s,s,s); printf(\n%c%d%f”,i,i,i); printf(\n%f%d\n”,a,a); }
char name[5][100]; sprintf() and sscanf() main() { char ch=‘A’; inti=12; float a = 3.14; char str[30]; printf(\n%d%c%f”,i,ch,a); sprintf(str,”%d%c%f”,i,ch,a); printf(\n”%s”,str); } • Printf prints on the console, where as sprintf() stores them in a string • Sscanf(), is counter part of sprintf(). It allows us to read characters from a string and to convert and store them in C variables according to specifiers • It is a good tool for doing in-memory conversions of characters.
Example Program: sprintf, sscanf /*C Program#134 - sscanf() & sprintf() functions in C. 1. sscanf() reads characters from an array & stores them in different arguments. 2. sprintf() prints data items in the form of characters into an array. #include<stdio.h> int main() { char fullname[60],shortname[30],firstname[20],middlename[20],lastname[20]; char bdate[10]; intday,month,year; printf("\n\n Enter Full Name (FirstNameMiddleNameLastName) : "); gets(fullname); //reading fullname with spaces. sscanf(fullname,"%s %s %s",firstname,middlename,lastname); //reading firstname,middlename & lastname from fullname sprintf(shortname,"%.1s. %.1s. %s",firstname,middlename,lastname); //printing firstname and middlename initials with lastname into shortname printf("\n\n Your name ( Format - Without MiddleName) : %s %s",firstname,lastname); //printing extracted firstname and lastname printf("\n\n____________________________________________________________________"); printf("\n\n Enter Date of Birth (Format : dd/mm/yyyy e.g. 21/09/2002) : "); gets(bdate); //reading birth-date in string format sscanf(bdate,"%2d/%2d/%4d",&day,&month,&year); //reading data from bdate string and extracting to individual int variables day,month & year printf("\n\n Date of Birth (Format - YYYY-MM-DD) : %d-%d-%d",year,month,day); //self-explained - see output printf("\n\n Date of Birth (Format - MM-DD-YYYY) : %d-%d-%d",month,day,year); //self-explained - see output printf("\n\n"); return 0; }
char name[5][100]; Disk I/O Functions • There are two types of Disk I/O available • Standard I/O or Stream I/O – high level • System I/O – low level • Basic difference between them is, in high level buffer management is automatically done but in other case, programmer is expected to do. • A FILE is a place on the physical disk where information is stored. • Why file is needed ? • When a program is terminated, the in-memory data is lost. Storing in a file will preserve your data even for later usage. • For a large number of data, it will take a lot of time to enter them all.However, if you have a file containing all the data, you can easily access the contents of the file using few commands in C. • Moving data from one computer to another without any changes is possible • Types of FILE • Text File • Binary File
File Opening Modes • FILE *fptr; • fptr = fopen(“fileopen_name”,”mode”); • For Ex: • fopen(“file_name”,”mode”); • Closing a file • fclose(fptr);
File Opening Modes with flag for low level • For Ex: • Open(“temp.dat”,OREAD|O_BINARY)
Reading from a text file /* C program to read a text file */ #include <stdio.h> #include <stdlib.h> intmain() { intnum; FILE *fptr; if ((fptr = fopen("C:\\program.txt","r")) == NULL){ printf("Error! opening file"); // Program exits if the file pointer returns NULL. exit(1); } fscanf(fptr,"%d", &num); printf("Value of n=%d", num); fclose(fptr); return 0; }
Writing to a text file /* C program to read a text file */ #include <stdio.h> #include <stdlib.h> intmain() { intnum; FILE *fptr; fptr = fopen("C:\\program.txt","w"); if(fptr == NULL) { printf("Error!"); exit(1); } printf("Enter num: "); scanf("%d",&num); fprintf(fptr,"%d",num); fclose(fptr); return 0; }
Reading from a bin file: fread() #include <stdio.h> #include <stdlib.h> structthreeNum { int n1, n2, n3; }; intmain() { int n; structthreeNumnum; FILE *fptr; if ((fptr = fopen("C:\\program.bin","rb")) == NULL){ printf("Error! opening file"); // Program exits if the file pointer returns NULL. exit(1); } for(n = 1; n < 5; ++n) { fread(&num, sizeof(structthreeNum), 1, fptr); printf("n1: %d\tn2: %d\tn3: %d", num.n1, num.n2, num.n3); } fclose(fptr); return 0; }
Writing to a bin file: fwrite() #include <stdio.h> #include <stdlib.h> structthreeNum { int n1, n2, n3; }; int main() { int n; structthreeNumnum; FILE *fptr; if ((fptr = fopen("C:\\program.bin","wb")) == NULL){ printf("Error! opening file"); // Program exits if the file pointer returns NULL. exit(1); } for(n = 1; n < 5; ++n) { num.n1 = n; num.n2 = 5*n; num.n3 = 5*n + 1; fwrite(&num, sizeof(structthreeNum), 1, fptr); } fclose(fptr); return 0; }
char name[5][100]; File Operations – fseek() • Getting data using fseek() • If there are many records inside a file and need to access a record at a specific position, you need to loop through all the records before it to get the record, which will waste memory and operation time. • Syntax of fseek() • fseek(FILE * stream, long int offset, int whence) Different Whence in fseek
Example program: fseek() #include <stdio.h> #include <stdlib.h> structthreeNum { int n1, n2, n3; }; intmain() { int n; structthreeNumnum; FILE *fptr; if ((fptr = fopen("C:\\program.bin","rb")) == NULL){ printf("Error! opening file"); // Program exits if the file pointer returns NULL. exit(1); } // Moves the cursor to the end of the file fseek(fptr, -sizeof(structthreeNum), SEEK_END); for(n = 1; n < 5; ++n) { fread(&num, sizeof(structthreeNum), 1, fptr); printf("n1: %d\tn2: %d\tn3: %d\n", num.n1, num.n2, num.n3); fseek(fptr, -2*sizeof(structthreeNum), SEEK_CUR); } fclose(fptr); return 0; }