I am having a structure
struct myStruct {
fstream fp;
char *buffer;
size_t size;
};
I am new to C++ and trying to write a code wherein one thread will read from a file into buffer and main thread will write the buffer to other file. The sample of the code is as follows:
int main() {
pthread tid1;
struct myStruct readArgs;
fstream fileP, fileP2;
fileP.open("/tmp/20MBFile", fstream::in);
fileP2.open("/tmp/trial-file", fstream::out);
char *buffer;
readArgs.fp = fileP;
readArgs.buffer = buffer;
readArgs.size = 1048576;
pthread_create(&tid1, NULL, Read, &readArgs);
pthread_join(tid1, NULL);
fileP2.write(buffer, 1048576);
......
}
The read function is as follows:
void *Read(struct myStruct *readArgs) {
readArgs->fp.read(readArgs->buffer, readArgs->size);
pthread_exit(NULL);
}
however, when I compile my code I get following errors:
error: use of deleted function 'std::basic_fstream& std::basic_fstream::operator=(const std::basic_fstream&)' readArgs.fp = fileP;
AND
error: invalid conversion from 'void* ()(myStruct)' to 'void* ()(void)' [-fpermissive] pthread_create(&tid1, NULL, Read, &readArgs); ^ In file included from /usr/lib/gcc/x86_64-redhat-linux/4.8.3/../../../../include/c++/4.8.3/x86_64-redhat-linux/bits/gthr-default.h:35:0, from /usr/lib/gcc/x86_64-redhat-linux/4.8.3/../../../../include/c++/4.8.3/x86_64-redhat-linux/bits/gthr.h:148, from /usr/lib/gcc/x86_64-redhat-linux/4.8.3/../../../../include/c++/4.8.3/ext/atomicity.h:35, from /usr/lib/gcc/x86_64-redhat-linux/4.8.3/../../../../include/c++/4.8.3/bits/ios_base.h:39, from /usr/lib/gcc/x86_64-redhat-linux/4.8.3/../../../../include/c++/4.8.3/ios:42, from /usr/lib/gcc/x86_64-redhat-linux/4.8.3/../../../../include/c++/4.8.3/ostream:38, from /usr/lib/gcc/x86_64-redhat-
....
/usr/include/pthread.h:232:12: error: initializing argument 3 of 'int pthread_create(pthread_t*, const pthread_attr_t*, void* ()(void), void*)' [-fpermissive] extern int pthread_create (pthread_t *__restrict __newthread,
Am I missing anything here? Thanks in advance!
struct
is optional instruct myStruct readArgs;
for c++ – sjsamstruct
's members directly instead of creating extraneous local variables? What is the purpose of the local variablesbufffer
,fileP
,fileP2
? They serve no purpose and yourmain
is just doing unnecessary work assigning to them, and then you assign them to your struct's members. – PaulMcKenziestruct
directly (readArgs.fp.open(...);
) Second,buffer
is uninitialized pointer, and your code is attempting to use it in that state. – PaulMcKenzie