problem solving using c full course paart 3
problem solving using c full course paart 3
Every operating system uses a set of rules for naming its files.
The file name may contain two parts, a primary name and an optional
period with extension.
Example: input.txt
program.c
When the file is opened, the stream and the file are associated with
each other.
C includes many standard functions to input data from the keyword and
output data to the monitor.
We have already familiar with two formatting functions scanf and
printf.
These two functions can be used only with the keyboard and
monitor.
The fscanf function would read values from the file "pointed" to
by fptr1 and assign those values to a and b.
K.Vigneswara Reddy, Dept. of I.T
27
Writing to Files: fprintf ()
It can handle a group of mixed data simultaneously.
Its return value is integer. Up on successful writing returns the ASCII value
of character. Otherwise returns EOF.
The getc functions read the next character from the stream,
which can be a user-defined stream or stdin, and converts it in
to an integer.
Syntax:
int getc (FILE *spIn); or int getc(stdin);
int fgetc (FILE *spIn); or int fgetc(stdin);
• #include<stdio.h>
void main()
{
FILE *fp;
Char c;
fp=fopen(“readf.c”,”r”);
while((c=fgetc(fp))!=EOF)
printf(“%c”,c); •While loop would be exit when it reaches to
fclose(fp); the EOF which is special character of
}//main ascii value 26 placed beyond the last
character of the file, when the file is created.
In the text format, data are organized into lines, terminated by newline character.
The text files are in human readable form and they can be created and read using any
text editor.
Text files are read and written using input / output functions that convert characters
to data types: scanf and printf, getchar and putchar, fgets and fputs.
Binary files are read and written using binary streams known as block
input / output functions.
Just like text files, binary files must be closed when they are not needed
anymore using fclose ().
Mode Meaning
This mode opens a binary file in write mode.
wb
Example: fp=fopen (“data.dat”,”wb”);
This mode opens a binary file in read mode.
rb
Example: fp=fopen (“data.dat”,”rb”);
This mode opens a binary file in append mode.
ab
Example: fp=fopen (“data.dat”,”ab”);
This mode opens/creates a binary file in write and read mode.
w+b
Example: fp=fopen (“data.dat”,”w+b”);
This mode opens a pre-existing binary file in read and write mode.
r+b
Example: fp=fopen (“data.dat”,”r+b”);
This mode opens/creates a binary file in append mode.
a+b
Example: fp=fopen (“data.dat”,”a+b”);
K.Vigneswara Reddy, Dept. of I.T
Binary Modes of Opened File
BLOCK I/O FUNCTIONS:
C language uses the block input and output functions to read and write
data of binary files.
As we know that data are stored in memory in the form of 0’s and 1’s.
When we read and write the binary files, the data are transferred
just as they are found in memory and hence there are no format
conversions.
The block read function is file read(fread) and the block write
function is file write (fwrite).
To read the records from a file use fread with four arguments .
fread (&structure variable,sizeof(structure variable),no.of records to be
read at a time,file pointer);
#include<stdio.h>
void main(int argc,char *argv[])
{
char ch;
FILE *fp;
fp=fopen(argv[1],"w");
while((ch=getchar())!=EOF)
fputc(ch,fp);
fclose(fp);
fp=fopen(“xyz.c”,”r”);
while((ch=fgetc(fp))!=EOF)
printf(“%c”,ch);
fclose(fp);
}//main
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <time.h>
int main()
{
char path[100];
struct stat stats;
// File permissions
printf("\nFile access: ");
if (stats.st_mode & R_OK)
printf("read ");
if (stats.st_mode & W_OK)
printf("write ");
if (stats.st_mode & X_OK)
printf("execute");
// File size
K.Vigneswara Reddy, Dept. of I.T
printf("\nFile size: %d", stats.st_size);
// Get file creation time in seconds and
// convert seconds to date and time format
dt = *(gmtime(&stats.st_ctime));