Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3613488
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:07:51+00:00 2026-05-18T22:07:51+00:00

Hey guys using POSIX API system calls read , write , open , etc.

  • 0

Hey guys using POSIX API system calls read, write, open, etc. I can open, read, write to a file and copy its contents to an output file. How would I go about copying more than one file to an output file using related system calls only?

I currently have:

filein = open(argv[1],O_RDONLY,0);

to open one file.(which is argv1 but I’d like to know how to do argv2 and argv3 etc.)

I tried :

j=0;
filein = open(argv[j],O_RDONLY,0);

but that prints out contents of argv0 into my outputfile.

I am stuck on the next stage to do more than one file. (I also have an EOF loop so after 1 file it exits-How would I make this continue for the next file).

Please could you help me with how to approach the next stage? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T22:07:51+00:00Added an answer on May 18, 2026 at 10:07 pm

    Background

    argv[0] is the name of the program.

    argv[1] is the 1st command line parameter.

    argv[2] is the 2nd command line parameter.

    etc.

    So:

    1. Start your loop at 1, instead of 0 (i.e., j=0 is incorrect).
    2. Be sure to close the file immediately after reading it and before opening the next file.

    Algorithm

    Think about the algorithm before writing the code.

    1. Set counter to the index of the first argument.
    2. Open the file.
    3. Assign a handle to the open file.
    4. Read the file contents.
    5. Write (if required) the file contents.
    6. Close the file using the handle.
    7. Increment the counter.
    8. Loop until there are no more command line arguments.

    Now you can write the code.

    You might get bonus points if you include error handling. (What happens when the file is missing, is not readable, the file system is corrupt, or the machine has run out of memory or disk space?)

    Concatenating Files

    If you want to concatenate two file names to a third, you need to rethink the algorithm, and what you need. There is a difference between “read the first two files given on the command line and write them to the third file” and “append all the files given on the command line to the last file given.”

    Read Two, Write One

    The algorithm:

    1. Make sure that there are exactly three parameters.
    2. Create a file handle variable for the third file (output).
    3. Create a file handle variable for the first file (input).
    4. Create a file handle variable for the second file (input).
    5. Open the first file for reading.
    6. Open the second file for reading.
    7. Open the third file for writing.
    8. Read the contents of the first file and write them to the third file.
    9. Read the contents of the second file and write them to the third file.
    10. Close the third file.
    11. Close the second file.
    12. Close the first file.

    You will notice a lot of redundancy at this point.

    Read N, Write One

    This algorithm is a bit more challenging, but removes the redundancy.

    1. Make sure there are at least two parameters.
    2. Open the last file for writing.
    3. Loop over every file name up to, but not including, the last file name given:
      1. Open the input file for reading.
      2. Write the contents of the file to the last file.
      3. Close the input file.
    4. Close the output file.

    For this you will need to understand argc and its relationship with argv. In pseudo-code:

    if number_of_arguments < 2 then
      print "This program concatenates files; two or more file names are required."
      exit
    end
    
    int outfile = open arguments[ number_of_arguments ] for writing
    int j = 1
    
    while j < number_of_arguments do
      int infile = open arguments[ j ] for reading
      string contents = read infile
      write contents to outfile
      close infile
      increment j
    end
    
    close outfile
    

    Tutorials

    If you are having trouble with C syntax, search for tutorials. For example:

    • http://www.cprogramming.com/tutorial/c/lesson3.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys i'm using this plugin from Hawkee. Its like twitter where you can
Hey guys, I'm using Twitter's PHP API, called twitterlibphp , and it works well,
Hey guys, first off all sorry, i can't login using my yahoo provider. anyways
Hey guys I am using open id authentication on my website and after authenticating
Hey guys I'm using python and I was just wondering how we can make
Hey guys I am trying to manipulate a word .docx file using the openXML
hey guys, m using an api of Bits on the Run following is the
Hey guys, I'm wondering can someone help me out. I'm using javascript to serialize
Hey guys, first time using stackoverflow. can you guys help me debug? Heres the
Hey Guys I am in the process of learning JQuery and I started using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.