2012-07-18 68 views
0

尝试使用当前的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 
} 
+0

你试过'fscanf()'吗? – YePhIcK 2012-07-18 08:44:30

+0

我可能会使用fscanf(“%d”,#)..类似的东西..但我想我真的想知道是否要搬到下一行..请别人给我我需要的while循环与EOF检查或任何正确的方式来做到这一点在旧式的C ..我猜你只需要给我一个3或4班轮.. :) – Matt 2012-07-18 08:46:30

回答

1

我会建议你使用fgets

char buffer[16]; 
while (fgets(buffer, sizeof(buffer), fp1)) 
{ 
    long value = strtol(buffer, NULL, 10); 

    /* Use the value... */ 
} 

/* fgets failed ro read, check why */ 
if (!feof(fp1)) 
    printf("Error: %s\n", strerror(errno)); 

编辑:如何获取文件中的条目数量:如果不以任何其他方式跟踪它(例如,将条目数量作为第一行),唯一的解决方案可能是将文件读取两次。一次来计算行数,一次来读取实际的数字。计数后使用fseekrewind将读指针“倒回”到文件的开头。

我会亲自把计数放在一个单独的函数中,也是实际的读数。这样,如果你想从多个文件中读取,你不必重复代码。

+0

这似乎很好,谢谢。但问题的一部分也是..如何使一个未知长度的int数组?在C#中,我只是使用一个列表或类似的东西..在这种情况下,我在这里做什么?你会被标记为答案,谢谢..但请帮助这部分太..因为它是相关的.. – Matt 2012-07-18 08:59:16

+0

非常感谢! – Matt 2012-07-18 09:03:52

+0

@Matt用一种方法来计算文件中的行数,从而更新了我的答案。 – 2012-07-18 09:04:31

1

您的问题可以分为三个不同的部分:读取两个文件,排序数据,并将输出写入文件。我在这里假设这两个输入文件尚未排序。如果是的话,这个问题将会大大简化(如果是这种情况,google for mergesort)。

如果要打开文件进行阅读,则必须使用"r"而不是"w"作为文件打开模式标志。在你的示例代码中,读/写部分与上面描述的相反。然后,您应该使用fscanf从FILE *中读取格式化的输入。 scanf(...)只是fscanf(stdin, ...)的简称。您可以访问这些文件以下列方式:

FILE *fin1 = fopen("seq1.txt", "r"); 
FILE *fin2 = fopen("seq2.txt", "r"); 
FILE *fout = fopen("out.txt", "w"); 

if (fin1 && fin2 && fout) { 
    // Do whatever needs to be done with the files. 
} 
if (fout) 
    fclose(fout); 
if (fin2) 
    fclose(fin2); 
if (fin1) 
    fclose(fin1); 

使用动态内存来存储整数是困难的。当你写入越来越多的数据时,你需要使用realloc来增长缓冲区,最后使用qsort对数据进行排序。如果需要,其他人可以希望更深入地了解这一点。

+0

欣赏信息。谢谢。 – Matt 2012-07-18 13:37:35

相关问题