写了这个代码不知道为什么它不工作读取整数。在命令行文件(TXT),并偶奇文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char *argv[]){
int i=1;
//this is where i started loop read file.
for (i=1; i<argc;i++){
FILE *file1 = fopen("argv[i]","r");//reading file
FILE *file2 = fopen("even.txt","w");//making even file
FILE *file3 = fopen("odd.txt","w");//odd file
//These are the files i am reading and writing to.
int nums;
Main looping
while (file1 != EOF)
{
fscanf (file1,"%d",&nums);
nums++;
//adding the conditions to what i want each file to have.
if (num % 2 == '0'){
fprintf (file2,"%d",nums);
}
//if condition fails move the numbers to the Odd file.
else {
fprintf (file3,"%d",nums);
}
//I tried the loops here but ut gave me segment error.
}
//closing all files
fclose (file1);
fclose (file2);
fclose (file3);
}
return 0;
}
'fopen(“argv [i]”,“r”)'尝试打开一个名为'argv [i]'的文件。你想做'fopen(argv [i],“r”)'而不用引号。 –
'FILE *'不能与'EOF'相比较 –
条件'file1!= EOF'不应该是错误的。如果'fopen'失败则返回'NULL','EOF'等于'-1'。 –