The following problem is related to System Calls in OS. I have given the coding question as well as my own approach to the question. ***Please do not give me any new solution; just tell me what modification my code needs in order to give the expected output. *** Coding Problem: Write a c program that will open a file given from the command line argument and then it will
The following problem is related to System Calls in OS. I have given the coding question as well as my own approach to the question.
***Please do not give me any new solution; just tell me what modification my code needs in order to give the expected output. ***
Coding Problem:
Write a c program that will open a file given from the command line argument and then it will
ask the user to input strings that will be written to that file. It will continue to ask the user to
enter a string as long as the user enters “-1”. If the given file does not exist in the directory, then
your program will automatically create the file.
My Attempt:
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
int n, fd, size;
size = 50;
char buff[size];
char *filename = argv[1];
fd=open(filename, O_WRONLY|O_CREAT, 777);
while (1) {
char text[30];
printf("Enter String:");
fgets(text, sizeof(text), stdin);
if (text == "-1") {
break;
}
else {
write(fd, text, n);
}
return 0;
}
}
Step by step
Solved in 4 steps with 4 images