C语言下,2个TXT文件一行一个数值,但是其中一个多了几个数值,怎么样才能继续把剩下的输出到output里
#include<stdio.h>#include<stdlib.h>intmain(intargc,char*argv[]){FILE*fp_input1;FILE*f...
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
FILE* fp_input1;
FILE* fp_input2;
FILE* fp_output;
int numbers_of_conversion;
int numbers1;
int numbers2;
fp_input1 = fopen("numbers1.txt", "r");
fp_input2 = fopen("numbers2.txt", "r");
fp_output = fopen("output.txt", "w");
if (fp_input1 == NULL || fp_input2 == NULL || fp_output == NULL)
{
printf("fail to open.");
exit(1);
}
numbers_of_conversion = fscanf(fp_input1, "%d\n", &numbers1);
numbers_of_conversion = fscanf(fp_input2, "%d\n", &numbers2);
while (numbers_of_conversion != EOF)
{
if (numbers_of_conversion == 1)
{
if (numbers1 > numbers2)
{
fprintf(fp_output, "%d\n", numbers2);
numbers_of_conversion = fscanf(fp_input2, "%d", &numbers2);
}
else if (numbers2 > numbers1)
{
fprintf(fp_output, "%d\n", numbers1);
numbers_of_conversion = fscanf(fp_input1, "%d", &numbers1);
}
}
}
fclose(fp_input1, fp_input2, fp_output);
return 0;
}
2个TXT文件一行一个数值,但是其中一个多了几个数值,怎么样才能让程序继续把剩下的输出到output里。
假如我一个txt有5个数值,一个有8个,我的程序就只能输出10个,剩下的3个怎么输出到output里
求助!!! 展开
#include <stdlib.h>
int main(int argc, char* argv[])
{
FILE* fp_input1;
FILE* fp_input2;
FILE* fp_output;
int numbers_of_conversion;
int numbers1;
int numbers2;
fp_input1 = fopen("numbers1.txt", "r");
fp_input2 = fopen("numbers2.txt", "r");
fp_output = fopen("output.txt", "w");
if (fp_input1 == NULL || fp_input2 == NULL || fp_output == NULL)
{
printf("fail to open.");
exit(1);
}
numbers_of_conversion = fscanf(fp_input1, "%d\n", &numbers1);
numbers_of_conversion = fscanf(fp_input2, "%d\n", &numbers2);
while (numbers_of_conversion != EOF)
{
if (numbers_of_conversion == 1)
{
if (numbers1 > numbers2)
{
fprintf(fp_output, "%d\n", numbers2);
numbers_of_conversion = fscanf(fp_input2, "%d", &numbers2);
}
else if (numbers2 > numbers1)
{
fprintf(fp_output, "%d\n", numbers1);
numbers_of_conversion = fscanf(fp_input1, "%d", &numbers1);
}
}
}
fclose(fp_input1, fp_input2, fp_output);
return 0;
}
2个TXT文件一行一个数值,但是其中一个多了几个数值,怎么样才能让程序继续把剩下的输出到output里。
假如我一个txt有5个数值,一个有8个,我的程序就只能输出10个,剩下的3个怎么输出到output里
求助!!! 展开
1个回答
展开全部
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[]) {
FILE* fp_input1;
FILE* fp_input2;
FILE* fp_output;
// int numbers_of_conversion;
int number1;
int number2;
int flag;
fp_input1 = fopen("numbers1.txt", "r");
fp_input2 = fopen("numbers2.txt", "r");
fp_output = fopen("output.txt", "w");
if (fp_input1 == NULL || fp_input2 == NULL || fp_output == NULL) {
printf("fail to open.\n");
exit(1);
}
fscanf(fp_input1,"%d",&number1);
fscanf(fp_input2,"%d",&number2);
while (!feof(fp_input1) && !feof(fp_input2)) {
if (number1 > number2) {
fprintf(fp_output,"%d ",number2);
fscanf(fp_input2,"%d",&number2);
flag = 2;
}
else {
fprintf(fp_output,"%d ",number1);
fscanf(fp_input1,"%d",&number1);
flag = 1;
}
}
while (!feof(fp_input1)) { // 2#文件已经处理完毕
if (flag == 2) { // 最后写入的是2#文件的数据
fprintf(fp_output,"%d ",number1);
flag = 1;
}
fscanf(fp_input1,"%d", &number1);
fprintf(fp_output,"%d ",number1);
}
while (!feof(fp_input2)) { // 1#文件已经处理完毕
if (flag == 1) {
fprintf(fp_output,"%d ",number2);
flag = 2;
}
fscanf(fp_input2,"%d",&number2);
fprintf(fp_output,"%d ",number2);
}
fclose(fp_input1);
fclose(fp_input2);
fclose(fp_output);
return 0;
}
追问
我参考你的代码修改了下我的
但是我让txt1 = 1 3 5 7 9,txt2= 2 4 6 8 10 11 12 13
为什么输出中少了一个10?
是我理解你的代码有问题吗?
主要我不太懂你的!eof()判定,有和我的判定一样的写法吗?
因为我还要考虑如果有个txt里面没有数值的时候!
感谢!!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询