ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
fprintf( stderr,"Too few arguments maybe you must use: dcopy %s <filesource> %s <filedestination>\n\n");
}
if( argc > 3 ) {
fprintf( stderr,"\nToo many arguments, the correct usage is: ./dcopy <filesource> <filedestination>\n\n") ;
perror("too many arguments");
exit( EXIT_FAILURE );
}
sti = fopen( name,"r" );
if( ( sti = fopen( name,"r" ) ) == NULL )
fprintf( stderr,"(1)The fist file do not exists\n\n" );
else
return((int)sti);
gcc -Wall says..
mess.c: In function `main':
mess.c:18: warning: too few arguments for format
Some problems:
You try to open both the files twice. Don't.
You call the memcpy twice. I see no reason for that.
The perror should only be used after a syscall that sets the errno value to something smart, not spontaneously (like the too many arguments case)
The first 'if' won't stop the execution. It should.
You mustn't "return" in the middle of the execution of your main-function (syntactially that is not a probem, but you do want to run to the end of the function when there is no error)
The problem in line 18 is that you give a promise of arguments to the (f)printf -method, but you give no arguments. Either give some arguments (what?), or take the %s marks away.
memcpy (or the sizeof operator) doesn't work that way. sizeof(size) gives you a sizeof int, that is 4 bytes. If you want to copy for example 512 bytes, replace "sizeof(size)" to size.
To pass a pointer, you should not pass a pointer to a pointer. That is, the proper syntax for memcpy is not '&name, &name2', but 'name,name2'.
I don't understand why you are copying the filenames to eachother, and why are you doing it with memcpy. If you want to copy strings, use strcpy. If you want to access the contents of the files you have opened, see the fread function (man fread).
// This is a call to memcpy
memcpy( &name, &name2, sizeof ( size ) );
//
if((
//This is a call to memcpy also
memcpy( &name, &name2,sizeof ( size ) )
) == NULL ) {
Also I don't understand why you are interested of the return value of memcpy. According to my manpage (man 3 memcpy), it only returns a pointer to dest, no errorcode.
And as jim mcnamara said, your name and name2 are not set to any value. You have to fix this and other problems i mentioned in my first post. Actually the perror issue is not critical, but other are in a sense that they either lead to a crash or the program working plain wrong.
See the manpage memcpy doesn't fail and atleast won't express the failure by returning any value.
Garbage in, garbage out. You have to take care that the destination refers to a memory that you are allowed to write(that is, you have allocated the space by malloc or you have got the area by other means) and the src points to somewhere you are allowed to read, and they do not overlap. If first two won't be fullfilled, a segfault happens. If the last won't be fullfilled, an unexpected behaviour (segfault or corrupted data).
#include <stdio.h>
/* #include <errno.h> not needed */
#include <string.h>
#include <stdlib.h>
int main( int argc, char *argv[] ) {
/* int EXIT_FAILURE=1; a #define in stdlib*/
FILE *sti;
FILE *std;
char name[512];
char name2[512];
unsigned int size = 511;
if( argc == 1 || argc > 3) {
fprintf( stderr,"Wrong number of arguments \n\tyou must use: dcopy <filesource> <filedestination>\n\n");
exit(EXIT_FAILURE);
}
strcpy(name,argv[1]);
if( ( sti = fopen( name,"r" ) ) == NULL ) {
perror("First file dioes not exist"); /* use perror when a real error occurs */
exit(EXIT_FAILURE);
/* this return() should not be here
return((int)sti); */
} /* end if block */
strcpy(name2,argv[2]);
if( ( std = fopen( name2, "r" ) ) == NULL ) {
perror( "(2)Problems with the second file\n\n ");
exit( EXIT_FAILURE );
} /* end if block remove return() */
/* this makes no sense, either syntactically or logically */
/* --------------------------------
memcpy( name, name2, (size_t) size ); <- correct usage -- the return value is useless
if( (memcpy( &name, &name2,sizeof ( size ) ) ) == NULL ) {
perror( "can't allocate memory" ); <-- PS: you allocate memory with malloc()
exit( EXIT_FAILURE );
}
----------- so bye bye code for now */
/* at this point I'm guessing you want to append the first file onto the second file */
fclose(std);
std=fopen(name2,"r+"); /* oepn the second file for append */
if(fseek(std,0,SEEK_END)){
perror("Error positioning read cursor in file #1 ");
exit(EXIT_FAILURE); /* closes all files, exits */
} /* go to end of destination file name2 */
while(!feof(sti)){
char tmp[8192]; /* buffer to read/write files */
memset(tmp,0x00,sizeof(tmp));
if(fgets(tmp,8191,sti)!=NULL){ /* write each record in name to name2 */
fprintf(std,"%s",tmp);
}
}
fclose(sti); /* close the files */
fclose(std);
return(0);
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.