Cannot copy an entire file to another one using write system call - c

I have to copy the content of file1 into a buffer (of size 23 bytes), then, I have to copy data from the buffer to file2.
I have trouble to make sure that file1 is entirely copied into the buffer. When the buffer is copied to file2, file2 contents only part of the content of file1, and the output says that only 4 bytes of data have been copied into file2.
I tried to figure out what I did wrong, but I have no luck so far. Your help will be very appreciated.
I am using Oracle VM VirtualBox, where I have Ubuntu installed.
I am also using make (MakeFile) to update all files at once on the command prompt.
My code is below in C/POSIX.
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#define My_Full_Name "AAA!"
int PrintSentence()
{
size_t buffersize = (size_t) (4 * 5.75); //or (4 bytes * 5.75) = 23 bytes
char buffer[buffersize];
char source_file[200];
char destination_file[200];
ssize_t bytes_read;
int fdSource, fdDestination;
mode_t mode = S_IRUSR | S_IWUSR;
printf("Welcome to File Copy by %s\n", My_Full_Name);
printf("Enter the name of the source file: ");
scanf("%s", source_file);
printf("Enter the name of the destination file: ");
scanf("%s", destination_file);
fdSource = open(source_file, O_RDONLY);
if (fdSource < 0)
{
perror("Open failed!!");
return 1;
}
else
{
bytes_read = read(fdSource, buffer, sizeof(buffer));
fdDestination = open(destination_file, O_CREAT | O_WRONLY | mode);
if (fdDestination < 0)
{
perror("Oups!! cannot create file again!!");
return 1;
}
else
{
write(fdDestination, buffer, sizeof(buffer));
printf("current content of buffer: %s\n", buffer); //just to check
printf("current value of buffer size = %zd \n", buffersize); //just to check
printf("File copy was successful, with %d byte copied\n", fdDestination); //the output says only 4 bytes are copied
}
}
return;
}

Here:
printf("File copy was successful, with %d byte copied\n", fdDestination );
fdDestination is a file descriptor, it's not the number of bytes written. 0, 1 and 2 are your three standard streams, 3 will be your input file which is opened first, so 4 will be your output file, that's why it's always outputting 4.
You want to save the return value from write(), and use the value of that instead (after checking that return value for errors, of course, which you should be doing for read() as well).
EDIT: Slightly modifying your code:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#define My_Full_Name "AAA!"
int main(void) {
size_t buffersize = (size_t) (4 * 5.75);
char buffer[buffersize];
char source_file[200];
char destination_file[200];
ssize_t bytes_read, bytes_written;
int fdSource, fdDestination;
mode_t mode = S_IRUSR | S_IWUSR;
printf("Welcome to File Copy by %s\n", My_Full_Name);
printf("Enter the name of the source file: ");
scanf("%s", source_file);
printf("Enter the name of the destination file: ");
scanf("%s", destination_file);
fdSource = open(source_file, O_RDONLY);
if (fdSource < 0) {
perror("Open failed!!");
return 1;
} else {
bytes_read = read(fdSource, buffer, sizeof(buffer));
fdDestination = open(destination_file, O_CREAT | O_WRONLY | mode);
if (fdDestination < 0) {
perror("Oups!! cannot create file again!!");
return 1;
} else {
bytes_written = write(fdDestination, buffer, sizeof(buffer));
printf("current content of buffer: %s\n", buffer);
printf("current value of buffer size = %zd \n", buffersize);
printf("File copy was successful, with %d byte copied\n",
bytes_written);
}
}
return 0;
}
gives me this:
paul#local:~/src/c/fpc$ cat infile
12345678901234567890123
paul#local:~/src/c/fpc$ ./fpc
Welcome to File Copy by AAA!
Enter the name of the source file: infile
Enter the name of the destination file: outfile
current content of buffer: 12345678901234567890123
current value of buffer size = 23
File copy was successful, with 23 byte copied
paul#local:~/src/c/fpc$ cat outfile; echo ""
12345678901234567890123
paul#local:~/src/c/fpc$

