-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Shibam Saha
committed
Nov 23, 2022
1 parent
eba6971
commit 302b43b
Showing
30 changed files
with
1,056 additions
and
327 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,45 @@ | ||
// Q. Write a program using open() system call to read data from (max 50 char) | ||
// an existing file “Text1.txt” and write them into non-existing file “Text2.txt” | ||
// an existing file �Text1.txt� and write them into non-existing file �Text2.txt� | ||
|
||
#include <stdio.h> | ||
#include <unistd.h> | ||
#include <sys/types.h> | ||
#include <sys/stat.h> | ||
#include <fcntl.h> | ||
|
||
int main (){ | ||
|
||
int main() | ||
{ | ||
|
||
int len, fd, fd1; | ||
char ch[50]; | ||
|
||
// Opens test.txt in read mode and | ||
// the file descriptor is saved in integer fd | ||
fd = open("Text1.txt", O_RDONLY); | ||
fd = open("Text1.txt", O_RDONLY); | ||
|
||
// The value of the file descriptor is printed. | ||
printf("The file descriptor of the file is: %d\n", fd); | ||
printf("The file descriptor of the file is: %d\n", fd); | ||
|
||
// Read maximum 50 characters from the file pointed by file descriptor fd | ||
// and save them in buffer (ch) | ||
len = read(fd, ch, 50); | ||
|
||
// New file is created in write only mode and | ||
// the file descriptor is saved in integer fd1 | ||
// Pipe symbol (|) is used to separate O_WRONLY and O_CREAT | ||
fd1 = open("Text2.txt", O_WRONLY | O_CREAT, 0642); | ||
|
||
// Write in the Text2.txt file from the buffer | ||
write(fd1, ch, len); | ||
write(fd1, ch, len); | ||
printf("\nTotal bytes written: %d.\n", len); | ||
} | ||
|
||
/* | ||
Output: | ||
s4shibam@SHIBAM:~/OS$ gcc 4_Open_Read_And_Write_In_New_File.c | ||
s4shibam@SHIBAM:~/OS$ ./a.out | ||
The file descriptor of the file is: 3 | ||
Total bytes written: 23. | ||
s4shibam@SHIBAM:~/OS$ | ||
s4shibam@SHIBAM:~/OS$ | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,43 @@ | ||
// Q. Write a program using open() system call to read data from (max 50 char) | ||
// the standard input device and append them into existing file “Text3.txt” | ||
|
||
// the standard input device and append them into existing file �Text3.txt� | ||
|
||
#include <stdio.h> | ||
#include <unistd.h> | ||
#include <sys/types.h> | ||
#include <sys/stat.h> | ||
#include <fcntl.h> | ||
|
||
int main (){ | ||
|
||
int main() | ||
{ | ||
|
||
int len, fd, fd1; | ||
char ch[50]; | ||
char append[50]; | ||
|
||
// Read maximum 50 bytes from standard input device (0 --> stdin) | ||
// & store in buffer (ch) | ||
len = read(0, append, 50); | ||
printf("Total bytes read: %d.\n", len); | ||
|
||
printf("Total bytes read: %d.\n", len); | ||
|
||
// Existing file is opened in append mode and | ||
// the file descriptor is saved in integer fd | ||
// Pipe symbol (|) is used to separate O_WRONLY and O_APPEND | ||
fd = open("Text3.txt", O_WRONLY | O_APPEND); | ||
|
||
// Append in the Text3.txt file from the buffer | ||
write(fd, append, len); | ||
write(fd, append, len); | ||
printf("\nTotal bytes appended: %d.\n", len); | ||
} | ||
|
||
/* | ||
Output: | ||
s4shibam@SHIBAM:~/OS$ gcc 5_Open_Read_And_Append_In_Existing_File.c | ||
s4shibam@SHIBAM:~/OS$ gcc 5_Open_Read_And_Append_In_Existing_File.c | ||
s4shibam@SHIBAM:~/OS$ ./a.out | ||
This is the appended line! | ||
Total bytes read: 27. | ||
Total bytes appended: 27. | ||
s4shibam@SHIBAM:~/OS$ | ||
s4shibam@SHIBAM:~/OS$ | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.