[英]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系统调用read
, write
, open
等。我可以打开,读取,写入文件并将其内容复制到输出文件。 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. 谢谢。
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
, instead of 0
(ie, j=0
is incorrect). 从1
而不是0
开始循环(即j=0
不正确)。 Think about the algorithm before writing the code. 在编写代码之前,请先考虑一下算法。
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?) (当文件丢失,不可读,文件系统损坏或计算机内存或磁盘空间不足时会发生什么?)
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." “读取命令行上给出的前两个文件并将它们写入第三个文件”与“将命令行上给出的所有文件追加到给出的最后一个文件”之间是有区别的。
The algorithm: 算法:
You will notice a lot of redundancy at this point. 此时,您会发现很多冗余。
This algorithm is a bit more challenging, but removes the redundancy. 该算法更具挑战性,但消除了冗余。
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
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.