Thread: Loading multiple files

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2

    Loading multiple files

    I need to be able to prompt the user to enter files that they want linked in a program, (i.e enter the files to be linked: file1.c file2.c file3.c) all on one line and separated by a space, I know how to load one file, but not multiple files, can anybody help?

  2. #2
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    take the file names at the command prompt.
    argv[0] is the name of the file that u r executing,
    argv[1] is the name of the first file that u will bw working on,
    argv[2] is the name of the second file that u will bw working on and so on...
    the total number of arguments that u have entered at the command prompt is know, i.e., the it is stored in argc..

    now,

    Code:
     int i;
     FILE *fp;
     for(i=1;i<=argc;i++)
      {
         fp=fopen(argv[i],"r");
         / * 
            whatever u want to do with hte file 
        */
        fclose(fp);
      }
    I suppose this is what u were looking for.
    In the middle of difficulty, lies opportunity

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2

    loading multiple files

    looking at what you wrote, it seems to be what i'm looking for, and i thank you for that, but, just to be sure, argv[0] is the file i'm executing, [1] is the file i'll be working on. also will the file names automatically be assigned to the arguments, because, the user needs to enter all the file names at once, it's not enter a file name one at a time, it's all on one command line, on a unix system

  4. #4
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    > argv[0] is the file i'm executing,
    yes, u print its contents, u get " ./a.out "
    >will the file names automatically be assigned to the arguments, because, the user needs to enter all the file names at once, it's not enter a file name one at a time, it's all on one command line, on a unix system
    yes, u have enter all the names at once, argv is the array of char pointers, so each element in that array points to the strings that u have entered at the prompt. U dont have to worry about the mapping part.
    In the middle of difficulty, lies opportunity

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > for(i=1;i<=argc;i++)
    Should be < argc
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with loading files into rich text box
    By blueparukia in forum C# Programming
    Replies: 3
    Last Post: 10-19-2007, 12:59 AM
  2. Windows shell commands - multiple files
    By Magos in forum Tech Board
    Replies: 3
    Last Post: 02-28-2006, 01:56 AM
  3. Opening Multiple Files in sequence
    By wujiajun in forum C++ Programming
    Replies: 7
    Last Post: 01-16-2006, 08:47 PM
  4. copy multiple files to a destination file
    By Bones in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2003, 10:47 AM
  5. opening multiple files sequentially
    By moonwalker in forum C Programming
    Replies: 5
    Last Post: 08-20-2002, 09:57 PM