#include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <sys/types.h>
ID: 3585703 • Letter: #
Question
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>
//using namespace std;
char script[600];
int main( int argc, char** argv )
{
int j;
char data[600];
//cout << "You have entered " << argc-1 << " arguments:";
for (j = 1; j < argc; ++j)
{
sprintf(script, "%s %s", "cat ", argv[j]);
// content stored as a C string in the buffer pointed by str.
printf(" ***************Start of File******************* ");
system(script); // system call to execute the command
printf(" *******************End Of File******************* ");
return 0;
}
//int open(const char *pathname, int flags);
int fd,fd1,fd2;//declaring all three here
fd = open("path_to_a_file", O_RDWR | O_CREAT, 0755);
if(fd == -1) {
perror("opening file");
}
int close(int fd);
char stringbuffer[1024];
fd1 = open("read1.txt", O_RDONLY);
ssize_t howmany, some_struct;
howmany = read(fd1, stringbuffer, 1024);
if(howmany==-1) {
perror("reading file 1");
exit(1);
}
fd2 = open("read1.txt", O_RDONLY);
howmany = read(fd2, &data, sizeof(some_struct));
if(howmany==-1)
{
perror("reading file 2");
exit(2);
}
ssize_t write(int fd, const void *buf, size_t count);
char mystring[] = "Some text to write.";
char * pointer = (char *) mystring;
fd1 = open("write1", O_CREAT | O_WRONLY, 0777); // Line #49: As you are opening a file which is not there its giving error. SO you need to add a flag O_CREAT also. apart from this once the file will be created it should have some permission which is going to be third parameter. so set that to 0777 or what you feel better as per security concerns.
fd2 = open("write2", O_CREAT | O_WRONLY, 0777); // Line #49: The same issue here as explained above. Set O_CREAT Flag as if file doesn't exists it will add the same and do not forget to set permission. otherwise with default permission you will not be able to open/write the file again.
// write the character array to file 1
howmany = write(fd1, mystring, sizeof(mystring));
if(howmany==-1) {
perror("writing to file 1"); exit(1);
}
// sizeof gives the wrong answer for pointers, use strlen
howmany = write(fd2, pointer, strlen(pointer));
if(howmany==-1)
{
perror("writing to file 2");
exit(2);
}
return 0;
}
Help me fix Line 49 Please
Explanation / Answer
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>
//using namespace std;
char script[600];
int main( int argc, char** argv )
{
int j;
char data[600];
//cout << "You have entered " << argc-1 << " arguments:";
for (j = 1; j < argc; ++j)
{
sprintf(script, "%s %s", "cat ", argv[j]);
// content stored as a C string in the buffer pointed by str.
printf(" ***************Start of File******************* ");
system(script); // system call to execute the command
printf(" *******************End Of File******************* ");
//return 0;
}
//int open(const char *pathname, int flags);
int fd,fd1,fd2;//declaring all three here
fd = open("path_to_a_file", O_RDWR | O_CREAT, 0755);
if(fd == -1) {
perror("opening file");
}
int close(int fd);
char stringbuffer[1024];
fd1 = open("read1.txt", O_RDONLY);
ssize_t howmany, some_struct;
howmany = read(fd1, stringbuffer, 1024);
if(howmany==-1) {
perror("reading file 1");
exit(1);
}
fd2 = open("read1.txt", O_RDONLY);
howmany = read(fd2, &data, sizeof(some_struct));
if(howmany==-1)
{
perror("reading file 2");
exit(2);
}
ssize_t write(int fd, const void *buf, size_t count);
char mystring[] = "Some text to write.";
char * pointer = (char *) mystring;
fd1 = open("write1", O_CREAT | O_WRONLY, 0777); // Line #49: As you are opening a file which is not there its giving error. SO you need to add a flag O_CREAT also. apart from this once the file will be created it should have some permission which is going to be third parameter. so set that to 0777 or what you feel better as per security concerns.
fd2 = open("write2", O_CREAT | O_WRONLY, 0777); // Line #49: The same issue here as explained above. Set O_CREAT Flag as if file doesn't exists it will add the same and do not forget to set permission. otherwise with default permission you will not be able to open/write the file again.
// write the character array to file 1
howmany = write(fd1, mystring, strlen(pointer));
printf("Sizeofmystring: %ld ", sizeof(mystring));
printf("strlen: %ld ", strlen(pointer));
if(howmany==-1) {
perror("writing to file 1"); exit(1);
}
// sizeof gives the wrong answer for pointers, use strlen
howmany = write(fd2, pointer, strlen(pointer));
if(howmany==-1)
{
perror("writing to file 2");
exit(2);
}
return 0;
}
/* Answer :: As of my understanding there is no problem with Line no:49.
*
* Write1 was failed to open, because -> Problem is at line 52 : howmany = write(fd1, mystring, sizeof(mystring));
* This line 52 should be replaced with "howmany = write(fd1, mystring, strlen(pointer));"
* Because, sizeof(mystring) returns size of the string including string terminator character (''). So, while writing into a file
* it tries to write the '' character also. So, while opening that file you may face some problem.
* But, strlen gives the size of the string excluding the strin terminator character (''). So, there is no problem with write2.
*
* Note:
* Still your problem didn't solved, comment on this about your problem more elobaratively. So that, i can try to explain more detail. */
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.