Input Output
Input Output
Copy file “alice1.txt” in your working directory. Then build and run the
code below:
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 “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.