Question
Can someone show me whats wrong with this program Im just trying to read from 1 file to another. I can only use systems calls
Can someone show me whats wrong with this program Im just trying to read from 1 file to another. I can only use systems calls for files.
#include < stdlib.h > #include < stdio.h > #include < unistd.h > #include < fcntl.h > #define BUFSIZE 100 #define PERM 0766
int convert_copyfile(const char * name1, const char * name2);
int main() { convert_copyfile("C:\Users\\Desktop\a.txt", "C:\Users\DevinBS\Desktop\Cpfile.txt"); int fd = open("C:\Users\\Desktop\Cpfile.txt"); printf("fd = %d ", fd); return 0; }
int convert_copyfile(const char * name1, const char * name2) { int infile, outfile; size_t nread; char buffer[BUFSIZE]; if ((infile = open(name1, O_RDONLY)))---1) //read file 100 bytes at a time { return ( - 1); } if ((outfile = open(name2, O_WRONLY | O_CREAT | O_TRUNC, PERM))--1) //if file exist write to only, if not creates empty file { close(infile); return (-2); } } while (nread = read(infile, buffer, BUFSIZE) > 0) //read from infile(source file) into buffer then write contents to outfile(target file) { if (write(outfile, buffer, nread) < nread) { close(infile); close(outfile); return ( - 3); } }
close(infile); close(outfile);
if (nread == -1) return ( - 4); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started