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

  • Home
  • SEARCH
  • 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 3496586
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:16:02+00:00 2026-05-18T12:16:02+00:00

The user should input some file names in the command line and the program

  • 0

The user should input some file names in the command line and the program will read each file name from argv[] array. I have to perform error checking etc.

I want to read each filename. For example, if argv[2] is ‘myfile.txt’, the program should read the content of ‘myfile.txt’ and store value in char buffer[BUFSIZ] and then write the content of buffer into another file.

However before the content is written, the program should also write the name of the file and the size. Such that the file can be easily extracted later. A bit like the tar function.

The file I write the content of buffer, depending on the number of files added by user, should be a string like:

myfile.txt256Thisisfilecontentmyfile2.txt156Thisisfile2content..............

My question is

1) How do I write value of argv[2] into file using write() statement, as having problems writing char array, what should I put as (sizeof(?)) inside write(). see below as I don’t know the length of the file name entered by the user.

2) Do I use the ‘&’ to write an integer value into file after name, for example write 4 bytes after file name for the size of file

Here is the code I have written,

char buffer[BUFSIZ];
int numfiles=5; //say this is no of files user entered at command
open(file.....

lseek(fdout, 0, SEEK_SET); //start begging of file and move along each file some for loop

for(i=0-; ......
//for each file write filename,filesize,data....filename,filesize,data......
int bytesread=read(argv[i],buffer,sizeof(buffer));
write(outputfile, argv[i], sizeof(argv)); //write filename size of enough to store value of filename
write(outputfile, &bytesread, sizeof(bytesread));
write(outputfile, buffer, sizeof(buffer));

But the code is not working as I expected.

Any suggestions?

  • 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-18T12:16:02+00:00Added an answer on May 18, 2026 at 12:16 pm

    Since argv consists of null-terminated arrays, the length you can write is strlen(argv[2])+1 to write both the argument and null terminator:

    size_t sz = strlen (argv[2]);
    write (fd, argv[2], sz + 1);
    

    Alternatively, if you want the length followed by the characters, you can write the size_t itself returned from strlen followed by that many characters.

    size_t sz = strlen (argv[2]);
    write (fd, &sz, sizeof (size_t));
    write (fd, argv[2], sz);
    

    You probably also need to write the length of the file as well so that you can locate the next file when reading it back.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Should I use the change or textInput event to capture user input on a
In Model-View-Presenter pattern where should we write validations of user input.
What are the dangerous characters that should be replaced in user input when the
[EDIT] Hmm. Perhaps this question should be titled what is the default user-input dialog
Simple question that keeps bugging me. Should I HTML encode user input right away
I'm currently working on a program that has many of those the user SHOULD
Theoretically, the end user should never see internal errors. But in practice, theory and
I'm a new Windows programmer and I'm not sure where I should store user
I know that most links should be left up to the end-user to decide
In a Swing app a method should continue only after user enters a correct

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.