How can you expect that a file will be entirely written into the buffer when your buffer is but 23 bytes long? With call to read you are reading only 23 bytes and leave the rest of the contents of the file1 untouched. Or, do is it your expected behaviour that your program should copy only 23 bytes of its contents to the target file?

Related

I made program that copies data from one file and pastes to another using (read,write) but i think its taking too long

i need to copy 1gb file to another and i am using this code while using different buffers (1byte, 512byte and 1024byte) while using 512byte buffer it took me about 22seconds but when i use 1byte buffer copying doesnt end even after 44minutes. Is that time expected or mby something is wrong with my code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <corecrt_io.h>
int main(int argc, char* argv[])
{
char sourceName[20], destName[20], bufferStr[20];
int f1, f2, fRead;
int bufferSize = 0;
char* buffer;
/*printf("unesite buffer size(u bytima): ");
scanf("%d", &bufferSize);*/
//bufferSize = argv[3];
bufferSize = atoi(argv[3]);
buffer = (char*)calloc(bufferSize, sizeof(char));
/*printf("unesite source name: ");
scanf("%s", sourceName);*/
strcpy(sourceName, argv[1]);
f1 = open(sourceName, O_RDONLY);
if (f1 == -1)
printf("something's wrong with oppening source file!\n");
else
printf("file opened!\n");
/*printf("unesite destination name: ");
scanf("%s", destName);*/
strcpy(destName, argv[2]);
f2 = open(destName, O_CREAT | O_WRONLY | O_TRUNC | O_APPEND);
if (f2 == -1)
printf("something's wrong with oppening destination file!\n");
else
printf("file2 opened!");
fRead = read(f1, buffer, bufferSize);
while (fRead != 0)
{
write(f2, buffer, bufferSize);
fRead = read(f1, buffer, bufferSize);
}
return 0;
}
Yes, this is expected, because system calls are expensive operations, so the time is roughly proportional to the number of times you call read() and write(). If it takes 22 seconds to copy with 512-byte buffers, you should expect it to take about 22 * 512 seconds with 1-byte buffers. That's 187 minutes, or over 3 hours.
This is why stdio implements buffered output by default.

Unwanted characters when copying file using scatter/gather I/O (readv/writev)

I'm trying to build a program to copy existing content from an existing file to the new file using readv() and writev().
Here is my code:
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/uio.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char *argv[])
{
int fs, fd;
ssize_t bytes_read, bytes_written;
char buf[3][50];
int iovcnt;
struct iovec iov[3];
int i;
fs = open(argv[1], O_RDONLY);
if (fs == -1) {
perror("open");
return -1;
}
fd = open(argv[2], O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
if (fd == -1) {
perror("open");
return 1;
}
for(i = 0; i < 3; i++) {
iov[i].iov_base = buf[i];
iov[i].iov_len = sizeof(buf[i]);
}
iovcnt = sizeof(iov) / sizeof(struct iovec);
if ((bytes_read=readv(fs, iov, iovcnt)) != -1)
if ((bytes_written=writev(fd, iov, iovcnt)) == -1)
perror("error writev");
printf("read: %ld bytes, write: %ld bytes\n", bytes_read, bytes_written);
if (close (fs)) {
perror("close fs");
return 1;
}
if (close (fd)) {
perror("close fd");
return 1;
}
return 0;
}
Problem: Let's say I ran the program with argv[1] corresponding to the file called file1.txt and copied it to argv[2], let's say it's called as hello.txt.
This is the content of file1.txt:
Ini adalah line pertamaS
Ini adalah line kedua
Ini adalah line ketiga
When I ran the program, the new created file specified in argv[2] were filled by unwanted characters such as \00.
Output after running the program:
Ini adalah line pertamaS
Ini adalah line kedua
Ini adalah line ketiga
\00\00\FF\B5\F0\00\00\00\00\00\C2\00\00\00\00\00\00\00W\D4\CF\FF\00\00V\D4\CF\FF\00\00\8D\C4|\8C\F8U\00\00\C8o\A6U\E5\00\00#\C4|\8C\F8U\00\00\00\00\00\00\00\00\00\00 \C1|\8C\F8U\00\00`\D5\CF\FF
I suspect the main cause of the problem is unfitted size of buf array. I've already look up internet for the solutions and there are nothing to be found. Can anyone give me some enlightment to fix this problem? I tried to make the buf or iov_len to be variable-length but I couldn't find the right way to do it. Thanks everyone!
readv() works with byte counts driven by each .iov_len and no special treatment for any content (like a line-feed). The readv() in the original posting is passed an array of (3) struct iovec, each with .iov_len set to 50. After a successful readv(), the content of the local buf[3][50] would be:
buf[0] : first 50 bytes from the input file
buf[1] : next 20 bytes from the input file, then 30 bytes of uninitialized/leftover stack data
buf[2] : another 50 bytes of uninitialized/leftover stack data
The writev() reuses the same struct iovec array with all (3) .iov_len unchanged from 50, and writes 150 bytes as expected. The content of the output file has the first 70 bytes copied from the input file and 80 bytes of leftover stack data. If the local buf was cleared before calling readv(), the output file would contain trailing NULLs.

unbuffered I/O: read int from file and write it on standard output

This program is meant to take as parameter a file, then read a string from standard input and write its length into the file, then read the content of the file (which is supposed to contain the lengths of the strings from the standard input) and write it in standard output:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define MAX_BUFF 4096
int main(int argc, char **argv)
{
if (argc != 2)
{
puts("you must specify a file!");
return -1;
}
int nRead;
char buffer[MAX_BUFF], tmp;
int fd;
puts("write \"end\" to stop:");
fd = open(argv[1], O_RDWR | O_CREAT | O_APPEND, S_IRWXU);
while ((nRead = read(STDIN_FILENO, buffer, MAX_BUFF)) > 0 && strncmp(buffer,"end", nRead-1) != 0 )
{
if ( write(fd, &nRead, 1) < 0 )
{
perror("write error.");
return -1;
}
}
puts("now i am gonna print the length of the strings:");
lseek(fd, 0, SEEK_SET); //set the offset at start of the file
while ((nRead = read(fd, buffer, 1)) > 0)
{
tmp = (char)buffer[0];
write(STDOUT_FILENO, &tmp, 1);
}
close(fd);
return 0;
}
this is the result:
write "end" to stop:
hello
world
i am a script
end
now i am gonna print the length of the strings:
I tried to convert the values written in the file into char before write in standard output with no success.
How am i supposed to print on standard output the lengths by using unbuffered I/O? Thank you for your replies
EDIT: i changed the read from file with this:
while((read(fd, &buffer, 1)) > 0)
{
tmp = (int)*buffer;
sprintf(buffer,"%d:", tmp);
read(fd, &buffer[strlen(buffer)], tmp);
write(STDOUT_FILENO, buffer, strlen(buffer));
}
but actually i have no control on the effective strlen of the string thus the output is this:
13:ciao atottti
4:wow
o atottti
5:fine
atottti
as you can see, the strlength is correct because it consinder the newline character ttoo. Still there is no control on the effective buffer size.

C using open()read()write() to access text modify it and store in a new file

My program is supposed to take text from a file given in the command line change it to uppercase and store it in another file.
It works except the output file has a whole bunch of garbage after the converted text. Thank you
Edit: I changed my read to check for 0 bytes and used ret_in to write per Pyjamas it still pulls two or three garbage values. It's definitely read getting the garbage because when I output the buffer before converting it's there.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#define BUF_SIZE 500
int main(int argc, char* argv[]){
char buffer[BUF_SIZE];
int ret_in;
char inputf[100],outputf[100],txt[4],up[3];
// Takes input and adjusts it to the correct file type.
strcpy(inputf,argv[1]);
strcpy(outputf,argv[1]);
strcat(outputf,".up");
printf("%s\n",outputf);
strcat(inputf,".txt");
printf("%s\n",inputf);
int output, input,wrt;
int total;
//opens input file
input=open(inputf, O_RDONLY);
if (input == -1) {
printf("Failed to open file\n");
exit(1);
}
ret_in = read(input,buffer,BUF_SIZE);
total = ret_in;
// output to console
while (ret_in!= 0) {
// printf("%s\n", buffer);
ret_in = read(input,buffer,BUF_SIZE);
total += ret_in;
}
//ret_in= read(input,&buffer,BUF_SIZE);
puts(buffer);
close(input);
int i = 0;
while(buffer[i]) {
buffer[i] = toupper(buffer[i]);
i++;
}
// output buffer in console
puts(buffer);
//output filename in console
printf("%s\n",outputf);
// Opens or creates output file with permissions.
output = open(outputf, O_CREAT| S_IRUSR | O_RDWR);
if (output == -1) {
printf("Failed to open or create the file\n");
exit(1);
}
// write to output file
wrt = write(output, buffer,total);
close(output);
return 0;
}
Because you read ret_in bytes from file, but you write BUF_SIZE to the file, you should write ret_in bytes to the file. You are not supposed to read BUF_SIZE bytes from file every time, it depends, right?
write(output, buffer,BUF_SIZE);//wrong
write(output, buffer,ret_in); //right

mmap is wiping my file instead of copying it

So I'm using mmap to then write to another file. But the weird thing is, when my code hits mmap, what it does is clears the file. So I have a file that's populated with random characters (AB, HAA, JAK, etc...). What it's supposed to do is use mmap as read basically and then write that file to the new file. So that first if (argc == 3) is the normal read and write, the second if (argc ==4) is supposed to use mmap. Does anyone have any idea why on Earth this is happening?
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/io.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/resource.h>
int main(int argc, char const *argv[])
{
int nbyte = 512;
char buffer[nbyte];
unsigned char *f;
int bytesRead = 0;
int size;
int totalBuffer;
struct stat s;
const char * file_name = argv[1];
int fd = open (argv[1], O_RDONLY);
int i = 0;
char c;
int fileInput = open(argv[1], O_RDONLY);
int fileOutPut = open(argv[2], O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
fstat(fileInput, &s);
size = s.st_size;
printf("%d\n", size);
if (argc == 3)
{
printf("size: %d\n", size);
printf("nbyte: %d\n", nbyte);
while (size - bytesRead >= nbyte)
{
read(fileInput, buffer, nbyte);
bytesRead += nbyte;
write(fileOutPut, buffer, nbyte);
}
read(fileInput, buffer, size - bytesRead);
write(fileOutPut, buffer, size - bytesRead);
}
else if (argc == 4)
{
int i = 0;
printf("4 arg\n");
f = (char *) mmap (0, size, PROT_READ, MAP_PRIVATE, fileInput, 0);
/* This is where it is being wipped */
}
close(fileInput);
close(fileOutPut);
int who = RUSAGE_SELF;
struct rusage usage;
int ret;
/* Get the status of the file and print some. Easy to do what "ls" does with fstat system call... */
int status = fstat (fd, & s);
printf("File Size: %d bytes\n",s.st_size);
printf("Number of Links: %d\n",s.st_nlink);
return 0;
}
EDIT: I wanted to mention that the first read and write works perfectly, it is only when you try to do it through the mmap.
If you mean it's clearing your destination file, then yes, that's exactly what your code will do.
It opens the destination with truncation and then, in your argc==4 section, you map the input file but do absolutely nothing to transfer the data to the output file.
You'll need a while loop of some description, similar to the one in the argc==3 case, but which writes the bytes in mapped memory to the fileOutput descriptor.

Resources