Thread: could you help with me C code?thanks,a little urgent.

  1. #16
    Registered User
    Join Date
    Nov 2008
    Posts
    42

    Unhappy

    Quote Originally Posted by Adak View Post
    Post it up and show the errors from the compiler - I don't have your compiler so that may be a problem.

    Others have gcc, however.
    10-1.c: In function ‘main’:
    10-1.c:16: error: redeclaration of ‘sourcename’ with no linkage
    10-1.c:10: error: previous declaration of ‘sourcename’ was here
    10-1.c:19: error: redeclaration of ‘destinationname’ with no linkage
    10-1.c:11: error: previous declaration of ‘destinationname’ was here
    10-1.c: At top level:
    10-1.c:57: warning: parameter names (without types) in function declaration
    10-1.c:57: warning: data definition has no type or storage class
    10-1.c:58: warning: parameter names (without types) in function declaration
    10-1.c:58: warning: data definition has no type or storage class
    10-1.c:59: warning: data definition has no type or storage class
    10-1.c:61: error: syntax error before ‘}’ token

    Code:
    #include<stdio.h> 
    #include<string.h>      // for strlen() function. 
    #include<stdlib.h>      // for exit() function. 
    
    
    
    int main()   
    {  
    	int data[1024];
    	char   sourcename[20];  
    	char   destinationname[20]; 
    	FILE   *sFile,*dFile;   
    	long t;
    	
    	
     char sourcename[20];
    	printf("please input source filename");
     gets(sourcename);
     char destinationname[20];
    	printf("please input destination filename");
     gets(destinationname);
    	
    
     if((sFile=fopen(sourcename,"rb"))==NULL)   
     {   
      printf("fail to open source file\n");   
      exit(0);   
     }
       
     if((dFile=fopen(destinationname,"wb"))==NULL)   
     {   
      printf("fail to open destination file\n");   
      exit(0);   
     }   
    	
    	fseek(sFile,0L,SEEK_END);
    	t=ftell(sFile);  //get the size of the file.
    	
    #if 0	
    	for(;(feof(sFile)!=0);)
    	{
    		fread(data,t,1,sFile);
    		printf("%s",sFile);
    fwrite(data,t,1,dFile);
    		printf("%s",dFile);
    	}
    #else 
    	while (1) {
    		fread(data,t,1,sFile);
    		fwrite(data,t,1,dFile);
    		if(feof(sFile)==0)
    			break;
    		}
    	
    #endif
    	}
     fclose(sFile);   
     fclose(dFile);
    	getch();
       
    }

    i still don't know how to change those codes into a while() loop you told me.....


    i am so stupid

  2. #17
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    Code:
    #include<stdio.h> 
    #include<string.h>      // for strlen() function. 
    #include<stdlib.h>      // for exit() function. 
    
    
    
    int main()   
    {  
    	int data[1024];
    	char   sourcename[20];  
    	char   destinationname[20]; 
    	FILE   *sFile,*dFile;   
    	long t;
    	
    	
     char sourcename[20];
    	printf("please input source filename");
     gets(sourcename);
     char destinationname[20];
    	printf("please input destination filename");
     gets(destinationname);
    	
    
     if((sFile=fopen(sourcename,"rb"))==NULL)   
     {   
      printf("fail to open source file\n");   
      exit(0);   
     }
       
     if((dFile=fopen(destinationname,"wb"))==NULL)   
     {   
      printf("fail to open destination file\n");   
      exit(0);   
     }   
    	
    	fseek(sFile,0L,SEEK_END);
    	t=ftell(sFile);  //get the size of the file.
    	
    #if 0	
    	for(;(feof(sFile)!=0);)
    	{
    		fread(data,t,1,sFile);
    		printf("%s",sFile);
    fwrite(data,t,1,dFile);
    		printf("%s",dFile);
    	}
    #else 
    	while (1) {
    		fread(data,t,1,sFile);
    		fwrite(data,t,1,dFile);
    		if(feof(sFile)==0)
    			break;
    		}
    	
    #endif
    	}
     fclose(sFile);   
     fclose(dFile);
    	getch();
       
    }
    10-1.c: In function ‘main’:
    10-1.c:16: error: redeclaration of ‘sourcename’ with no linkage
    10-1.c:10: error: previous declaration of ‘sourcename’ was here
    10-1.c:19: error: redeclaration of ‘destinationname’ with no linkage
    10-1.c:11: error: previous declaration of ‘destinationname’ was here
    10-1.c: At top level:
    10-1.c:57: warning: parameter names (without types) in function declaration
    10-1.c:57: warning: data definition has no type or storage class
    10-1.c:58: warning: parameter names (without types) in function declaration
    10-1.c:58: warning: data definition has no type or storage class
    10-1.c:59: warning: data definition has no type or storage class
    10-1.c:61: error: syntax error before ‘}’ token


    i dont know how to change the codes into the while() loop..

    how silly i am~~

  3. #18
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It takes time, and a lot of work. C is no "I fell off the log and was enlightened all about it", kind of thing.

    Matsp and others will be here soon, I'm sure. I'll try your code out in Turbo C and C what I can see.

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Like I tried to say: You are declaring sourcename and destinationname twice - you should only declare those variables ONCE, which is exactly what "recdeclaration without storage class" actually means.

    You still need to store the value returned by fread.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #20
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    Quote Originally Posted by Adak View Post
    It takes time, and a lot of work. C is no "I fell off the log and was enlightened all about it", kind of thing.

    Matsp and others will be here soon, I'm sure. I'll try your code out in Turbo C and C what I can see.
    Quote Originally Posted by matsp View Post
    Like I tried to say: You are declaring sourcename and destinationname twice - you should only declare those variables ONCE, which is exactly what "recdeclaration without storage class" actually means.

    You still need to store the value returned by fread.

    --
    Mats


    Code:
    #include<stdio.h> 
    #include<string.h>      // for strlen() function. 
    #include<stdlib.h>      // for exit() function. 
    
    
    
    int main()   
    {  
    	int data[1024];
    	char   sourcename[20];  
    	char   destinationname[20];    // redeclaration?
    	FILE   *sFile,*dFile;   
    	long t;
    	
    	printf("please input source filename");
     gets(sourcename);
    	printf("please input destination filename");
     gets(destinationname);           //sth wrong with it?
    	
     if((sFile=fopen(sourcename,"rb"))==NULL)   
     {   
      printf("fail to open source file\n");   
      exit(0);   
     }
       
     if((dFile=fopen(destinationname,"wb"))==NULL)   
     {   
      printf("fail to open destination file\n");   
      exit(0);   
     }   
    	
    	fseek(sFile,0L,SEEK_END);
    	t=ftell(sFile);  //get the size of the file.
    	
    #if 0	                       // the codes below is tough to me
    	for(;(feof(sFile)!=0);)
    	{
    		fread(data,t,1,sFile);
    		printf("%s",sFile);
    fwrite(data,t,1,dFile);
    		printf("%s",dFile);
    	}
    #else 
    	while (1) {
    		fread(data,t,1,sFile);
    		fwrite(data,t,1,dFile);   // i don't know what to replace that t.though i think it's wrong!
    		if(feof(sFile)==0)
    			break;
    		}
    	
    #endif
    	}
     fclose(sFile);   
     fclose(dFile);
    	getch();
       
    }

    10-1.c:53: warning: parameter names (without types) in function declaration
    10-1.c:53: warning: data definition has no type or storage class
    10-1.c:54: warning: parameter names (without types) in function declaration
    10-1.c:54: warning: data definition has no type or storage class
    10-1.c:55: warning: data definition has no type or storage class
    10-1.c:57: error: syntax error before ‘}’ token

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need to indent your code properly, and you will find the extra/missing brace [you have one too many at the end of main, but I think it's a missing one above that is really the problem].

    What happens if the size of the file is bigger than 1024 bytes?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #22
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Make the changes I've noted, and it should be close. This compiles with no errors in Turbo C.

    Code:
    #include<stdio.h> 
    #include<string.h>      // for strlen() function. 
    #include<stdlib.h>      // for exit() function. 
    
    
    
    int main()   
    {  
    	int data[1024];
    	char   sourcename[20];  
    	char   destinationname[20]; 
    	FILE   *sFile,*dFile;   
    	long t;
    	
    	
    // char sourcename[20];  //<---Delete this line*********
    	printf("please input source filename");
     gets(sourcename);
    // char destinationname[20];   //<----Delete this line*****
    	printf("please input destination filename");
     gets(destinationname);
    	
    
     if((sFile=fopen(sourcename,"rb"))==NULL)   
     {   
      printf("fail to open source file\n");   
      exit(0);   
     }
       
     if((dFile=fopen(destinationname,"wb"))==NULL)   
     {   
      printf("fail to open destination file\n");   
      exit(0);   
     }   
    	
    	fseek(sFile,0L,SEEK_END);
    	t=ftell(sFile);  //get the size of the file.
    	
    //#if 0
    	
    	for(;(feof(sFile)!=0);)
    	{
    		fread(data,t,1,sFile);
    		printf("%s",sFile);
                    fwrite(data,t,1,dFile);
    		printf("%s",dFile);
    	}
    //#else 
    /*	you already have a read and write loop, just up above this.
    You don't need another one. :)
    
    while (1) {
    		fread(data,t,1,sFile);
    		fwrite(data,t,1,dFile);
    		if(feof(sFile)==0)
    			break;
    		}
    	
    #endif
    }
    */
     
    fclose(sFile);   
     fclose(dFile);
    	getch();
     return 0;   
    }
    The code in blue you already have, so you can get rid of it. Also, the lines of code with the stars and delete on them.

  8. #23
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    Quote Originally Posted by matsp View Post
    You need to indent your code properly, and you will find the extra/missing brace [you have one too many at the end of main, but I think it's a missing one above that is really the problem].

    What happens if the size of the file is bigger than 1024 bytes?

    --
    Mats
    Code:
    #include<stdio.h> 
    #include<string.h>      // for strlen() function. 
    #include<stdlib.h>      // for exit() function. 
    
    
    
    int main()   
    {  
    	int data[1024];
    	char   sourcename[20];  
    	char   destinationname[20];    // redeclaration?
    	FILE   *sFile,*dFile;   
    	long t;
    	
    	printf("please input source filename");
     gets(sourcename);
    	printf("please input destination filename");
     gets(destinationname);           //sth wrong with it?
    	
    	
    	
     if((sFile=fopen(sourcename,"rb"))==NULL)   
     {   
      printf("fail to open source file\n");   
      exit(0);   
     }
    	
    	
       
     if((dFile=fopen(destinationname,"wb"))==NULL)   
     {   
      printf("fail to open destination file\n");   
      exit(0);   
     }   
    	
    	
    	
    	
    	fseek(sFile,0L,SEEK_END);
    	t=ftell(sFile);  //get the size of the file.
    	
    	
    	
    	{
    #if 0	                       // the codes below is tough to me
    	for(;(feof(sFile)!=0);)
    	   {
    		fread(data,t,1,sFile);
    		printf("%s",sFile);
            fwrite(data,t,1,dFile);
    		printf("%s",dFile);
    	    }
    #else 
    	while (1) 
    	    {
    		fread(data,t,1,sFile);
    		fwrite(data,t,1,dFile);   // i don't know what to replace that t.though i think it's wrong!
    		if(feof(sFile)==0)
    			break;
    		}
    	
    #endif
    	}
              fclose(sFile);   
              fclose(dFile);
    	getch();
       
    }
    appledemacbook:documents apple$ gcc 10-1.c
    Undefined symbols:
    "_getch", referenced from:
    _main in ccnCLv2m.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status

  9. #24
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You already have the get data & print data to file loop, in that for loop. You can delete the while(1) loop - it's job is already done.

    You should change getch() to getchar().
    Last edited by Adak; 11-23-2008 at 07:57 AM.

  10. #25
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    Code:
    #include<stdio.h> 
    #include<string.h>      // for strlen() function. 
    #include<stdlib.h>      // for exit() function. 
    
    
    
    int main()   
    {  
    	int data[1024];
    	char   sourcename[20];  
    	char   destinationname[20];    // redeclaration?
    	FILE   *sFile,*dFile;   
    	long t;
    	
    	printf("please input source filename");
     gets(sourcename);
    	printf("please input destination filename");
     gets(destinationname);           //sth wrong with it?
    	
    	
    	
     if((sFile=fopen(sourcename,"rb"))==NULL)   
     {   
      printf("fail to open source file\n");   
      exit(0);   
     }
    	
    	
       
     if((dFile=fopen(destinationname,"wb"))==NULL)   
     {   
      printf("fail to open destination file\n");   
      exit(0);   
     }   
    	
    	
    	
    	
    	fseek(sFile,0L,SEEK_END);
    	t=ftell(sFile);  //get the size of the file.
    	
    	
    	
    	
    	                       // the codes below is tough to me
    	for(;(feof(sFile)!=0);)
    	   {
    		fread(data,t,1,sFile);
    		printf("&#37;s",sFile);
            fwrite(data,t,1,dFile);
    		printf("%s",dFile);
    	    }
    
    
    	
              fclose(sFile);   
              fclose(dFile);
    	getchar();
       
    }
    can the codes feed the instructions of the question?
    it require us to copy data in binary with 1024 bytes in a loop.

  11. #26
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    No, the program code won't "feed" anything, but I don't really understand your question. I would have it open a few files, and then see if they compare OK with the original.

    Maybe a small and medium sized bitmap files - and some small and medium text files?

  12. #27
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, so the comment that "you already have one loop" is incorrect. It is commented out using #if 0, so there is only one loop. I personally prefer the variant with the while(1) loop.

    Aside from STILL not using the value returned by fread, you are also still assuming that the entire file will fit in 1024 bytes, as t is the length of the file, and if we pick an arbitrary file in the system, it may be larger than 1024 bytes. What I'm saying here is that "t" is not the right value to pass to fread.

    Further, after you seek to the end of the file, how much data do you expect to be able to read after that? Do you think it may be a good idea to do something about that?

    There is a reasion I'm not telling you exactly what to do: You need to learn to UNDERSTAND what your code is doing, not just randomly change it without understanding why. Otherwise, you will never learn how to write programs.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #28
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    Quote Originally Posted by Adak View Post
    No, the program code won't "feed" anything, but I don't really understand your question. I would have it open a few files, and then see if they compare OK with the original.

    Maybe a small and medium sized bitmap files - and some small and medium text files?
    what i want to do is feed the request of the question, however, it is not easy to fully understand and do that task.
    i think a small text file is okay.
    the codes is really beyond my head.

    hth

  14. #29
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by matsp View Post
    Right, so the comment that "you already have one loop" is incorrect. It is commented out using #if 0, so there is only one loop. I personally prefer the variant with the while(1) loop.

    Aside from STILL not using the value returned by fread, you are also still assuming that the entire file will fit in 1024 bytes, as t is the length of the file, and if we pick an arbitrary file in the system, it may be larger than 1024 bytes. What I'm saying here is that "t" is not the right value to pass to fread.

    Further, after you seek to the end of the file, how much data do you expect to be able to read after that? Do you think it may be a good idea to do something about that?

    There is a reasion I'm not telling you exactly what to do: You need to learn to UNDERSTAND what your code is doing, not just randomly change it without understanding why. Otherwise, you will never learn how to write programs.

    --
    Mats
    I liked the while(1) loop better, myself, but I saw the for loop there, and thought his idea should be followed. HH, if you have no preferences, why not remove the for loop, and put the while(1) loop into it's place.

    Note that the if 0 line of code, is gone, and should stay gone.

    We do have a problem with the fread() line. IMO "t" should not be in that line. You have a buffer to put the data into, and the data has to fit into that buffer. T has nothing to do with that.

    I suspect that T is not even needed, but Matsp is the expert.

    I see it as "You're going to take a bunch of apples & put them in a box of 1024, and stack them in a warehouse". You don't care how many apples there are in the truck, because you have to take them all, anyway."

  15. #30
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Write a file copy program that copies an existing file into another file. The program should ask the user to enter the source file name and destination file name. The file copy should be performed in binary mode. Use fseek() to move within the source file and ftell() to determine the size of the file (if needed). Read the data from the source file and write the values read in the destination file in a loop. Read and write data in blocks of 1024 bytes.
    If you are supposed to read in blocks of 1024 bytes, it implies to me that files BIGGER than 1024 bytes can be expected. A small text file may well be smaller than 1024 bytes. Also, it may hide problems if you do not try to copy some non-text content (an a.out file would work as a good example of a non-text content).

    Use "diff" to compare the files.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BitWise Newbie - Needs Urgent Help
    By frodonet in forum C Programming
    Replies: 15
    Last Post: 09-26-2007, 12:58 PM
  2. display character size...(quite urgent..)
    By karthi in forum C Programming
    Replies: 10
    Last Post: 07-11-2007, 09:42 PM
  3. Help.... Urgent... Thanks!
    By weihann in forum C Programming
    Replies: 0
    Last Post: 02-27-2002, 10:15 PM