Now the problem with file

This is a discussion on Now the problem with file within the C Programming forums, part of the General Programming Boards category; I want to read file file as command line operator But when i declare the args in main() function it ...

  1. #1
    Registered User planet_abhi's Avatar
    Join Date
    Oct 2002
    Posts
    92

    Now the problem with file

    I want to read file file as command line operator
    But when i declare the args in main() function it returns error
    the code is like the
    Code:
    # include<stdio.h>
    void main(char *args[])
    {
    FILE *fp,*fp1;
    fp=fopen(args[1],"w");
    fprintf (fp,"Hello i love c");
    fclose (fp);
    }
    It return the error Null pointer assignment.
    What is the problem?
    AbHHinaay

  2. #2
    anonytmouse
    Guest
    Code:
    int main( int argc, char* argv[] ) {
    argc being the number of arguments including the program name.

    Have Fun!

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Also check the number of arguments and check if the fopen function failed.

    Code:
    #include<stdio.h>
    
    int main(int argc, char *argv[])
    {
       FILE *fp,*fp1;
    
       if(argc != 2) /* first argument (argv[0]) is the name of the application */
       {
          printf("Usage: %s <filename>\n", argv[0]);
          return -1;
       }
    
       fp=fopen(argv[1],"w");
    
       if(fp == NULL)
       {
          perror(argv[1]);
          return -2;
       }
    
       fprintf (fp,"Hello i love c");
       fclose (fp);
       return 0;
    }
    Cheers,
    Monster

  4. #4
    Registered User planet_abhi's Avatar
    Join Date
    Oct 2002
    Posts
    92
    Now i want 2 copy the content of first file to another file from command line operator but its do nothing ,
    actually i just block when i need to write code for copy
    here is my code
    [code]
    # include<stdio.h>
    void main(int argc,char *argv[])
    {
    FILE *fp1,*fp2;
    //char args[1];
    char info[];
    int i;
    fp1=fopen(argv[1],"w");
    fprintf(fp1,"You just enteredHello my name is abhinay");
    fclose(fp1);
    fp1=fopen(argv[1],"r");
    fp2=fopen(argv[2],"r");
    while(!EOF(fp1))
    {

    fprintf(fp2,fp1);
    }
    }
    [\code]
    what 'll i do?
    AbHHinaay

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    You can use the fgets function the read from file 1 and the fputs function to write to file 2.
    Code:
    char buffer[BUFSIZ];
    ...
    while(fgets(buffer, BUFSIZ, fp1) != NULL)
       fputs(buffer, fp2);
    Or just read/write blocks of data (fread/fwrite)

    Use &#91;/CODE] in stead of [\CODE] (and indent your code please)

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    @planet_abhi

    Read my signature about code tag usage.

    >>void main(int argc,char *argv[])
    You've beed told already It's int main, not void main.

    >>while(!EOF(fp1))
    Eh? What's that then?

    >>copy a file
    To do a character by character copy try this, or for block copying, try fread/frwite as already suggested.
    Code:
    int c;
    while ((c = fgetc(fpIN)) != EOF)
       fputc(c, fpOUT);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Rename file problem
    By Emporio in forum C Programming
    Replies: 2
    Last Post: 06-05-2002, 09:36 AM

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