LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 09-10-2007, 02:45 PM   #1
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,561

Rep: Reputation: 57
gcc, filecopy program ? (corrections)


How can I update this program to make filecopy file1 to file2 ?

Code:
#include <stdio.h>
#define max 60
#define min 3
main()
{
        int count;
        int i;
        char name[max];
        char extname[]="jpg";
        FILE *fp;
        FILE*fp2;
        int c;
        int zero='0';

        printf("Please input the name of the file");
        scanf("%s",name);
        printf("\n");
        printf("Please input file count");
        scanf("%d",&count);
        printf("\n");

        fp=fopen("dmtmp","wr");
        for( i = 0; i < count ; i++){
                fprintf(fp,"%s",name);
                fprintf(fp,"%3d",i);
                fprintf(fp,"%3d",i+1);
                fprintf(fp,".%s",extname);
                fprintf(fp,"\n");
        }
       
        rewind(fp);
        fp2=fopen("dmlist","w");
       
        while((c = getc(fp)) != EOF){
                if ( c == ' ')
                        putc('0',fp2);
                else
                        putc(c,fp2);
        }

        fclose(fp);
        fclose(fp2);
}

Code:
#include <stdio.h>

main(int argc, char *argv[])
{
        FILE *fp;
        void filecopy(FILE * , FILE * );

        if ( argc == 1 )
                filecopy(stdin,stdout);
        else
                while ( --argc > 0)
                         if((fp=fopen(*++argv,"r")) == NULL ){
                                printf("cat:can'topen %s\n",*argv);
                                return 1;
                        }else{
                                filecopy(fp,stdout);
                                fclose(fp);
                        }
                        return 0;
}

void filecopy(FILE *ifp, FILE *ofp)
{
        int c;

        while ((c = getc(ifp)) != EOF)
                if(c == ' ')
                        putc('0',ofp);
                else
                        putc(c,ofp);
               
}
 
Old 09-10-2007, 11:14 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
May I suggest reading from & writing to files in much larger blocks than single bytes? Why do you inspect every byte, and replace space characters with '0's? If you really need to do this, then your program function is probably about as good as it can be.

I'm not sure if function declarations are allowed the way you've done it, but it is more conventional to make the declaration file file-scoped, by putting the declaration outside of main(), or any other function. I suppose if the function calling the declared function is the only caller, it makes some sense. In that case, I would tend to make it a static function.

--- rod.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
GCC not able to run C program sco1984 Mandriva 5 08-12-2006 06:11 PM
Can't compile c++ program using gcc pengu Programming 9 02-22-2006 01:59 PM
filecopy from xp to samba slow, copy to mounted share fast xtra Linux - Software 6 07-29-2004 04:37 PM
Filecopy Using Sockets Without Explicitly Running Server Side Program. chaits Linux - Networking 3 03-11-2004 02:08 AM
new patch in linux includes the corrections of previous versions? ysturdivant Linux - Security 3 11-05-2003 03:24 PM

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

All times are GMT -5. The time now is 06:13 PM.

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