gpt4 book ai didi

c - 读取 int。 file(txt) 从命令行并创建偶数奇数文件

转载 作者:行者123 更新时间:2023-11-30 20:36:32 24 4
gpt4 key购买 nike

写了这段代码不知道为什么它不起作用

#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;
}

最佳答案

修改以下行以与整数进行比较。

if (num % 2 == '0') // This is wrong, you are comparing `int` with `character`
{
fprintf (file2,"%d",nums);
}

将其更改为;

if (num % 2 == 0) // This is wrong, you are comparing `int` with `character`
{
fprintf (file2,"%d",nums);
}

希望这有帮助。

关于c - 读取 int。 file(txt) 从命令行并创建偶数奇数文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35850647/

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