简体   繁体   English

C:读入多个文件

[英]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. 大家好,使用POSIX API系统调用readwriteopen等。我可以打开,读取,写入文件并将其内容复制到输出文件。 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.) 打开一个文件。(这是argv1,但我想知道如何做argv2和argv3等。)

I tried : 我试过了 :

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

but that prints out contents of argv0 into my outputfile. 但这会将argv0的​​内容打印到我的输出文件中。

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). (我也有一个EOF循环,因此在1个文件退出后,我将如何继续执行下一个文件)。

Please could you help me with how to approach the next stage? 请您协助我进入下一阶段? Thanks. 谢谢。

Background 背景

argv[0] is the name of the program. argv[0]是程序的名称。

argv[1] is the 1 st command line parameter. argv[1]是第1命令行参数。

argv[2] is the 2 nd command line parameter. argv[2]第二个命令行参数。

etc. 等等

So: 所以:

  1. Start your loop at 1 , instead of 0 (ie, j=0 is incorrect). 1而不是0开始循环(即j=0不正确)。
  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. 将counter设置为第一个参数的索引。
  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 读N,写一个

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 . 为此,您需要了解argc及其与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. 如果您在使用C语法时遇到问题,请搜索教程。 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]. 从1开始,跳过位于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 [0]是程序的名称。 argv[1] is the first then you pass on the command line. argv [1]是第一个,然后您在命令行中传递。

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], ...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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