./practice 10 13
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
FILE* newfile;
int i = argc;
while (i<arvf[]) */im pretty sure i<arvf doesnt work but how do i capture
the range inputed by the user,and open each one?*/
{
newfile = fopen(("file05-data-%d.txt",i) "r")
i = i + 1
}
我对如何获取用户输入以及打开用户输入范围内的文件感到困惑。任何帮助将不胜感激。
答案 0 :(得分:0)
E.g
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[]){
FILE *file;
if(argc != 3){
fprintf(stderr, "Usage >practice start_num end_num\n");
return 1;
}
int start = atoi(argv[1]);
int end = atoi(argv[2]);
if(!(1 <= start && start <= 99 && 1<= end && end <= 99 && start <= end)){
fprintf(stderr, "Specifying the range of values must be 1-99.\n");
return 2;
}
char filename[FILENAME_MAX];
int no;
for(no = start ; no <= end ; ++no){
snprintf(filename, sizeof(filename), "file05-data-%d.txt", no);
if(NULL==(file=fopen(filename, "r"))){
fprintf(stderr, "%s can't open.\n", filename);
//return 3;
continue;
}
/* input && output
char line[128];
while(fgets(line, sizeof(line), file)){
printf("%s", line);
}
*/
fclose(file);
}
return 0;
}
答案 1 :(得分:0)
./practice 10 13
这很好,但是你对argc和argv的使用是不正确的。我在这里假设您正在尝试打开#10,11,12,13文件。
第一次检查你是否正在运行带有正确输入的二进制文件(我认为你要求两个参数):
if(argc!=3)
{
// print error msg & exit
}
建立文件名:
char newfile[MAX_SIZE];
for(i=atoi(argv[1]); i<atoi(argv[2]); i++)
{
snprintf(newfile, "file-data-%d", i);
//open this file, read data, and then loop to the next file
}
就是这样!