尝试使用当前的C I/O。我有一个文件,该文件只保存整数且有每行只有一个..不是逗号,等..什么是阅读的最佳途径:C:I/O - 从文件中读取int的最快/最佳方式
我创建2个文件,这是工作的罚款。但最终,我想将它们都读入,并将它们合并到一个集合中,对它们进行排序,然后将它们打印到一个新文件中。有没有必要为你做的一切,对我来说,但请上述帮助..这是我的努力迄今:
void simpleCopyInputToOutput(void);
void getSortSave(void);
int main()
{
//simpleCopyInputToOutput();
getSortSave();
system("PAUSE");
return 0;
}
void getSortSave(void)
{
FILE *fp1;
FILE *fp2;
FILE *fpMerged;
printf("Welcome. You need to input 2 sets of numbers.\n");
printf("Please input the first sequence. Press 0 to stop.\n");
if ((fp1 = fopen("C:\\seq1.txt", "w")) == NULL)
{
printf("Cannot open or create first file!\n");
exit(1);
}
int num;
int i = 1;
while (num != 0)
{
printf("Please input value # %d\n", i);
scanf("%d", &num);
if (num == 0)
{
break;
}
fprintf(fp1, "%d\n", num);
i++;
}
printf("Please input the second sequence. Press 0 to stop.\n");
if ((fp2 = fopen("C:\\seq2.txt", "w")) == NULL)
{
printf("Cannot open or create second file!\n");
exit(1);
}
num = -1;
i = 1;
while (num != 0)
{
printf("Please input value # %d\n", i);
scanf("%d", &num);
if (num == 0)
{
break;
}
fprintf(fp2, "%d\n", num);
i++;
}
fclose(fp1);
fclose(fp2);
if ((fp1 = fopen("C:\\seq1.txt", "r")) == NULL)
{
printf("Cannot open first file!\n");
exit(1);
}
//WHILE NOT EOF
// Read a number
// Add to collection
//TODO: merge ints from both files, sort and output to new file
}
你试过'fscanf()'吗? – YePhIcK 2012-07-18 08:44:30
我可能会使用fscanf(“%d”,#)..类似的东西..但我想我真的想知道是否要搬到下一行..请别人给我我需要的while循环与EOF检查或任何正确的方式来做到这一点在旧式的C ..我猜你只需要给我一个3或4班轮.. :) – Matt 2012-07-18 08:46:30