简体   繁体   中英

C: read in more than one file

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.

Background

argv[0] is the name of the program.

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

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

etc.

So:

  1. Start your loop at 1 , instead of 0 (ie, 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:

Use a loop to read all the files. Start at 1 to skip the current executing process which is located at argv[0].

for(int i = 1; i < argc; ++i)
{
    int filein = open(argv[i],O_RDONLY,0);
    // ... process file
    close(filein)
}

argv[0] is the name of the program. argv[1] is the first then you pass on the command line.

Open your output file then each input file. read each input file into the output file then close them all and exit.

打开一个文件。(这是argv1,但我想知道如何做argv2和argv3等。)

fopen(argv[2], ...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM