是否可以从一个文件中复制搜索到的字符串并打印到另一个文件中?

时间:2017-04-03 04:23:55

标签: c

以下功能仅用于显示文件的某些区域。在这种情况下,我试图复制每一行都有" EMPTY"从主文件到另一个文件。除了通过打开要写入的文件,我使用char数组来查找所需的正确字符串。此时,我尝试使用该文件中的数据(在其中搜索字符串)并尝试将其复制到另一个文件中。此时,一些新文件打印到屏幕上,这是我创建文件的顶部的第一部分,但是当我尝试从其他文件添加字符串时,它们不打印。你能从一个文件到另一个文件打印某些字符串吗?

void emptySeats(void)
    {
        int position = 0;
        int AvalabileOne = 0;
        int countGone = 0;
        int confirm = 0; 
        char testOne[] = "EMPTY";
        char StrOne[10], StrTwo[10], StrThree[10], StrFour[10];

        FILE *fEmptyseatArrangement;
        fEmptyseatArrangement = fopen("airlineEmptySeatingArrangment.txt", "w+");
        fprintf(fseatArrangement, "\t\t\tEcono-Airlines Passangers System\n\n\t   Seat Number\t Seatavalability      Last Name      First Name\n");

        do
        {
            fseatArrangement = fopen("airlineSeatingArrangment.txt", "r");
            fseek(fseatArrangement, 102, SEEK_SET);


            while (AvalabileOne < FULL)
            {
                fscanf(fseatArrangement, "%s%s%s%s\n", &StrOne, &StrTwo, &StrThree, &StrFour);
                if (strcmp(StrThree, testOne))
                {
                    fprintf(fEmptyseatArrangement, "\t\t%s\t\t%s\t\t%s\t\t%s\n", StrOne, StrTwo, StrThree, StrFour);
                    countGone = countGone + 1;
                }
                AvalabileOne = AvalabileOne + 1;
            }
            fclose(fEmptyseatArrangement);
            fclose(fseatArrangement);

            fEmptyseatArrangement = fopen("airlineEmptySeatingArrangment.txt", "r");
            while (fgets(econoAirEmptySeatArrangement, 1000, fEmptyseatArrangement) != NULL)
                printf("%s", econoAirEmptySeatArrangement);
            fclose(fEmptyseatArrangement);
            printf("There are %d seats vacant at the moment\n", FULL - countGone);


            printf("Enter Zero(0) Key to return to menu at anytime.");
            scanf(" %d", &position);
            if (position == 0)
            {
                system("cls");
                menu();
            }
            else
            {
                system("cls");
                printf("INVALID INPUT! Please try again.\n");
            }
        } while (AvalabileOne != 0);

        system("pause");
        return;
    }

0 个答案:

没有答案