Thread: Open & pass data onto a file on cmd line

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    4

    Unhappy Open & pass data onto a file on cmd line

    Hi I been reading lots on how to use argc and argv, and have managed to get some data be passed on argv
    argv[1] = 234
    argv[2] = 323 etc, now I have a problem, I want all of this data to be passed onto a file but really dont know how to.. First of all how to allocate the text file where this data is gonna be stored from my program and then I'm not sure if by just doing an ./a.out filename.c 234 323 is going to pass the data straight onto the file.. Please someone explain this to me..

    This is the code Im working on im sure its totally wrong, but hey, you dont learn unless you try.

    "Only those who will risk going too far can possibly find out how far one can go." -- TS Eliot.

    Code:
    #include <stdio.h>  //Allows input and output of program
    #include <io.h>     //Checks for existence of file
    
    int main(int argc, char* argv[])
    
    {
    
    fp = fopen(argv[i], "r");
    fpo = fopen(argv[i], "w");
    
       char* filename = NULL;     
    
       if( argc == 2)
           filename = argv[1];
       else
       {
           printf("Error\n");
           return 1;
       }
    
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    1. argc is the number or arguments on the command line, including the name of the program. So, if you type "./a.out filename.c 234 323 <Enter key>" the value of argc will be 4. The test for argc in your program will be wrong. Instead of testing for 2 it should test for > 1
    Code:
       if( argc > 1)
           filename = argv[1];
    2. The same file can not normally be opened twice -- if the first fopen() succeeds then the second is probably going to fail. If you want the file for both reading and writing then open it once and specify "r+", "w+", or "a+", depending on how you want it opened. see man pages or msdn for description of these flags.


    Code:
    #include <stdio.h>  //Allows input and output of program
    #include <io.h>     //Checks for existence of file
    
    int main(int argc, char* argv[])
    
    {
       FILE* fp;
       int i;
       if( argc < 2)
       {
           printf("Error\n");
           return 1;
       }
       if( (fp = fopen(argv[1],"w")) == NULL)
       {
            printf("can't open file %s\n", argv[1]);
            return 1;
       }
       // write out each of the remaining arguments
       for(i = 2; i < argc; ++i)
         fprintf(fp, "%s\n", argv[i]);
       fclose(fp);
       return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM
  5. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 06:04 AM