简体   繁体   English

在C中使用两个文件

[英]Working with two files in C

I am reading a file named "text.txt" that contains a series of numbers with a space in between. 我正在读取一个名为“ text.txt”的文件,该文件包含一系列数字,两者之间有一个空格。 I want to write in a new file named "text_nr.txt", all the numbers in "text.txt" but without repetition! 我想在一个名为“ text_nr.txt”的新文件中写入“ text.txt”中的所有数字,但不要重复! My code runs in an infinite loop at the second while loop, but I can't understand why. 我的代码在第二个while循环中无限循环运行,但是我不明白为什么。

#include <stdio.h>
#include <stdlib.h>


int main()
{
    int n=0,m=0,nr;

    FILE *fp1;
    FILE *fp2;
    fp1 = fopen("text.txt","r");
    fp2 = fopen("text_nr.txt","a+");

    while(fscanf(fp1,"%d",&n) != EOF){
        nr=0;
        while(fscanf(fp2,"%d",&m) != EOF){
            printf("%d  %d ",m,n);
            if(n != m) continue;
            else{
                nr++;
                break;
            }
        }   
        if(nr == 0){
            fprintf(fp2,"%d ",n);
        }
    }

    fclose(fp1);
    fclose(fp2);

    return 0;      
}

Your inner loop will not reach the end of file since it won't match your search string if the file starts empty. 您的内部循环将不会到达文件末尾,因为如果文件开始为空,则它将与您的搜索字符串不匹配。

You could do the fscanf and check if it's 0 (couldn't find anything) or EOF (hit the end). 您可以执行fscanf并检查它是否为0 (找不到任何内容)或EOF (结束)。

Functions like fscanf() return various values like EOF (end of file or error ) or the number of specifiers matched, 0 , 1 , ... 类似功能fscanf()返回等各种值EOF (文件或错误的端部)或匹配符的数量, 01 ,...

Rather than test for one value code needs to stop on, test against all undesired values. 无需停止测试一个值代码,而是针对所有不需要的值进行测试。

// while(fscanf(fp1,"%d",&n) != EOF){
while(fscanf(fp1,"%d",&n) == 1){
    nr=0;
    // while(fscanf(fp2,"%d",&m) != EOF){
    while(fscanf(fp2,"%d",&m) == 1){

@William Pursell explained it well @威廉·珀塞尔很好地解释了

At some point in the input, the format to fscanf doesn't match, so fscanf returns 0 but does not advance the file pointer. 在输入中的某些时候,fscanf的格式不匹配,因此fscanf返回0,但不前进文件指针。 So it keeps reading from the same spot and returns 0 each time. 因此,它始终从同一位置读取数据,并且每次返回0。

Infinite loop. 无限循环。

Try to use fseek function to move stream pointer inside output file. 尝试使用fseek函数在输出文件中移动流指针。

 while(fscanf(fp1,"%d",&n) != EOF){

    fseek ( fp2, 0 , SEEK_SET );
    bool flag = false;

    while(fscanf_s(fp2,"%d",&m) != EOF){
        printf("%d  %d ",m,n);

        if(n == m) 
        {
            printf("%d = %d ", m, n);
            flag = true;


            break;
        }
    }   
    if(!flag)
    {
        fseek ( fp2, 0 , SEEK_END );
        fprintf(fp2,"%d ",n);
    }
}

EDIT 编辑

Another method is to store written values in array. 另一种方法是将写入的值存储在数组中。 For this you can use vector. 为此,您可以使用向量。

vector<int> vData;

while(fscanf(fp1,"%d",&n) != EOF){


    bool flag = false;

    for(int i = 0; i < vData.size(); i++)
    {
        if(vData[i] == n)
        {
            flag = true;
            break;
        }
    }

    if(!flag)
    {
        vData.push(n);
        fprintf(fp2,"%d ",n);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM