The document discusses various concepts related to file handling in C programming. It explains that files are used to store data in secondary storage and introduces file streams as an interface between programs and files. It then covers functions for character, string, formatted and binary input/output as well as file positioning. The document also discusses reading and writing files from the command line.
File organization uses storage, organization, and access of data stored in files. There are two main types of file organization: sequential and multitable clustering. Sequential organization stores records in order of a search key, while multitable clustering stores related records from different relations together to minimize disk accesses. Proper file organization is important for database efficiency. Common file functions in C include fopen(), fclose(), fread(), fwrite(), getc(), putc(), getw(), and putw() to open, close, read, write, and access data in text and binary files.
The document discusses files in C programming and file input/output operations. It defines what a file is and explains the need for files when storing and accessing data. There are two main types of files - text files and binary files. The key file I/O functions in C like fopen(), fclose(), fprintf(), fscanf(), fgets(), fputs() and their usage are explained. Both formatted and unformatted I/O functions are covered along with reading and writing characters, integers and strings to files.
1) A file stores related data permanently on secondary storage like hard disks. It supports volatile main memory by storing data when the system shuts down.
2) A file name typically contains a primary name and optional extension separated by a period. File information like name and read/write position is stored in a file information table.
3) Standard input, output, and error streams (stdin, stdout, stderr) allow programs to read from and write to files and devices. Functions like fopen(), fclose(), fread(), fwrite() manage file access.
1. A file represents a sequence of bytes that can store data even if a program terminates. There are two types of files: text files containing plain text and binary files containing data in binary form (0s and 1s).
2. Common file operations include opening, closing, reading, and writing files. Functions like fopen(), fclose(), fgetc(), fputc(), fread(), and fwrite() are used to perform these operations.
3. Files allow permanent storage of data that can be accessed and transferred between computers. Programs demonstrate how to perform tasks like reading a file, copying file contents, finding the largest number in a file, and appending data to an existing file.
fread() and fwrite() are functions used to read and write structured data from files. fread() reads an entire structure block from a file into memory. fwrite() writes an entire structure block from memory to a file. These functions allow efficient reading and writing of complex data types like structures and arrays from binary files.
Contents:-
Introduction
What is a File?
High Level I/O Functions
Defining & Opening a File
Closing a File
The getc and putc Functions
The getw and putw Functions
The fprintf and fscanf Functions
introduction, file, types of files, need for file handling, steps for processing a file, file input/out functions {declaraion of file, opening a file, reading data from a file, writing data in a file, closing the file}, programs #technology #computers
File organization uses storage, organization, and access of data stored in files. There are two main types of file organization: sequential and multitable clustering. Sequential organization stores records in order of a search key, while multitable clustering stores related records from different relations together to minimize disk accesses. Proper file organization is important for database efficiency. Common file functions in C include fopen(), fclose(), fread(), fwrite(), getc(), putc(), getw(), and putw() to open, close, read, write, and access data in text and binary files.
The document discusses files in C programming and file input/output operations. It defines what a file is and explains the need for files when storing and accessing data. There are two main types of files - text files and binary files. The key file I/O functions in C like fopen(), fclose(), fprintf(), fscanf(), fgets(), fputs() and their usage are explained. Both formatted and unformatted I/O functions are covered along with reading and writing characters, integers and strings to files.
1) A file stores related data permanently on secondary storage like hard disks. It supports volatile main memory by storing data when the system shuts down.
2) A file name typically contains a primary name and optional extension separated by a period. File information like name and read/write position is stored in a file information table.
3) Standard input, output, and error streams (stdin, stdout, stderr) allow programs to read from and write to files and devices. Functions like fopen(), fclose(), fread(), fwrite() manage file access.
1. A file represents a sequence of bytes that can store data even if a program terminates. There are two types of files: text files containing plain text and binary files containing data in binary form (0s and 1s).
2. Common file operations include opening, closing, reading, and writing files. Functions like fopen(), fclose(), fgetc(), fputc(), fread(), and fwrite() are used to perform these operations.
3. Files allow permanent storage of data that can be accessed and transferred between computers. Programs demonstrate how to perform tasks like reading a file, copying file contents, finding the largest number in a file, and appending data to an existing file.
fread() and fwrite() are functions used to read and write structured data from files. fread() reads an entire structure block from a file into memory. fwrite() writes an entire structure block from memory to a file. These functions allow efficient reading and writing of complex data types like structures and arrays from binary files.
Contents:-
Introduction
What is a File?
High Level I/O Functions
Defining & Opening a File
Closing a File
The getc and putc Functions
The getw and putw Functions
The fprintf and fscanf Functions
introduction, file, types of files, need for file handling, steps for processing a file, file input/out functions {declaraion of file, opening a file, reading data from a file, writing data in a file, closing the file}, programs #technology #computers
File handling in C allows programs to perform operations on files stored on the local file system such as creation, opening, reading, writing and deletion of files. Common file handling functions include fopen() to open a file, fprintf() and fscanf() to write and read from files, fputc() and fgetc() to write and read single characters, and fclose() to close files. Binary files store data directly from memory to disk and allow for random access of records using functions like fseek(), ftell() and rewind(). Command line arguments can be accessed in the main() function through the argc and argv[] parameters.
The document discusses file management in C. It defines a file as a collection of related data treated as a single unit by computers. C uses the FILE structure to store file attributes. The document outlines opening, reading, writing and closing files in C using functions like fopen(), fclose(), fread(), fwrite(), fseek(), ftell() and handling errors. It also discusses reading/writing characters using getc()/putc() and integers using getw()/putw() as well as formatted input/output with fscanf() and fprintf(). Random access to files using fseek() is also covered.
File handling in C allows programs to permanently store and retrieve large amounts of data from files. Files must be opened before use and closed after to ensure data is properly written. Basic file operations include opening, reading, writing, and closing files. Functions like fopen open a file and return a file pointer. fread and fwrite can read and write arrays of data. An example shows merging two text files containing numbers into a single output file by comparing the numbers and writing the smaller value.
INput output stream in ccP Full Detail.pptxAssadLeo1
This document provides information about file handling functions in C language. It discusses functions for opening, reading, writing, and closing files. Some key functions covered are fopen(), fclose(), fgetc(), fputc(), fprintf(), fscanf(), fgets(), fputs(), fread(), and fwrite(). Examples of using these functions to perform basic file operations like reading/writing characters, strings, and structures are also presented.
The document discusses file management in C programming. It defines a file as a sequence of bytes stored on disk for permanent storage of data. It describes basic file operations like opening, reading, writing and closing files. It explains functions for high level input/output like fopen(), fclose(), getc(), putc(), fprintf(), fscanf(), getw(), putw() and their usage. It also covers error handling functions like feof() and ferror(). The document discusses random access of files using functions like ftell(), fseek() and rewind(). It finally talks about command line arguments and how they are passed to main() in C programs.
This document discusses file handling in C. It covers opening, reading from, writing to, and closing files. The key points are:
- Files allow storing data permanently that can be accessed by programs. Common file operations in C include opening, reading, writing, and closing files.
- To open a file, the fopen function is used, specifying the file name and mode (e.g. read, write, append). This returns a FILE pointer used for subsequent operations.
- Common functions for reading from files include getc, fgetc, fscanf. Common functions for writing include putc, fputc, fprintf.
- It is important to close files after
Unit 5 discusses file handling in C programming. It defines a file as a collection of bytes stored on disk where related data is stored. There are two main types of file accessing: sequential and random. Sequential files are processed line-by-line while random accessing allows accessing any point in the file. Common file handling functions in C include fopen(), fclose(), fread(), fwrite(), fseek(), ftell() among others. Files are needed to permanently store data for programs to access even after terminating. Reading from files uses functions like fscanf() while writing uses fprintf(). The key aspects of files, records and fields are also discussed along with examples of reading and writing to files in C.
The document discusses various C programming concepts like typedef, bitfields, enumeration, file I/O, text files vs binary files. typedef allows defining a new name for an already defined datatype. Bitfields allow packing structure members efficiently using bits. Enumeration defines a set of named integer constants. File I/O functions like fopen, fclose, fread, fwrite are used to read and write data to files. Text files contain human readable characters while binary files store data as bytes.
File handling in C programming uses file streams as the means of communication between programs and data files. The input stream extracts data from files and supplies it to the program, while the output stream stores data from the program into files. To handle file input/output, header file fstream.h is included, which contains ifstream and ofstream classes. Common file operations include opening, reading, writing, and closing files using functions like fopen(), fgetc(), fputs(), fclose(), and checking for end-of-file conditions. Files can be opened in different modes like read, write, append depending on the operation to be performed.
File handling in C allows programs to read from and write to files. A file contains related data treated as a single unit that is stored in secondary storage. C uses the FILE structure to represent an opened file and track attributes. The basic file operations are opening a file with fopen, reading/writing with fread/fwrite, seeking position with fseek, getting position with ftell, and closing with fclose. Files can be opened in different modes like read, write, append to specify how the file will be used.
COURSE TITLE: SOFTWARE DEVELOPMENT VI
COURSE CODE: VIT 351
TOPICS COVERED:
FILES
FILES I/O STREAM
TYPES OF FILES
DRAWBACKS OF TRADITIONAL METHOD OF DATA STORAGE
CONCEPT OF BUFFER
MODES OF FILE OPENING
END OF FILE
PROCESSORS DIRECTIVES
MACROS
TYPES OF MACROS
DIFFERENCE BETWEEN MACROS AND FUNCTIONS
QUIZ SET 5
The document discusses file handling in C programming. It explains that files allow permanent storage of data that can be accessed quickly through library functions. There are two main types of files - sequential and random access. It also describes various functions used to open, read, write, close and manipulate files like fopen(), fread(), fwrite(), fclose() etc. It provides examples of reading from and writing to text and binary files as well as reading and writing structures and integers from files.
Files allow programs to store and retrieve data permanently. There are three standard streams in C - standard input, output, and error. Files are accessed sequentially to process the data in order, or randomly to access records in any order. Functions like fopen(), fclose(), fprintf(), fscanf() allow opening, closing, reading from and writing to files. File streams have buffers associated with them for efficient reading and writing of data to disk in blocks.
This document discusses file handling in C programming. It describes the high level and low level methods of performing file operations in C using functions like fopen(), fclose(), getc(), putc(), fprintf(), fscanf(), getw(), putw(), fseek(), and ftell(). It explains how to open, read, write, close and perform random access on files. Functions like fopen(), fclose() are used to open and close files while getc(), putc(), fprintf(), fscanf() are used to read and write data from files.
This document discusses file handling in C programming. It begins by explaining why files are useful for storing data permanently and how programs can read from and write to files. It then covers the basic file operations in C like opening, reading from, writing to, and closing files. Different file modes for opening files are described. Functions for reading and writing single characters and formatted data to files like fopen, fclose, getc, putc, fscanf, fprintf are explained with examples. The document also discusses lower level functions for reading and writing blocks of data like fread and fwrite along with an example. It concludes with exercises asking the reader to write programs that read and write data to files.
This document discusses data files in C programming. It covers opening and closing data files, creating data files, and processing data files. Some key points:
1) To access a data file in C, it must first be opened using the fopen() function, which returns a FILE pointer. This pointer is then used to read from or write to the file.
2) A data file can be created by writing data from a program to a new file using functions like putc() and fputs().
3) To process a data file, functions like fgetc() and fputs() can be used to read and write data, character by character or as strings. Command line arguments passed to
File handling in C allows storing program data in files that can be accessed later. There are two main file types: text files containing human-readable ASCII characters and binary files containing 0s and 1s in a non-human-readable format. Common file operations in C include creating, opening, writing, reading, moving within and closing files using functions like fopen(), fprintf(), fscanf(), fputc(), fgetc(), fclose(), and fseek(). A file pointer references an opened file and is required for read/write operations.
Unit-VI discusses files in C programming. A file is a collection of related records stored permanently on secondary storage devices like hard disks. There are several file operations in C - opening a file using fopen(), reading the file using fgetc(), and closing it using fclose(). Different text modes like w, r, a, w+, a+ are used for opening files for write, read, append, write and read, append and read operations respectively. Programs are provided to demonstrate opening, writing, reading and closing files in various modes.
File handling in C allows programs to perform operations on files stored on the local file system such as creation, opening, reading, writing and deletion of files. Common file handling functions include fopen() to open a file, fprintf() and fscanf() to write and read from files, fputc() and fgetc() to write and read single characters, and fclose() to close files. Binary files store data directly from memory to disk and allow for random access of records using functions like fseek(), ftell() and rewind(). Command line arguments can be accessed in the main() function through the argc and argv[] parameters.
The document discusses file management in C. It defines a file as a collection of related data treated as a single unit by computers. C uses the FILE structure to store file attributes. The document outlines opening, reading, writing and closing files in C using functions like fopen(), fclose(), fread(), fwrite(), fseek(), ftell() and handling errors. It also discusses reading/writing characters using getc()/putc() and integers using getw()/putw() as well as formatted input/output with fscanf() and fprintf(). Random access to files using fseek() is also covered.
File handling in C allows programs to permanently store and retrieve large amounts of data from files. Files must be opened before use and closed after to ensure data is properly written. Basic file operations include opening, reading, writing, and closing files. Functions like fopen open a file and return a file pointer. fread and fwrite can read and write arrays of data. An example shows merging two text files containing numbers into a single output file by comparing the numbers and writing the smaller value.
INput output stream in ccP Full Detail.pptxAssadLeo1
This document provides information about file handling functions in C language. It discusses functions for opening, reading, writing, and closing files. Some key functions covered are fopen(), fclose(), fgetc(), fputc(), fprintf(), fscanf(), fgets(), fputs(), fread(), and fwrite(). Examples of using these functions to perform basic file operations like reading/writing characters, strings, and structures are also presented.
The document discusses file management in C programming. It defines a file as a sequence of bytes stored on disk for permanent storage of data. It describes basic file operations like opening, reading, writing and closing files. It explains functions for high level input/output like fopen(), fclose(), getc(), putc(), fprintf(), fscanf(), getw(), putw() and their usage. It also covers error handling functions like feof() and ferror(). The document discusses random access of files using functions like ftell(), fseek() and rewind(). It finally talks about command line arguments and how they are passed to main() in C programs.
This document discusses file handling in C. It covers opening, reading from, writing to, and closing files. The key points are:
- Files allow storing data permanently that can be accessed by programs. Common file operations in C include opening, reading, writing, and closing files.
- To open a file, the fopen function is used, specifying the file name and mode (e.g. read, write, append). This returns a FILE pointer used for subsequent operations.
- Common functions for reading from files include getc, fgetc, fscanf. Common functions for writing include putc, fputc, fprintf.
- It is important to close files after
Unit 5 discusses file handling in C programming. It defines a file as a collection of bytes stored on disk where related data is stored. There are two main types of file accessing: sequential and random. Sequential files are processed line-by-line while random accessing allows accessing any point in the file. Common file handling functions in C include fopen(), fclose(), fread(), fwrite(), fseek(), ftell() among others. Files are needed to permanently store data for programs to access even after terminating. Reading from files uses functions like fscanf() while writing uses fprintf(). The key aspects of files, records and fields are also discussed along with examples of reading and writing to files in C.
The document discusses various C programming concepts like typedef, bitfields, enumeration, file I/O, text files vs binary files. typedef allows defining a new name for an already defined datatype. Bitfields allow packing structure members efficiently using bits. Enumeration defines a set of named integer constants. File I/O functions like fopen, fclose, fread, fwrite are used to read and write data to files. Text files contain human readable characters while binary files store data as bytes.
File handling in C programming uses file streams as the means of communication between programs and data files. The input stream extracts data from files and supplies it to the program, while the output stream stores data from the program into files. To handle file input/output, header file fstream.h is included, which contains ifstream and ofstream classes. Common file operations include opening, reading, writing, and closing files using functions like fopen(), fgetc(), fputs(), fclose(), and checking for end-of-file conditions. Files can be opened in different modes like read, write, append depending on the operation to be performed.
File handling in C allows programs to read from and write to files. A file contains related data treated as a single unit that is stored in secondary storage. C uses the FILE structure to represent an opened file and track attributes. The basic file operations are opening a file with fopen, reading/writing with fread/fwrite, seeking position with fseek, getting position with ftell, and closing with fclose. Files can be opened in different modes like read, write, append to specify how the file will be used.
COURSE TITLE: SOFTWARE DEVELOPMENT VI
COURSE CODE: VIT 351
TOPICS COVERED:
FILES
FILES I/O STREAM
TYPES OF FILES
DRAWBACKS OF TRADITIONAL METHOD OF DATA STORAGE
CONCEPT OF BUFFER
MODES OF FILE OPENING
END OF FILE
PROCESSORS DIRECTIVES
MACROS
TYPES OF MACROS
DIFFERENCE BETWEEN MACROS AND FUNCTIONS
QUIZ SET 5
The document discusses file handling in C programming. It explains that files allow permanent storage of data that can be accessed quickly through library functions. There are two main types of files - sequential and random access. It also describes various functions used to open, read, write, close and manipulate files like fopen(), fread(), fwrite(), fclose() etc. It provides examples of reading from and writing to text and binary files as well as reading and writing structures and integers from files.
Files allow programs to store and retrieve data permanently. There are three standard streams in C - standard input, output, and error. Files are accessed sequentially to process the data in order, or randomly to access records in any order. Functions like fopen(), fclose(), fprintf(), fscanf() allow opening, closing, reading from and writing to files. File streams have buffers associated with them for efficient reading and writing of data to disk in blocks.
This document discusses file handling in C programming. It describes the high level and low level methods of performing file operations in C using functions like fopen(), fclose(), getc(), putc(), fprintf(), fscanf(), getw(), putw(), fseek(), and ftell(). It explains how to open, read, write, close and perform random access on files. Functions like fopen(), fclose() are used to open and close files while getc(), putc(), fprintf(), fscanf() are used to read and write data from files.
This document discusses file handling in C programming. It begins by explaining why files are useful for storing data permanently and how programs can read from and write to files. It then covers the basic file operations in C like opening, reading from, writing to, and closing files. Different file modes for opening files are described. Functions for reading and writing single characters and formatted data to files like fopen, fclose, getc, putc, fscanf, fprintf are explained with examples. The document also discusses lower level functions for reading and writing blocks of data like fread and fwrite along with an example. It concludes with exercises asking the reader to write programs that read and write data to files.
This document discusses data files in C programming. It covers opening and closing data files, creating data files, and processing data files. Some key points:
1) To access a data file in C, it must first be opened using the fopen() function, which returns a FILE pointer. This pointer is then used to read from or write to the file.
2) A data file can be created by writing data from a program to a new file using functions like putc() and fputs().
3) To process a data file, functions like fgetc() and fputs() can be used to read and write data, character by character or as strings. Command line arguments passed to
File handling in C allows storing program data in files that can be accessed later. There are two main file types: text files containing human-readable ASCII characters and binary files containing 0s and 1s in a non-human-readable format. Common file operations in C include creating, opening, writing, reading, moving within and closing files using functions like fopen(), fprintf(), fscanf(), fputc(), fgetc(), fclose(), and fseek(). A file pointer references an opened file and is required for read/write operations.
Unit-VI discusses files in C programming. A file is a collection of related records stored permanently on secondary storage devices like hard disks. There are several file operations in C - opening a file using fopen(), reading the file using fgetc(), and closing it using fclose(). Different text modes like w, r, a, w+, a+ are used for opening files for write, read, append, write and read, append and read operations respectively. Programs are provided to demonstrate opening, writing, reading and closing files in various modes.
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPRAHUL
This Dissertation explores the particular circumstances of Mirzapur, a region located in the
core of India. Mirzapur, with its varied terrains and abundant biodiversity, offers an optimal
environment for investigating the changes in vegetation cover dynamics. Our study utilizes
advanced technologies such as GIS (Geographic Information Systems) and Remote sensing to
analyze the transformations that have taken place over the course of a decade.
The complex relationship between human activities and the environment has been the focus
of extensive research and worry. As the global community grapples with swift urbanization,
population expansion, and economic progress, the effects on natural ecosystems are becoming
more evident. A crucial element of this impact is the alteration of vegetation cover, which plays a
significant role in maintaining the ecological equilibrium of our planet.Land serves as the foundation for all human activities and provides the necessary materials for
these activities. As the most crucial natural resource, its utilization by humans results in different
'Land uses,' which are determined by both human activities and the physical characteristics of the
land.
The utilization of land is impacted by human needs and environmental factors. In countries
like India, rapid population growth and the emphasis on extensive resource exploitation can lead
to significant land degradation, adversely affecting the region's land cover.
Therefore, human intervention has significantly influenced land use patterns over many
centuries, evolving its structure over time and space. In the present era, these changes have
accelerated due to factors such as agriculture and urbanization. Information regarding land use and
cover is essential for various planning and management tasks related to the Earth's surface,
providing crucial environmental data for scientific, resource management, policy purposes, and
diverse human activities.
Accurate understanding of land use and cover is imperative for the development planning
of any area. Consequently, a wide range of professionals, including earth system scientists, land
and water managers, and urban planners, are interested in obtaining data on land use and cover
changes, conversion trends, and other related patterns. The spatial dimensions of land use and
cover support policymakers and scientists in making well-informed decisions, as alterations in
these patterns indicate shifts in economic and social conditions. Monitoring such changes with the
help of Advanced technologies like Remote Sensing and Geographic Information Systems is
crucial for coordinated efforts across different administrative levels. Advanced technologies like
Remote Sensing and Geographic Information Systems
9
Changes in vegetation cover refer to variations in the distribution, composition, and overall
structure of plant communities across different temporal and spatial scales. These changes can
occur natural.
How to Make a Field Mandatory in Odoo 17Celine George
In Odoo, making a field required can be done through both Python code and XML views. When you set the required attribute to True in Python code, it makes the field required across all views where it's used. Conversely, when you set the required attribute in XML views, it makes the field required only in the context of that particular view.
This presentation was provided by Rebecca Benner, Ph.D., of the American Society of Anesthesiologists, for the second session of NISO's 2024 Training Series "DEIA in the Scholarly Landscape." Session Two: 'Expanding Pathways to Publishing Careers,' was held June 13, 2024.
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.pptHenry Hollis
The History of NZ 1870-1900.
Making of a Nation.
From the NZ Wars to Liberals,
Richard Seddon, George Grey,
Social Laboratory, New Zealand,
Confiscations, Kotahitanga, Kingitanga, Parliament, Suffrage, Repudiation, Economic Change, Agriculture, Gold Mining, Timber, Flax, Sheep, Dairying,
The chapter Lifelines of National Economy in Class 10 Geography focuses on the various modes of transportation and communication that play a vital role in the economic development of a country. These lifelines are crucial for the movement of goods, services, and people, thereby connecting different regions and promoting economic activities.
2. Files
Syllabus
FILES: Introduction to files, Streams, I/O
using streams – opening a stream, closing
stream; Character input, Character output,
File position indicator, End of file and errors,
Line input and line output, Formatted I/O,
Block input and output, File type, Files and
command line arguments.
3. Files
Files:
A file is an external collection of related data
treated as a unit.
The aim of a file is to keep a record of data.
Since the contents of primary memory lost
when the computer is shut down, we need
files to store data in a more permanent form.
Additionally, the collection of data is often
too large to reside entirely in main memory
at one time.
4. Files
Files are stored in auxiliary or secondary
storage devices.
The two most common forms of secondary
storage are disk (hard disk, CD, and DVD)
and tape.
The stdio.h header file defines the file
structure;
FILE is predefined datatype. When we need
a file in our program, we declare it using the
FILE type.
FILE *sp; //sp is a pointer to type FILE
5. Files
Streams
Stream is a buffer. It will act as interface between
terminals (such as keyboard,monitor etc..)
It receives or send data as sequence of byes
There are two types of streams
1. input stream
2. output stream
Input stream will send the data to the program by taking
from keyboard or file
Output Stream will send the data to the monitor or file
by taking from the program
7. Files
Text files vs Binary files:
In text files, everything is stored in terms of text
i.e. even if we store an integer 54; it will be
stored as a 2- bytes. In a text file certain
character translation may occur.
A binary file Contains data that was written in
the same format used to store internally in main
memory. For example, the integer value 1245
will be stored in 2 bytes depending on the
machine while it will require 4 bytes in a text
file.
8. Files
Text File Binary File
Bits represent character. Bits represent a custom data.
Less prone to get corrupt as
changes reflect as soon as the
file is opened and can easily be
undone.
Can easily get corrupted, even a
single bit change may corrupt
the file.
Can store only plain text in a
file.
Can store different types of data
(image, audio, text) in a single
file.
Mostly .txt , .dat, .c and .rtf are
used as extensions to text files.
Can have any application
defined extension .jpg,.png,.exe.
20. Files
Write a C program to read and write a character
from/into a file
#include<stdio.h>
main(){
FILE *fp;
char ch;
fp = fopen("one.txt", "w");
printf("Enter data...");
ch=getchar();
putc(ch, fp);
fclose(fp);
21. Files
fp = fopen("one.txt", "r");
printf(“nThe data in the file is”);
ch=getc(fp);
printf("%c",ch);
fclose(fp);
}
22. Files
Write a C program to read and write text
from/into a file
#include<stdio.h>
main(){
FILE *fp;
char ch;
fp = fopen("one.txt", "w");
printf("Enter data...");
while( (ch=getchar())!=EOF) {
putc(ch, fp); }
fclose(fp);
23. Files
fp = fopen("one.txt", "r");
printf(“nThe data in the file is”);
while((ch=getc(fp))!=EOF)
printf("%c",ch);
fclose(fp);
}
24. Files
Write a program in C to copy a file in another
name.
#include<stdio.h>
main(){
FILE *fp1,*fp2;
char ch;
fp1 = fopen("one.txt", "w");
printf("Enter data...");
while((ch=getchar())!=EOF) {
fputc(ch, fp1); }
fclose(fp1);
27. Files
Writing File : fputs() function
Syntax:
int fputs(const char *s, FILE *stream)
Reading File : fgets() function
Syntax:
char* fgets(char *s, int n, FILE *stream)
28. Files
//WAP to write and read string into/from file
#include<stdio.h>
void main(){
FILE *fp;
char text[300];
fp=fopen("myfile2.txt","w");
fputs("hello c programming",fp);
fclose(fp);
fp=fopen("myfile2.txt","r");
fgets(text,200,fp);
printf("%s",text);
fclose(fp);
} Output:
hello c programming
29. Files
Formatting Input/Output functions:
fscanf() and fprintf()
If the file contains data in the form of digits,
real numbers, characters and strings, then
character input/output functions are not enough
as the values would be read in the form of
characters.
The syntax for these functions is:
int fscanf(FILE *fp, char *format,. . .);
int fprintf(FILE *fp, char *format,. . .);
30. Files
/* Write a program in C to copy a file in another
name. */
#include<stdio.h>
#include<stdlib.h>
int main() {
FILE *fp1,*fp2;
int a,b;
fp1=fopen("one.txt", "w");
printf("Enter a and b values from key board: n");
scanf("%d%d",&a,&b); /* read from keyboard */
fprintf(fp1, "%d %d",a,b); /* write to file */
fclose(fp1);
33. Files
fseek() function is used to move the file position
to a desired location within the file.
Syntax:
int fseek(FILE *fp, long int numbytes, int
origin);
Here, fp is a file pointer returned by a call to
fopen( ),
numbytes is the number of bytes that pointer
moves from origin, which will become the new
current position.
34. Files
Origin is one of the following macros:
Origin Macro Name value
Beginning of file SEEK_SET 0
Current position SEEK_CUR 1
End of file SEEK_END 2
35. Files
Statement Meaning
fseek(fp,00,0) Go to the beginning.
fseek(fp,00,1) Stay at current position.
fseek(fp,-l,2) Go to the end of the file,
past the last character of the file.
fseek(fp,m,0) move to (m+1)th byte in
the file.
fseek(fp,m,1) Go Forward by m bytes.
fseek(fp,-m,1) Go Backward by m bytes
from the current position.
fseek(fp,-m,2) Go Backward by m
bytes from the end.
36. Files
ftell():
ftell() returns the location of the current position
of the file associated with fp.
If a failure occurs, it returns –1.
long int ftell(FILE *fp);
ftell takes a file pointer as argument and return a
number of type long that corresponds to the
current position.
37. Files
rewind( ):
The rewind( ) function moves the file position
indicator to the start of the specified file.
void rewind(FILE *fp);
It also clears the end-of-file and error flags
associated with file.
This function helps us reading the file more
than once, without having to close and open
the file.
39. Files
printf("nthe cursor position %dn",ftell(fp));
printf("character=%c",fgetc(fp));
rewind(fp);
printf("nthe cursor position %dn",ftell(fp));
printf("character=%c",fgetc(fp));
fclose(fp); } output:
the cursor position 0
character=T
the cursor position 6
character=s
the cursor position 0
character=T
40. Files
Block Input/output functions:
fread( ) and fwrite( ):
To read and write data types that are longer than 1
byte, the C file system provides two functions:
fread( ) and fwrite( ).
These functions allow the reading and writing of
blocks of any type of data.
size_t fread (void *buffer, size_t num_bytes,
size_t count, FILE *fp);
size_t fwrite (const void *buffer, size_t
num_bytes, size_t count, FILE *fp);
41. Files
For fread( ), buffer is a pointer to a region of
memory that will receive the data from the file.
For fwrite( ), buffer is a pointer to the
information that will be written to the file.
The value of count determines how many items
are read or written, with each item being num
bytes in length.
fp is a file pointer to a previously opened stream.
44. Files
Files and command line arguments.
It is a procedure of passing the arguments to the main function
from the command prompt.
/*This program reads a file specified by the user as a command
line argument and display the contents of the file on screen. */
#include <stdio.h>
int main(int argc, char *argv[]) {
FILE *fp; char *filename;
char ch;