Copying certain contents of a file to another - c

I am trying to limit the amount of lines I copy over to a file to x amount. I know how to copy the files, but how would I limit it, for example, 10 lines.
Code:
char buf[BUFSIZ];
int outft, inft,fileread;
if((outft = open("TMPFILE3", O_CREAT | O_APPEND | O_RDWR, 0644))==-1){
perror("open");
}
inft = open("TMPFILE", O_RDONLY);
if(inft >0){
fileread = read(inft, buf, sizeof(buf));
printf("%s\n", buf);
write(outft, buf, fileread);
close(inft);
}
close(outft);

Related

Cannot send data larger than 64 KB with pipes in C

I am trying to send usr/share/dict/words file (which is around 971 KB) from parent to two child processes via named pipes. However, output is empty and I did not get any errors.I waited for a minute but nothing changed. Program works fine with 10KB of data that I tried with. I know that pipe buffer size is 64KB but can someone explain what is wrong with my approach:
Here in the code:
- ptr_child_1 refers to shared memory pointer, 1 MB of shared segment mapped.
- fd and sd are pipes, file is the file pointer
Code is long that is why I did not paste all of them. I just wonder how I should send that large file
Parent write:
mknod(FIFO_1, S_IFIFO | 0666, 0);
mknod(FIFO_2, S_IFIFO | 0666, 0);
// open pipes
int fd = open(FIFO_1, O_WRONLY);
int sd = open(FIFO_2, O_WRONLY);
char str[3000];
while(fgets(str, sizeof(str), file) > 0)
{
// write all lines of file
write(fd, str, strlen(str));
write(sd, str, strlen(str));
}
// send EOF explicitly
write(fd, "\0", 1);
write(sd, "\0", 1);
// close file and pipes
close(fd);
close(sd);
fclose(file);
Child read:
int num;
char s[3000];
while((num = read(fd, s, sizeof(s))) > 0)
{
sprintf(ptr_child_1, "%s", s);
ptr_child_1 += num;
}
// close pipe
close(fd);

Call system UNIX - File copy in C

I try to create a copy of a source file but the target file is always empty.
The algorithm is: read from STDIN and write to source file, then read on this file and write the text in target file.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#define BUFFSIZE 8192
int main(){
int fdsource, fdtarget;
int n, nr;
char buff[BUFFSIZE];
fdsource = open("source.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); // Create and open a source file in read/write
if (fdsource < 0){
printf("Source file open error!\n");
exit(1);
}
fdtarget = open("target.txt", O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); // Create and open a source file in write only
if (fdtarget < 0){
printf("Target file open error!\n");
exit(1);
}
printf("\nInsert text:\n");
while ((n = read(STDIN_FILENO, buff, BUFFSIZE)) > 0){ // Read from STDIN and write to source file
if ((write(fdsource, buff, n)) != n){
printf("Source file write error!\n");
exit(1);
}
}
while ((read(fdsource, buff, n)) > 0){ // Read from source file and write to target file
if ((write(fdtarget, buff, n)) != n){
printf("Source file open error!\n");
exit(1);
}
}
close(fdsource);
close(fdtarget);
exit(0);
return 0;
}
The problem with your code is "You have opened both the file in initial stage". To solve the problem just open the source file in the write mode and write all the data, then close and reopen the source file in read mode, Then open the target file in the write mode.
The modified code is given below and it was not tested
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#define BUFFSIZE 8192
int main(){
int fdsource, fdtarget;
int n;
char buff[BUFFSIZE];
fdsource = open("source.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); // Create and open a source file in read/write
if (fdsource < 0){
printf("Source file open error!\n");
exit(1);
}
printf("\nInsert text:\n");
while ((n = read(STDIN_FILENO, buff, BUFFSIZE)) > 0){ // Read from STDIN and write to source file
if ((write(fdsource, buff, n)) != n){
printf("Source file write error!\n");
exit(1);
}
}
close(fdsource);
fdsource = open("source.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); // Create and open a source file in read/write
if (fdsource < 0){
printf("Source file open error!\n");
exit(1);
}
fdtarget = open("target.txt", O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); // Create and open a source file in write only
if (fdtarget < 0){
printf("Target file open error!\n");
exit(1);
}
while ((read(fdsource, buff, n)) > 0){ // Read from source file and write to target file
if ((write(fdtarget, buff, n)) != n){
printf("Source file open error!\n");
exit(1);
}
}
close(fdsource);
close(fdtarget);
exit(0);
return 0;
}
If am wrong anywhere use the logic mentioned above.

Open() for output not creating file

I have this function that utilizes open to set i/o redirection:
void setOutput(char * buffer){
int file = open(buffer, O_WRONLY || O_CREAT, S_IWUSR);
if(file < 0){ printf("error opening %s for output\n", buffer); }
if(dup2(file, 1) < 0){ printf("error with dup2 opening %s for output\n", buffer); }
}
When I run it, it works fine for files that are already defined but returns -1 when it receives a non-created file. Not sure why
You need to change the following
int file = open(buffer, O_WRONLY || O_CREAT, S_IWUSR);
To
int file = open(buffer, O_WRONLY | O_CREAT, S_IWUSR);
Format :
int open( char *filename, int access, int permission );
access : Should be provided as a bit wise OR operator, that means using | not || which is logical OR

Linux C write buffer to file from a while loop issue

I have the following function (that dumps a process memory region). If I write to stdout write(STDOUT_FILENO, buf, rd); it outputs the buffer correctly, the problem rises when I want to write the buffer to a file, the file gets written but with the same date over and over:
void dump_region(int fd, off64_t start, off64_t end)
{
char buf[4096];
int fdo;
fdo = open("memdump_log", O_WRONLY | O_CREAT, 0644);
if (fdo == -1) {
fprintf(stderr, "open failed: %m\n");
close(fd);
exit(1);
}
lseek64(fd, start, SEEK_SET);
while(start < end) {
int rd;
rd = read(fd, buf, 4096);
write(fdo, buf, rd);
//write(STDOUT_FILENO, buf, rd);
start += 4096;
}
close(fdo);
}
The function is accessed from main() like this:
if(maps && mem != -1) {
char buf[BUFSIZ + 1];
while(fgets(buf, BUFSIZ, maps)) {
off64_t start, end;
sscanf(buf, "%llx-%llx", &start, &end);
dump_region(mem, start, end);
}
}
Any idea where am I wrong?
Modify
fdo = open("memdump_log", O_WRONLY | O_CREAT, 0644);
into
fdo = open("memdump_log", O_WRONLY | O_CREAT | O_APPEND, 0644);
You need to seek to the end of your output file, or passing the O_APPEND to open
You keep reopening the output file on every call to dump_region. When opening a file it will always start writing at the start. Either keep the file open all the time, seek to the end, or try the O_APPEND flag.

Appending to existing file from resource file in Linux C Program

i've trying to perform appending to an existing file from resource file using the C programming in Linux. However, my code doesn't work for that, can any1 do tell me what's wrong with the code and also how O_APPEND is working? thanks :)
char ifile[150];
char tfile[150];
char cc;
system("clear");
printf("Please enter your resource file name : ");
gets(ifile);
printf("Please enter your destination file name : ");
gets(tfile);
int in, out;
in = open(ifile, O_RDONLY);
int size = lseek(in,0L,SEEK_END);
out = open(tfile, O_WRONLY |O_APPEND);
char block[size];
int pdf;
while(read(in,&block,size) == size)
pdf = write(out,&block,size);
close(in);close(out);
if(pdf != -1)
printf("Successfully copy!");
else
perror("Failed to append! Error : ");
printf("Press enter to exit...");
do
{
cc = getchar();
} while(cc != '\n');
The problem here is that you deplace the reading cursor at the end of the file in order to know its size, but you don't rewind to the start of the file to be able to read. So read() reads EOF, and returns 0.
int size = lseek(in, 0L, SEEK_END);
out = open(tfile, O_WRONLY | O_APPEND);
should be
int size = lseek(in, 0L, SEEK_END);
lseek(in, 0L, SEEK_SET);
out = open(tfile, O_WRONLY | O_APPEND);
In addition, when you read and write, you should use block and not &block, since block is already a pointer (or an address).
Oh, and also... When you open the file out for writing... It will fail if the file does not exist already.
Here how to create it with rights set to 644:
out = open(tfile, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
(This won't have any effect if the file already exists)

Resources