LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-29-2003, 06:55 PM   #1
linuxanswer
Member
 
Registered: Oct 2003
Distribution: woodY 3.0 stable
Posts: 61

Rep: Reputation: 15
sorry, but why?


I'm not understanding why this doesn't work, can you help me please?

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>

int main( int argc, char *argv[] ) {

/* int EXIT_FAILURE=1; */

FILE *sti;
FILE *std;
char *name;
char *name2;
unsigned int size = 512;

if( argc == 0 ) {

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);

std = fopen ( name2, "r" );
if( ( std = fopen( name2, "r" ) ) == NULL ) {
perror( "(2)Problems with the second file,it doesn\'t exist\n\n ");
exit( EXIT_FAILURE );
} else
return((int)std);

memcpy( &name, &name2, sizeof ( size ) );
if( (memcpy( &name, &name2,sizeof ( size ) ) ) == NULL ) {
perror( "can\'t allocate memory" );
exit( EXIT_FAILURE );
}

return(0);

}


in any way i compile this with : ./dcopy file1 file2 it returns always

(1)The fist file do not exists

(2)Problems with the second file,it doesn't exist

--------

And another question.What's the basic mode in gcc?There are a few mode?
 
Old 10-29-2003, 07:15 PM   #2
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
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).

Last edited by ToniT; 10-29-2003 at 07:18 PM.
 
Old 10-29-2003, 07:21 PM   #3
linuxanswer
Member
 
Registered: Oct 2003
Distribution: woodY 3.0 stable
Posts: 61

Original Poster
Rep: Reputation: 15
tnx , but it's a simple test, not more
 
Old 10-29-2003, 07:29 PM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
And you have dangling pointers name1, name2 - you cannot strcpy or memcpy anything into them.
 
Old 10-29-2003, 09:07 PM   #5
linuxanswer
Member
 
Registered: Oct 2003
Distribution: woodY 3.0 stable
Posts: 61

Original Poster
Rep: Reputation: 15
tonit why i have set memcpy twice i see onces


memcpy( &name, &name2, sizeof ( size ) );
if( (memcpy( &name, &name2,sizeof ( size ) ) ) == NULL ) {
perror( "can't allocate memory" );
exit( EXIT_FAILURE );
}
 
Old 10-30-2003, 10:12 AM   #6
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
Code:
// 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 ) {
1+1 = 2 = one too much.
 
Old 10-30-2003, 10:15 AM   #7
linuxanswer
Member
 
Registered: Oct 2003
Distribution: woodY 3.0 stable
Posts: 61

Original Poster
Rep: Reputation: 15
ok, but how can i set this error?
 
Old 10-30-2003, 10:29 AM   #8
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
I don't understand your question.

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.
 
Old 10-30-2003, 10:33 AM   #9
linuxanswer
Member
 
Registered: Oct 2003
Distribution: woodY 3.0 stable
Posts: 61

Original Poster
Rep: Reputation: 15
if i don't make memcpy( &name, &name2, sizeof ( size ) );
if( (memcpy( &name, &name2,sizeof ( size ) ) ) == NULL )

how can i check this error?eh? memcpy==NULL?
 
Old 10-30-2003, 10:48 AM   #10
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
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).
 
Old 10-30-2003, 11:34 AM   #11
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Code:
#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);

}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration