嘗試使用當前的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