Question

Assignment: Lab Experiment If you open a file for read-write with the append flag, can you still read from anywhere in the fi

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The answer is No.

When we try to open a file for read-write with append flag all the data will be wriiten to the end.

Basically what ever the position of file pointer the data written will only append.

The file opening with append can be done with O_APPEND Flag

Below is the sample code snippet

Here I am taking sample.txt for file operation . Ensure it is present in your current directory .

Content of sample.txt

This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is line 6
This is line 7
This is line 8
This is line 9
This is line 10

Below is the code:

#include<stdio.h>
#include<stdlib.h>
#include <unistd.h>
#include <fcntl.h>

int main() {
int fd = open("sample.txt", O_WRONLY | O_APPEND);

lseek(fd, 0, SEEK_SET);
write(fd, "abc", 3);
close(fd);
  
return 0;

}

File content of sample.txt after program execution:

This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is line 6
This is line 7
This is line 8
This is line 9
This is line 10

Add a comment
Know the answer?
Add Answer to:
Assignment: Lab Experiment If you open a file for read-write with the append flag, can you...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Lab Assignment 4B Modify your program in Lab Assignment 4A to throw an exception if the...

    Lab Assignment 4B Modify your program in Lab Assignment 4A to throw an exception if the file does not exist. An error message should result.   Use the same file name in your program as 4A, however, use the new input file in 4B assignment dropbox. Lab 4A: Write a program that reads a file (provided as attachment to this assignment) and writes the file to a different file with line numbers inserted at the beginning of each line. Test with...

  • How can I read a file and use the content to find the answer?

    How can I read a file and use the content to find the answer? 3. Write a MatLab program that reads in the set of values in the file "Values.txt" which is available from the d2l website. The values are stored as ASCII text separated by carriage returns, so you can open the file and look at the valucs yourself by double clicking on the file name in MatLab's Current Directory window. Your program should next calculate and print out:...

  • How can I read a file and use the content to find the answer?

    How can I read a file and use the content to find the answer? 3. Write a MatLab program that reads in the set of values in the file "Values.txt" which is available from the d2l website. The values are stored as ASCII text separated by carriage returns, so you can open the file and look at the valucs yourself by double clicking on the file name in MatLab's Current Directory window. Your program should next calculate and print out:...

  • How can I read a file and use the content to find the answer?

    How can I read a file and use the content to find the answer? 3. Write a MatLab program that reads in the set of values in the file "Values.txt" which is available from the d2l website. The values are stored as ASCII text separated by carriage returns, so you can open the file and look at the valucs yourself by double clicking on the file name in MatLab's Current Directory window. Your program should next calculate and print out:...

  • How can I read a file and use the content to find the answer?

    How can I read a file and use the content to find the answer? 3. Write a MatLab program that reads in the set of values in the file "Values.txt" which is available from the d2l website. The values are stored as ASCII text separated by carriage returns, so you can open the file and look at the valucs yourself by double clicking on the file name in MatLab's Current Directory window. Your program should next calculate and print out:...

  • Use C++ For this week’s lab you will write a program to read a data file...

    Use C++ For this week’s lab you will write a program to read a data file containing numerical values, one per line. The program should compute average of the numbers and also find the smallest and the largest value in the file. You may assume that the data file will have EXACTLY 100 integer values in it. Process all the values out of the data file. Show the average, smallest, largest, and the name of the data file in the...

  • Part 2: Programming with Exceptions In this part of the lab , you will write a...

    Part 2: Programming with Exceptions In this part of the lab , you will write a program that fetches the information stored at a give URL on the web and saves that data to a file. This will also include networking and file operations and partly an exercise in using exceptions. For doing I/O, Java has a pair of nice abstractions: InputStream and OutputStream. These are abstract classes in the package java.io. An InputStream is a place from which you...

  • Objective: Learn how to -- read string from a file -- write multiple files Problem: Modify...

    Objective: Learn how to -- read string from a file -- write multiple files Problem: Modify the Person class in Lab #4. It has four private data members firstNam (char[20]), day, month, year (all int); and two public member functions: setPerson(char *, int, int, int) and printInfo(). The function setPerson sets the first name and birthday in the order of day, month and year. The function printInfo prints first name and birthday. But it should print the date in the...

  • Using Unix processes Submit a README file that lists the files you have submitted along with...

    Using Unix processes Submit a README file that lists the files you have submitted along with a one sentence explanation. Call it Prj1README. MakeCopy.c : Write a C program that makes a new copy of an existing file using system calls for file manipulation. The names of the two files and copy block sizes are to be specified as command line arguments. Open the source file in read only mode and destination file in read/write mode. ForkCopy.c : Write a...

  • This is for C Programming Write a program to open a jpeg file and read a...

    This is for C Programming Write a program to open a jpeg file and read a byte of data 10 times and display each byte in hexadecimal format.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT