0% found this document useful (0 votes)
41 views9 pages

Input Output

The document discusses various methods of reading from and writing to files in C/C++ using file descriptors and system calls like open(), read(), write(), and lseek(). Some key points covered include: - How to open a file for reading/writing and check for errors - Reading/writing a set number of bytes to/from a file - Copying the entire contents of one file to another using read() and write() in a loop - Using lseek() to change the file offset/pointer when reading to start at a specified point in the file - An inefficient method of skipping bytes without lseek() by reading sequentially

Uploaded by

Irtiza Hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views9 pages

Input Output

The document discusses various methods of reading from and writing to files in C/C++ using file descriptors and system calls like open(), read(), write(), and lseek(). Some key points covered include: - How to open a file for reading/writing and check for errors - Reading/writing a set number of bytes to/from a file - Copying the entire contents of one file to another using read() and write() in a loop - Using lseek() to change the file offset/pointer when reading to start at a specified point in the file - An inefficient method of skipping bytes without lseek() by reading sequentially

Uploaded by

Irtiza Hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

File I/O

Q1. Open a file

Copy file “alice1.txt” in your working directory. Then build and run the
code below:

Here variable is a file descriptor, which is (usually) small nonnegative


integer. All system calls for performing I/O refer to open files using a file
descriptor. Here command allows the file to be read or write using
or commands. The flags argument specifies the access
mode for the file. argument allows the file to be read and write.

If the open() command fails then it will give -1.So we can create checks:
Q2. Read contents of a file
function puts the read bytes into the array called buffer. This
array has size of 10 bytes. function gives the number of bytes read.
If for some reason function could not read then it will give -1.

Q3.Write on a file.
Create an empty file called “hello1.txt” in your working directory. Then
build and run the following code:

Now open the file “hello1.txt” and you will see it will have text “Hello!How
are you?”
Q4: Copy contents of one file to another file.

Create an empty file called “alice2.txt” in your working directory. Then


Build and run the following code:

Here when we ‘OR’ three bit mask constants and


when we use open() command to open file “alice2.txt”.“alice2.txt”
file is both read and write due to bit mask constant. If we use
write() command more than one time then “alice2.txt” will be appended due
to bit mask constant .If “alice2.txt” file already has some
content then it will be deleted when open() function on it is called due to
bit mask constant. After running this code, when you open
“alice2.txt” file you will have some content of “alice1.txt” file in “alice2.txt”
file.

Q5. Copy all the contents of one file to another file.

Create an empty file “alice2.txt” in your working directory if you have not
already done so. Then build and run the following code:
If we use command a second time then it will read next
number of bytes. If we use a command a second
time then it will write next number of bytes. So using while loop
we use commands multiple times such that the whole
contents of the file “alice1.txt” are copied to “alice2.txt”. While loop will run
until function gives -1.

Q6 Use command to change file offset.


For each open file, the kernel records a file offset, sometimes also called the
read-write offset or pointer. This is the location in the file at which the
next or will commence.

Q7 Read “alice1.txt” file 500 bytes after start of file


Q8 Without using command read 120 bytes after start of file.

This surely is an inefficient method.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy