Answer the question
In order to leave comments, you need to log in
Why does it throw ERROR:Segmentation fault (core dumped) here?
I solve the problem of implementing a stream, and in it copying the contents of one file to another. I wrote the program, implemented the arguments of the function through the structure. Everything seems to be according to Feng Shui .. But, firstly, the compiler is unhappy, and secondly, when launched directly the program gives an error with memory .. I don’t understand what the error is. I create (open) files using system calls. Also, the compiler issues several warnings1):
1. 32 line warning: passing argument 1 of 'open' makes pointer from integer without a cast [-Wintconversion]
2. note: expected 'const char *' but argument is of type 'char'
3. 35 line warning: format '%s' expects argument of type 'char *', but argument 3 has type 'int' [-Wformat=]
4. 53 line warning: assignment to 'char' from 'char *' makes integer from pointer without a cast [-Wint-conversion
]
#include <stdio.h>
#include <pthread.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#define BUF_SIZE 4096
struct thread_arg
{
char* file, file2;
};
void * cp_sys (void * arg)
{
char buffer[BUF_SIZE];
struct thread_arg targ = *(struct thread_arg *) arg;
int in,out;
ssize_t bytes;
in = open( targ.file, O_RDONLY | O_CREAT );
if (in == -1)
{
fprintf(stderr,"Cannot open file %s\n", targ.file);
return NULL;
}
out = open( targ.file2, O_WRONLY | O_CREAT | O_TRUNC, 0640);
if ( out == -1 )
{
fprintf(stderr, "Cannot creat file %s\n", targ.file2);
return NULL;
}
while ((bytes = read ( in, buffer, BUF_SIZE)) > 0)
write (out , buffer, bytes );
close (in);
close (out);
return NULL;
}
int main (void)
{
pthread_t thread;
int result;
struct thread_arg targ;
targ.file = "file.in";
targ.file2 = "file.out";
result = pthread_create(&thread, NULL, &cp_sys, &targ);
while(1);
return 0;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question