[英]What should I add to this C code to open more than 1 file?
This code searches for a set of numbers in a file; 此代码在文件中搜索一组数字; if it finds it, it displays the line where it found it. 如果找到它,则显示找到它的行。 I want this code to search into more than 1 file. 我希望此代码搜索到多个文件中。 After it finished searching in the 1st file, it'll start searching in the second one and so on. 在第一个文件中完成搜索后,它将在第二个文件中开始搜索,依此类推。
This is the output: 这是输出:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int busca(char *str, char *archNom);
void archivo(char *nomArchivo);
int main(int argc, char *argv[])
{
int result, errno;
if(argc < 3 || argc > 3)
{
archivo(argv[0]);
exit(1);
}
result = busca(argv[1], argv[2]);
You could iterate over argv
, from 1
to argc - 2
(excluding the first and last elements), and then just run busca(argv[n], argv[argc - 1])
once for each item in the argv
array. 您可以遍历argv
,从1
到argc - 2
(不包括第一个和最后一个元素),然后为argv
数组中的每个项目运行一次busca(argv[n], argv[argc - 1])
一次。 You'd have to keep track of the result to determine wether an error occurred, perhaps using min()
or max()
. 您可能必须使用min()
或max()
来跟踪结果以确定是否发生了错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.