want to learn files,then just use this ppt to learnnalluribalaji157
The document serves as an introduction to file handling in C programming, outlining key operations such as creating, opening, reading, writing, and closing files. It details various library functions for file manipulation, including fopen, fclose, fgetc, fputc, and others, explaining their syntax and usage through examples. Additionally, the document discusses handling binary data and provides information on file pointers and relevant functions such as fseek and ftell.
A file is a collection of related data that a computer treats as a single unit. Files allow data to be stored permanently even when the computer is shut down. C uses the FILE structure to store attributes of a file. Files allow for flexible data storage and retrieval of large data volumes like experimental results. Key file operations in C include opening, reading, writing, and closing files. Functions like fopen(), fread(), fwrite(), fclose() perform these operations.
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.
This document discusses file handling in C. It defines a file, outlines the basic steps to process a file which are to open, read/write, and close it. The basic file operations like fopen, fclose, fread, fwrite are introduced. It also covers file open modes, additional modes like r+, w+, a+, and functions to work with files like fprintf, fscanf, getc, putc. Examples are provided to demonstrate reading, writing, seeking within a file using functions like fread, fwrite, fseek, ftell.
The document provides an overview of file handling in C programming, explaining what a file is, basic operations, and functions like fopen, fclose, fread, fwrite, and fseek. It details how to process files, including creating streams, reading/writing data, and closing files. Additionally, it includes syntax examples and explanations for various file handling functions.
This document discusses various functions used for file handling in C programming. It defines a file and describes basic file operations like opening, closing, reading, and writing to files. It then explains functions like fopen(), fclose(), fprintf(), fscanf(), fgetc(), fputc(), putw(), getw(), fseek(), and ftell() that are used to perform these basic file operations. Examples are provided to demonstrate the usage of each function.
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.
C programming provides functions to manage files, including 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). Different functions exist to read from and write to files, such as fgetc(), fputs(), fprintf(). Files should be closed with fclose() after operations are complete. Errors can be checked using feof() and ferror(). Random access functions like fseek(), ftell(), rewind() allow accessing arbitrary parts of a file.
This document discusses file handling in C. It covers console vs file-based input/output, defining and opening files, reading and writing to files using functions like fopen(), fclose(), getc(), putc(), fscanf(), fprintf(), getw(), putw(). It also discusses errors that can occur during file I/O and how to handle them using functions like feof(), ferror(). Random access to files using fseek() and ftell() is described. The use of command line arguments argc and argv[] is also summarized.
This document discusses file handling in C. It begins by contrasting console I/O and file I/O, noting that file I/O is needed for large data volumes. It then covers defining and opening files, different file modes, input/output operations like getc(), putc(), fscanf(), fprintf(), getw(), putw(), closing files, errors that can occur, and random access to files. It also briefly discusses command line arguments.
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 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.
The document is a lecture by Mr. Salunke on file handling in C programming, covering definitions, types of files, and file functions such as fopen(), fclose(), and others. It discusses file management techniques, including opening/closing files, input/output operations, and command line arguments, along with detailed examples of file operations. The lecture also explains random access files and demonstrates various file handling functions with coding examples.
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 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.
Files are used in programming to store data that persists even after a program terminates. There are two main types of files: text files which contain human-readable text and binary files which contain machine-readable binary data. Common file operations include creating, opening, reading from, writing to, and closing files using functions like fopen(), fread(), fwrite(), and fclose(). Pointers of type FILE are used to represent files and perform input/output operations on them.
This document provides an overview of various file handling functions in C, including FILE structure, fopen(), fclose(), file access modes, fputs(), fgets(), fputc(), fgetc(), fprintf(), fscanf(), fwrite(), fread(), freopen(), fflush(), feof(), fseek(), ftell(), fgetpos(), fsetpos(), rewind(), perror(), and examples of how to use each function. It explains concepts like FILE pointer, size_t and fpos_t data types used for file handling.
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.
File I/O allows programs to read from and write to files. The process involves opening a file using fopen(), reading/writing data using functions like fscanf() and fprintf(), and closing the file using fclose(). Functions like fseek() and ftell() allow random accessing of files by moving the file position indicator. Standard files stdin, stdout, and stderr represent the keyboard, monitor, and error output respectively.
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.
The document outlines the technical course on software development covering file I/O streams in C, explaining concepts such as buffers, file management, and preprocessor directives. It details how to create and manipulate text and binary files, the importance of file handling for data persistence and reusability, and the use of preprocessor commands for code modification before compilation. Additionally, it provides examples of C programs demonstrating file operations and the use of macros and conditional compilation.
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.
The document provides a comprehensive guide to file management in the C programming language, covering essential operations such as creating, opening, reading, writing, and closing files. It introduces various functions for file input/output operations, including fopen(), fclose(), fgetc(), fputc(), fscanf(), fprintf(), fgets(), and fputs(). Additionally, it explains handling command line arguments in C programs, allowing user inputs during program execution.
The document discusses file handling in C programming, detailing the processes of creating, opening, reading, writing, and closing files. It distinguishes between text and binary files, explains various file operations and their associated functions, and outlines file modes and access methods. Additionally, it includes examples and exercises related to file operations and command line arguments.
File handling in C enables programs to create, update, read, and delete files stored on the local file system. The fopen() function opens a file and returns a file pointer, which is then used with functions like fprintf() and fscanf() to write to or read from the file. The fclose() function closes the file and frees resources. Other functions like fseek() and rewind() allow manipulating the file pointer position.
The document is a presentation on file handling in C programming, outlining the concept of files, their types, and the necessary steps to process them. It provides detailed information about file operations such as declaring, opening, reading, writing, and closing files using various C functions. Additionally, it includes example code snippets for practical understanding and references for further reading.
The Influence off Flexible Work Policiessales480687
This topic explores how flexible work policies—such as remote work, flexible hours, and hybrid models—are transforming modern workplaces. It examines the impact on employee productivity, job satisfaction, work-life balance, and organizational performance. The topic also addresses challenges such as communication gaps, maintaining company culture, and ensuring accountability. Additionally, it highlights how flexible work arrangements can attract top talent, promote inclusivity, and adapt businesses to an evolving global workforce. Ultimately, it reflects the shift in how and where work gets done in the 21st century.
More Related Content
Similar to MODULE 8-File and preprocessor.pptx for c program learners easy learning (20)
This document discusses file handling in C. It covers console vs file-based input/output, defining and opening files, reading and writing to files using functions like fopen(), fclose(), getc(), putc(), fscanf(), fprintf(), getw(), putw(). It also discusses errors that can occur during file I/O and how to handle them using functions like feof(), ferror(). Random access to files using fseek() and ftell() is described. The use of command line arguments argc and argv[] is also summarized.
This document discusses file handling in C. It begins by contrasting console I/O and file I/O, noting that file I/O is needed for large data volumes. It then covers defining and opening files, different file modes, input/output operations like getc(), putc(), fscanf(), fprintf(), getw(), putw(), closing files, errors that can occur, and random access to files. It also briefly discusses command line arguments.
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 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.
The document is a lecture by Mr. Salunke on file handling in C programming, covering definitions, types of files, and file functions such as fopen(), fclose(), and others. It discusses file management techniques, including opening/closing files, input/output operations, and command line arguments, along with detailed examples of file operations. The lecture also explains random access files and demonstrates various file handling functions with coding examples.
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 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.
Files are used in programming to store data that persists even after a program terminates. There are two main types of files: text files which contain human-readable text and binary files which contain machine-readable binary data. Common file operations include creating, opening, reading from, writing to, and closing files using functions like fopen(), fread(), fwrite(), and fclose(). Pointers of type FILE are used to represent files and perform input/output operations on them.
This document provides an overview of various file handling functions in C, including FILE structure, fopen(), fclose(), file access modes, fputs(), fgets(), fputc(), fgetc(), fprintf(), fscanf(), fwrite(), fread(), freopen(), fflush(), feof(), fseek(), ftell(), fgetpos(), fsetpos(), rewind(), perror(), and examples of how to use each function. It explains concepts like FILE pointer, size_t and fpos_t data types used for file handling.
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.
File I/O allows programs to read from and write to files. The process involves opening a file using fopen(), reading/writing data using functions like fscanf() and fprintf(), and closing the file using fclose(). Functions like fseek() and ftell() allow random accessing of files by moving the file position indicator. Standard files stdin, stdout, and stderr represent the keyboard, monitor, and error output respectively.
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.
The document outlines the technical course on software development covering file I/O streams in C, explaining concepts such as buffers, file management, and preprocessor directives. It details how to create and manipulate text and binary files, the importance of file handling for data persistence and reusability, and the use of preprocessor commands for code modification before compilation. Additionally, it provides examples of C programs demonstrating file operations and the use of macros and conditional compilation.
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.
The document provides a comprehensive guide to file management in the C programming language, covering essential operations such as creating, opening, reading, writing, and closing files. It introduces various functions for file input/output operations, including fopen(), fclose(), fgetc(), fputc(), fscanf(), fprintf(), fgets(), and fputs(). Additionally, it explains handling command line arguments in C programs, allowing user inputs during program execution.
The document discusses file handling in C programming, detailing the processes of creating, opening, reading, writing, and closing files. It distinguishes between text and binary files, explains various file operations and their associated functions, and outlines file modes and access methods. Additionally, it includes examples and exercises related to file operations and command line arguments.
File handling in C enables programs to create, update, read, and delete files stored on the local file system. The fopen() function opens a file and returns a file pointer, which is then used with functions like fprintf() and fscanf() to write to or read from the file. The fclose() function closes the file and frees resources. Other functions like fseek() and rewind() allow manipulating the file pointer position.
The document is a presentation on file handling in C programming, outlining the concept of files, their types, and the necessary steps to process them. It provides detailed information about file operations such as declaring, opening, reading, writing, and closing files using various C functions. Additionally, it includes example code snippets for practical understanding and references for further reading.
The Influence off Flexible Work Policiessales480687
This topic explores how flexible work policies—such as remote work, flexible hours, and hybrid models—are transforming modern workplaces. It examines the impact on employee productivity, job satisfaction, work-life balance, and organizational performance. The topic also addresses challenges such as communication gaps, maintaining company culture, and ensuring accountability. Additionally, it highlights how flexible work arrangements can attract top talent, promote inclusivity, and adapt businesses to an evolving global workforce. Ultimately, it reflects the shift in how and where work gets done in the 21st century.
NVIDIA Triton Inference Server, a game-changing platform for deploying AI mod...Tamanna36
NVIDIA Triton Inference Server! 🌟
Learn how Triton streamlines AI model deployment with dynamic batching, support for TensorFlow, PyTorch, ONNX, and more, plus GPU-optimized performance. From YOLO11 object detection to NVIDIA Dynamo’s future, it’s your guide to scalable AI inference.
Check out the slides and share your thoughts! 👇
#AI #NVIDIA #TritonInferenceServer #MachineLearning
Data warehouses, lakes, lakehouses, streams, fabrics, hubs, vaults, and meshes. We sometimes choose deliberately, sometimes influenced by trends, yet often get an organic blend. But the choices have orders of magnitude in impact on operations cost and iteration speed. Let's dissect the paradigms and their operational aspects once and for all.
2025原版芝加哥大学毕业证书pdf电子版【q薇1954292140】美国毕业证办理UChicago芝加哥大学毕业证书多少钱?【q薇1954292140】海外各大学Diploma版本,因为疫情学校推迟发放证书、证书原件丢失补办、没有正常毕业未能认证学历面临就业提供解决办法。当遭遇挂科、旷课导致无法修满学分,或者直接被学校退学,最后无法毕业拿不到毕业证。此时的你一定手足无措,因为留学一场,没有获得毕业证以及学历证明肯定是无法给自己和父母一个交代的。
【复刻芝加哥大学成绩单信封,Buy The University of Chicago Transcripts】
购买日韩成绩单、英国大学成绩单、美国大学成绩单、澳洲大学成绩单、加拿大大学成绩单(q微1954292140)新加坡大学成绩单、新西兰大学成绩单、爱尔兰成绩单、西班牙成绩单、德国成绩单。成绩单的意义主要体现在证明学习能力、评估学术背景、展示综合素质、提高录取率,以及是作为留信认证申请材料的一部分。
芝加哥大学成绩单能够体现您的的学习能力,包括芝加哥大学课程成绩、专业能力、研究能力。(q微1954292140)具体来说,成绩报告单通常包含学生的学习技能与习惯、各科成绩以及老师评语等部分,因此,成绩单不仅是学生学术能力的证明,也是评估学生是否适合某个教育项目的重要依据!
我们承诺采用的是学校原版纸张(原版纸质、底色、纹路)我们工厂拥有全套进口原装设备,特殊工艺都是采用不同机器制作,仿真度基本可以达到100%,所有成品以及工艺效果都可提前给客户展示,不满意可以根据客户要求进行调整,直到满意为止!
【主营项目】
一、工作未确定,回国需先给父母、亲戚朋友看下文凭的情况,办理毕业证|办理文凭: 买大学毕业证|买大学文凭【q薇1954292140】芝加哥大学学位证明书如何办理申请?
二、回国进私企、外企、自己做生意的情况,这些单位是不查询毕业证真伪的,而且国内没有渠道去查询国外文凭的真假,也不需要提供真实教育部认证。鉴于此,办理美国成绩单芝加哥大学毕业证【q薇1954292140】国外大学毕业证, 文凭办理, 国外文凭办理, 留信网认证
一比一还原加利福尼亚大学旧金山法学院毕业证/UCLawSF毕业证书2025原版【q薇1954292140】我们专业办理澳洲大学毕业证成绩单,美国大学毕业证成绩单,英国大学毕业证成绩单,加拿大大学毕业证成绩单,新加坡大学毕业证成绩单,新西兰大学毕业证成绩单,韩国大学毕业证成绩单,日本大学毕业证成绩单。
【复刻一套加利福尼亚大学旧金山法学院毕业证成绩单信封等材料最强攻略,Buy University of California College of the Law, San Francisco Transcripts】
购买日韩成绩单、英国大学成绩单、美国大学成绩单、澳洲大学成绩单、加拿大大学成绩单(q微1954292140)新加坡大学成绩单、新西兰大学成绩单、爱尔兰成绩单、西班牙成绩单、德国成绩单。成绩单的意义主要体现在证明学习能力、评估学术背景、展示综合素质、提高录取率,以及是作为留信认证申请材料的一部分。
加利福尼亚大学旧金山法学院成绩单能够体现您的的学习能力,包括加利福尼亚大学旧金山法学院课程成绩、专业能力、研究能力。(q微1954292140)具体来说,成绩报告单通常包含学生的学习技能与习惯、各科成绩以及老师评语等部分,因此,成绩单不仅是学生学术能力的证明,也是评估学生是否适合某个教育项目的重要依据!
我们承诺采用的是学校原版纸张(原版纸质、底色、纹路)我们工厂拥有全套进口原装设备,特殊工艺都是采用不同机器制作,仿真度基本可以达到100%,所有成品以及工艺效果都可提前给客户展示,不满意可以根据客户要求进行调整,直到满意为止!
【主营项目】
一、工作未确定,回国需先给父母、亲戚朋友看下文凭的情况,办理毕业证|办理文凭: 买大学毕业证|买大学文凭【q薇1954292140】加利福尼亚大学旧金山法学院学位证明书如何办理申请?
二、回国进私企、外企、自己做生意的情况,这些单位是不查询毕业证真伪的,而且国内没有渠道去查询国外文凭的真假,也不需要提供真实教育部认证。鉴于此,办理美国成绩单加利福尼亚大学旧金山法学院毕业证【q薇1954292140】国外大学毕业证, 文凭办理, 国外文凭办理, 留信网认证
三.材料咨询办理、认证咨询办理请加学历顾问【微信:1954292140】加利福尼亚大学旧金山法学院毕业证购买指大学文凭购买,毕业证办理和文凭办理。学院文凭定制,学校原版文凭补办,扫描件文凭定做,100%文凭复刻。
Prescriptive Process Monitoring Under Uncertainty and Resource Constraints: A...Mahmoud Shoush
We introduced Black-Box Prescriptive Process Monitoring (BB-PrPM) – a reinforcement learning approach that learns when, whether, and how to intervene in business processes to boost performance under real-world constraints.
This work is presented at the International Conference on Advanced Information Systems Engineering CAiSE Conference #CAiSE2025
Starbucks in the Indian market through its joint venture.sales480687
This topic focuses on the growth and challenges of Starbucks in the Indian market through its joint venture with Tata. It covers localization strategies, menu adaptations, expansion goals, and financial performance. The topic also examines consumer perceptions, market competition, and how Starbucks navigates economic and cultural factors in one of its most promising international markets.
based on assumption that failure of such a weld is by shear on the
effective area whether the shear transfer is parallel to or
perpendicular to the axis of the line of fillet weld. In fact, the
strength is greater for shear transfer perpendicular to the weld axis;
however, for simplicity the situations are treated the same.
2. Agenda
1 . Introduction to Files
2 .Types of files
3 .Using Files in C
4 .Reading data from files
5 .Writing data to files
6 .Detecting the End-of-File
7.Functions for selecting a record
randomly(File positioning)
8 .Error Handling during file operations
9 .Accepting Command line arguments
10.Renaming and Creating temporary file
3. Drawbacks of Traditional System
• Until now we are using Console Oriented I/O functions.
• “Console Application” means an application that has a text-based
interface. (black screen window)
• Most applications require a large amount of data , if this data is
entered through console then it will be quite time consuming task
• Main drawback of using Traditional I/O :- data is temporary (and
will not be available during re-execution )
4. File handling in C :
• New way of dealing with data is file handling.
• Data is stored onto the disk and can be retrieve whenever
require.
• Output of the program may be stored onto the disk
• In C we have many functions that deals with file handling
• Collection of byte may be interpreted as –
– Single character
7. Introduction to File
• A file is a collection of related data that a
computers treats as a single unit.
• Computers store files to secondary storage so
that the contents of files remain intact when a
computer shuts down.
• When a computer reads a file, it copies the file
from the storage device to memory; when it
writes to a file, it transfers data from memory
to the storage device.
• C uses a structure called FILE (defined in
stdio.h) to store the attributes of a file.
8. Types of Files
The types of files used can be broadly classified
into two categories:
1. Text File
contains ASCII codes that performs
read/write operations.
2. Binary File
can contain non-ASCII characters
Image, audio, video, executable, etc.
9. Using in files in C
To use files in C, we must use the following
steps:
1. Declaring a file pointer variable.
2. Open the file
3. Process the file.
4. Close the file.
10. Declaring a file pointer variable
• To access a particular file specify name of
the file that has to be used.
• This can be accomplished by using file
pointer variable.
• The syntax for declaring the file pointer is:
FILE
*file_pointer_name;
Ex: FILE *fp;
11. Opening a File
• We can use the fopen( ) function to create a new
file or to open an existing file.
• Following is the syntax of this function call:
• Using above prototype file whose pathname is
pointed to by filename is opened in mode
specified using the mode.
FILE *fopen( const char * filename, const char * mode );
13. Code for opening a file
An error will be generated if you try to open a file that does not
exist.
FILE *fp;
fp=fopen(“student.txt”,”r”);
if(fp==NULL)
{
printf(“n the file could not be opened”);
exit(1);
}
14. Closing a File
• File must be closed as soon as all operations on
it completed
• Ensures
– All outstanding information associated with
file flushed out from buffers
– All links to file broken
– Accidental misuse of file prevented
16. Reading data from files
• C provides the following set of functions to
read data from a file:
fscanf( )
fgets( )
fgetc( )
fread( )
17. • fscanf( )
The fscanf() function is to read formatted data
from the stream.
• fscanf() is similar to the scanf() funtion except
that the first argument of fscanf() specifies a
stream from which to read,whereas scanf()
can only read from standard input
Syntax:
int fscanf( FILE *stream ,const char *format,…);
18. • fgets( )
The fgets() function is stands for file get string.this
function is used to get a stream.
Syntax:
• fgetc( )
The fgetc() function returns the next character
from stream and EOF if the end of file reached
or if there is an error
Syntax:
char *fgets( char *str ,int size,FILE *stream);
int fgetc(FILE *stream);
-
19. • fread( )
fread( ) function is used to read data from a file.
Syntax:
Remarks:
fread( ) reads a specified number of equal-sized data items
from an input stream into a block.
str = Points to a block into which data is read
size = Length of each item read, in bytes
num = Number of items read
stream = file pointer
int fread(void *str, size_t size, size_t num, FILE *stream);
21. Writing data to files
• C provides the following set of functions to
writing data to a file:
fprintf()
fputc()
fputs()
fwrite()
22. • fprintf( )
The fprintf( ) function is to write formatted output
to the stream.
Syntax:
int fprintf( FILE *stream ,const char *format,…);
23. • fputs( )
The fputs( ) function is used to write a line to a file
Syntax:
• fputc( )
The fputc() function is used to write a character to
the stream.
Syntax:
int fputs( const char *str ,FILE *stream);
int fputc(int c, FILE *stream )
24. • fwrite()
fwrite() function is used to write data to a file.
Syntax:
Remarks:
fwrite() appends a specified number of equal-sized data
items to an output file.
str = Pointer to any object; the data written begins
at ptr
size = Length of each item of data
count =Number of data items to be appended
stream = file pointer
int fwrite(const void *str, size_t size, size_t count, FILE *stream);
25. • Simple program for reading/writing data from/to files:
#include <stdio.h>
int main ( )
{
FILE *outfile, *infile ;
int b = 5, f ;
float a = 13.72, c = 6.68, e, g ;
outfile = fopen ("testdata", "w") ;
fprintf (outfile, “ %f %d %f ", a, b, c) ;
fclose (outfile) ;
infile = fopen ("testdata", "r") ;
fscanf (infile,"%f %d %f", &e, &f, &g) ;
printf (“ %f %d %f n ", a, b, c) ;
printf (“ %f %d %f n ", e, f, g) ;
}
13.720000,5,6.680000
13.720000,5,6.680000
26. Detecting End-of-File
There are a number of ways to test for the end-of-file
condition. Another way is to use the value returned by
the fscanf () function.The prototype of feof() can be
given as :
int feof(FILE *fp);
Ex: FILE *fptr1;
int istatus ;
istatus = fscanf (fptr1, "%d", &var) ;
if ( istatus == feof(fptr1) )
{
printf ("End-of-file encountered.n”) ;
}
27. Error handling during file operation
• Typical errors that occur
– trying to read beyond end-of-file
– trying to use a file that has not been opened
– perform operation on file not permitted by ‘fopen’ mode
– open file with invalid filename
– write to write-protected file
28. • ferror()
The library function is used to check for error in the
stream.
• clearerr()
The function is used to clear EOF and error
indicator for the stream.
• perror()
It stands for print error.
Syntax: int ferror(FILE *stream)
Syntax: void clearerr(FILE *stream)
Syntax: void perror(const char *s)
29. • The following example shows the usage of perror( ) function.
#include <stdio.h>
#include <errno.h>
#include <string.h>
extern int errno ;
int main () {
FILE * pf;
int errnum;
pf = fopen ("unexist.txt", "rb");
if (pf == NULL) {
errnum = errno;
fprintf(stderr, "Value of errno: %dn", errno);
perror("Error printed by perror");
fprintf(stderr, "Error opening file: %sn", strerror( errnum ));
}
else {
fclose (pf) ;
}}
Output :
Error reading
from file
"file.txt"
30. Functions for selecting record
randomly
• Functions that are used to randomly access a
record stored in a binary file. This functions
include:
• fseek( )
• ftell( )
• rewind( )
• fgetpos( )
• fsetpos( )
31. • fseek()
The function fseek() reposition a binary stream.
• fseek() seeks the file position for stream; a subsequent read
or write will access data beginning at the new position.
• For a binary file, the position is set to offset characters from
origin, which may be
SEEK_SET (beginning),
SEEK_CUR (current position) or
SEEK_END (end of file).
For a text stream, offset must be zero, or a value returned by
ftell (in which case origin must be set to SEEK_SET).
• SEEK_SET,SEEK_CUR and SEEK_END are defined
constants with value 0,1 and respectively.
Syntax : int fseek(FILE *stream, long offset, int origin)
32. The following example shows the usage of
fseek() function.
#include <stdio.h>
int main ()
{
FILE *fp;
fp = fopen("file.txt","w+");
fputs("D is not programming language", fp);
fseek( fp, 7, SEEK_SET );
fputs(" C Programming Language", fp);
fclose(fp);
return(0);
}
Output : C programming
language
33. • ftell( )
ftell( ) is used to find the current position of the file from
the beginning of the file.
• If successful, ftell() function returns the current
file position(in bytes) for stream.
• In case of error,ftell() returns -1.
• ftell() is useful when we have to deal with text
files for which position of the data cannot be
calculated.
Syntax: long int ftell(FILE *stream)
34. #include <stdio.h>
int main ()
{
FILE *fp;
int len;
fp = fopen("file.txt", "r");
if( fp == NULL )
{
perror ("Error opening file");
return(-1);
}
fseek(fp, 0, SEEK_END);
len = ftell(fp);
fclose(fp);
printf("Total size of file.txt = %d bytesn", len);
return(0);
}
Let us assume we have a text file file.txt, which has the following
content −
This is tutorialspoint.com
Output : Total size of file.txt =
27 bytes
35. • rewind()
rewind() is used to reposition the file pointer to
the
beginning of the file.
• fgetpos()
fgetpos() records the file position in stream in
*pos, for subsequent use. The type fpos_t is
suitable for recording such values.
Syntax: void rewind(FILE *stream)
rewind is equivalent to fseek(fp, OL, SEEK_SET);
Syntax: int fgetpos(FILE *stream, fpos_t*pos)
36. • fsetpos()
fsetpos() positions stream at the position recorded by
fgetpos in *pos.
• remove()
The remove() function,as the name suggest is
used to erase a file.
Syntax : int fsetpos(FILE *stream,const fpos_t *pos)
Syntax: int remove (const char *filename);
37. Command Line arguments
• Command line arguments are given after name of
program in command line operating system such as
DOS or LINUX and are passed into the program
from the operating system.
main ( int argc, char *argv[])
• argc – gives a count of number of arguments
(including program name)
• char *argv[] defines an array of pointers to character
(or array of strings)
• argv[0] – program name
• argv[1] to argv[argc -1] give the other arguments as
strings
38. Renaming a file
• rename( )
The function rename( ) as the name suggests is
used to rename a file.
If the oldname specifies pathname of a file that is
not directory,the new name shall also not point
to the pathname of a directory.
Syntax: int rename(const char *old name,const
char * new name)
39. Example:
#include <stdio.h>
int main ()
{
int ret;
char oldname[] = "file.txt";
char newname[] = "newfile.txt";
ret = rename(oldname, newname);
if(ret == 0)
{
printf("File renamed successfully");
}
else
{
printf("Error: unable to rename the file");
}
return(0);
}
Output: File renamed
successfully
40. Creating a Temporary file
• tmpfile()
The tmpfile() function is used to create a temporary
file.
A file created with tmpfile() will be automatically
deleted when all references to the file are closed.
On success,tmpfile( ) will return a pointer to the
stream of the file name that is created.
Syntax: FILE*tmpfile(void);
41. The following example shows the usage of tmpfile()
function.
#include <stdio.h>
int main ()
{
FILE *fp;
fp = tmpfile();
printf("Temporary file createdn");
/* you can use tmp file here */
fclose(fp);
return(0);
}
Output: Temporary file
created
43. Introduction
• The C Preprocessor is not a part of the compiler,
but is a separate step in the compilation process.
44. Three kinds of directives
• File inclusion
– #include
• Macros
– #define
• Conditional compilation
– #if, #ifdef, #ifndef, #elif, #else, #endif
Always starts with a line with “#”
Can appear anywhere in a program
46. File Inclusion
• Allow a program or a module’s implementation to use certain
header file.
• An interface or a header file contains declarations for a
module
• Name of the header file should end in .h
• User-define header files “ ... ”
#include “sample.h”
• System header files: < ... >
#include <stdio.h>
47. Ex: /* program to call the function defined in “sample.c” file */
#include<stdio.h>
#include<conio.h>
#include “sample.c”
void main ( )
{
clrscr ( );
display ( );
}
Output: welcome to c
• /* file sample.c*/
int display( )
{
printf(“welcome to c”);
return 0;
}
48. Macro Substitution
Provide parameterized text substitution
The syntax of #define directive is as follows
Example : #define MAXLINE 120
#define lower(c) ((c)-`A’+‘a’)
Macro replacement
char buf[MAXLINE+1];
becomes
char buf[120+1];
c = lower(buf[i]);
becomes
c = ((buf[i])-`A’+‘a’);
#define identifier <substitute text>
49. Macros: Use “(“ and “)”
Always parenthesize macro parameters in definition
#define plusone(x) x+1
i = 3*plusone(2);
becomes
i = 3*2+1
#define plusone(x) ((x)+1)
i = 3*plusone(2);
becomes
i = 3*((2)+1)
50. Conditional Compilation
One source for many platforms or many cases
Need to have special source for specific situations
Conditional compilation
#ifdef <identifier>
#ifndef <identifier>
#if <expr>
#elif <expr>
#else
#endif
Removing macro definitions
#undef <identifier>
51. • The syntax of the #ifdef directive is:
#ifdef <identifier>
{
statements1;
statements2;
}
#else
{
statements3;
statements4;
}
#endif
52. • Example: /* program to use conditional compilation statement to
check identifier is defined or not */
#include<stdio.h>
#include<conio.h>
#define LINE 1
void main ( )
{
clrscr ( );
#ifdef LINE
printf(“ This is line number one”);
#else
printf(“ This is line number two”);
#endif
getch( );
}
Output: This
is line
number one
53. The #ifndef directive
The syntax of the #ifndef directive is given below:
•
#ifndef <identifier>
{
statements1;
statements2;
}
#else
{
statements3;
statements4;
}
#endif
54. Ex: /* program to use conditional compilation directive
#ifndef */
•
#include<stdio.h>
#include<conio.h>
#define LINE
void main ( )
{
clrscr ( );
#ifndef LINE
printf(“ Macro is not defined”);
#else
printf(“Macro is defined”);
#endif
getch( );
}
Output: Macro is not
defined
55. Use of #if, #elif, #else and #endif
The preprocessor directives #if, #elif, #else and #endif allows to
conditionally compile a block of code based on predefined symbols.
Example: Program to illustrate this concept.
#include<stdio.h>
#define MAX 100
void main()
{
#if(MAX)
printf("MAX is defined");
#else
printf ("MAX is not defined");
#endif
}
Output:
MAX is defined