cat project

This is a discussion on cat project within the C Programming forums, part of the General Programming Boards category; hi all Code: #include<stdio.h> #include<string.h> #include<unistd.h> #define BUF 250 char read_file(char *file_name); char write_file(char *file_name); void help(char *string); main(int argc,char ...

  1. #1
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127

    cat project

    hi all
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<unistd.h>
    
    #define BUF 250
    
    char read_file(char *file_name);
    char write_file(char *file_name);
    void help(char *string);
    
    main(int argc,char **argv)
    {
    int c;
    char *file_name;
    if(argc<2||argc>3)
    {
              help(argv[0]);
    
              }
    while((c=getopt(argc,argv,"r:w:h"))!=-1)
    {
                                         switch(c)
                                         {
                                                  case 'r':
                                                     file_name=strdup(optarg);
                                                     read_file(file_name);/* inside there is a while loop for reading the file this function only called onec"*/
                                                       break;
                                                       case 'w':
                                                            file_name=strdup(optarg);
                                                            write_file(file_name);/* inside there is a while loop for writing in the file this function only called onec*/
                                                       case 'h':
                                                            help(argv[0]);
                                                            break;
                                         
                                                  }
                                                  }
                                                  }
    char read_file(char *file_name)
    {
         FILE *fp;
         fp=fopen(file_name,"r");
         if(fp==NULL)
         {
         fprintf(stderr,"Couldn't open File %s\n",file_name);
         exit(1);
                     }
         char buffer[BUF];
         printf("\n========================Reading======================\n");
         while((fgets(buffer,250,fp))!=NULL)
         {
         printf("%s",buffer);
         }
                      fclose(fp);
                      }
                                            
     char write_file(char *file_name)
     {
     FILE *fp;
         fp=fopen(file_name,"w");
         if(fp==NULL)
         {
                    fprintf(stderr,"Couldn't open File %s\n",file_name);
                    exit(1);
                     }
          char buffer[BUF];
           printf("\n========================writing (:wq!) for exit ======================\n");
          while((fgets(buffer,BUF,stdin))!=NULL)
          {
          if(strstr(buffer,":wq!"))
          {
          exit(0);
          }
                                                                        
          fputs(buffer,fp);
          }
          fclose(fp);
          }
     
     
     void help(char *string)
    {
         fprintf(stderr,"\nfor reading %s -r filename\nfor writing %s -w file_name\nfor help  %s -h\n(only one operation at one)\n",string,string,string);
         exit(0);
         }
    as you are generous so i am here only for taking advices from you
    thanks in advance

  2. #2
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    and for indentation i have nothing to do "i am missing you vim "
    sorry

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    Work on your code indentation and line length.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72
    How about doing the fopen part like

    Code:
    if ( (fp = fopen("file_name", "w")) == NULL) {
          fprintf(stderr, "Couldn't open &#37;s for writing\n", file_name);
          exit(1);
    }

    Also, in the switch() statement after "case 'w':" there's
    no "break" statement.
    Last edited by movl0x1; 05-18-2007 at 01:35 AM.

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    OOh my eyes! Also there is no default case statement within the switch as a possible error catch
    I'm just trying to be a better person - My Name Is Earl

  6. #6
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72
    Also, you've defined BUF as 250 and don't use it
    in the fgets()

    should be:

    Code:
       fgets(buffer, BUF, fp)
    And if there is only 1 statement in a while loop, you
    don't need braces.
    Last edited by movl0x1; 05-18-2007 at 02:17 AM.

  7. #7
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    Quote Originally Posted by Salem View Post
    Work on your code indentation and line length.
    i will believe me
    tanks
    ================================================== =========

    Quote Originally Posted by movl0x1 View Post
    How about doing the fopen part like



    Code:
    if ( (fp = fopen("file_name", "w")) == NULL) {
          fprintf(stderr, "Couldn't open %s for writing\n", file_name);
          exit(1);
    }

    Also, in the switch() statement after "case 'w':" there's
    no "break" statement.
    actually i forget to do this thanks allot
    ================================================== =====
    OOh my eyes! Also there is no default case statement within the switch as a possible error catch
    hehehehe sorry about your eyes
    yeah but don't you think getopt handling this already!
    ================================================== ======
    Also, you've defined BUF as 250 and don't use it
    in the fgets()

    should be:



    Code:
     fgets(buffer, BUF, fp)
    And if there is only 1 statement in a while loop, you
    don't need braces.
    i forget thanks about braces it's kind of comfortable to me but ok i will take this advice
    thanks all

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    I'm all smilied out for the week now ...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM
  4. HowTo (give your cat a pill)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 01-21-2004, 03:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21