gpt4 book ai didi

c - C中使用循环读取所有文本文件

转载 作者:太空宇宙 更新时间:2023-11-04 02:42:59 27 4
gpt4 key购买 nike

我知道我们有很多不同的方法来实现一个功能。有人有想法使用循环案例一次读取所有文本文件吗?

int readTenFiles(char (*names)[MAX_LENGTH_NAME], int (*ranks)[YEAR], int namesCount) {
int yearIndex = 0;
namesCount = readOneFile(fopen("yob1920.txt", "r"), names, ranks, yearIndex++, namesCount);
namesCount = readOneFile(fopen("yob1930.txt", "r"), names, ranks, yearIndex++, namesCount);
namesCount = readOneFile(fopen("yob1940.txt", "r"), names, ranks, yearIndex++, namesCount);
namesCount = readOneFile(fopen("yob1950.txt", "r"), names, ranks, yearIndex++, namesCount);
namesCount = readOneFile(fopen("yob1960.txt", "r"), names, ranks, yearIndex++, namesCount);
namesCount = readOneFile(fopen("yob1970.txt", "r"), names, ranks, yearIndex++, namesCount);
namesCount = readOneFile(fopen("yob1980.txt", "r"), names, ranks, yearIndex++, namesCount);
namesCount = readOneFile(fopen("yob1990.txt", "r"), names, ranks, yearIndex++, namesCount);
namesCount = readOneFile(fopen("yob2000.txt", "r"), names, ranks, yearIndex++, namesCount);
return readOneFile(fopen("yob2010.txt", "r"), names, ranks, yearIndex++, namesCount);
}

最佳答案

在一个数组中分配文件名列表,然后在这个数组上循环。

char *filenames[] = { 
"yob1920.txt" ,
"yob1930.txt" ,
"yob1940.txt" ,
"yob1950.txt" ,
"yob1960.txt" ,
"yob1970.txt" ,
"yob1980.txt" ,
"yob1990.txt" ,
"yob2000.txt" ,
"yob2010.txt" ,
};
int i = 0;
for ( i=0; i<sizeof(filenames)/sizeof(*filenames); i++ ) {
namesCount = readOneFile(fopen(filenames[i], "r"), names, ranks, yearIndex++, namesCount);
}

关于c - C中使用循环读取所有文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30006568/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com