制作了一个编辑线的程序

时间:2014-09-22 22:21:50

标签: c file

我设法创建了一个可以从文件中编辑表格行的程序。 但问题是,每当我输入要编辑的最后一个行号时,它都会使我的程序崩溃......

这是我用来编辑的文本文件....

                          ***** INVENTORY ***** 
-------------------------------------------------------------------------
 S.N. |        NAME         |        CODE         | QUANTITY |   PRICE   
-------------------------------------------------------------------------
    1   25                    45                    54             54.00  
    2   fkfgyl                55dtylk2d             52             54.25  
    2   fkfgyl                55dtylk2d             52             54.25  
    2   fkfgyl                55dtylk2d             52             54.25  
    5   kjbx                  zkjhvkz               45             45.00  
    2   fkfgyl                55dtylk2d             52             54.25  
    2   fkfgyl                55dtylk2d             52             54.25  
    2   fkfgyl                55dtylk2d             52             54.25  
    2   fkfgyl                55dtylk2d             52             54.25  
    2   kj                    5j;                   2               5.00  
    2   fkfgyl                55dtylk2d             52             54.25 

看!每当我输入11(表示第15行)时,程序就会冻结!

这是代码:

#include <stdio.h>

FILE *fileptr1, *fileptr2;

int adddata(i);

int main(void)
{
    char c;

    int delete_line, temp = 1;

    fileptr1 = fopen("inventory.txt", "r");

    c = getc(fileptr1);

    //print the contents of file .

    while (c != EOF)

    {

        printf("%c", c);

        c = getc(fileptr1);

    }

    printf("\n\n\n Enter S.N. to be deleted and replaced ");

    scanf("%d", &delete_line);

    delete_line = delete_line+4;

    //take fileptr1 to start point.

    rewind(fileptr1);

    //open replica.c in write mode

    fileptr2 = fopen("replica.c", "w");


    c = getc(fileptr1);

    while (c != EOF)

    {

        if (c == '\n')

        {

            temp++;

        }

        //till the line to be deleted comes,copy the content to other

        if (temp != delete_line)

        {

            putc(c, fileptr2);

        }

        else

        {
            while ((c = getc(fileptr1)) != '\n')

            {

            }

            fclose(fileptr2);

            adddata(delete_line);

            temp++;

            fileptr2 = fopen("replica.c", "a");

            fflush(stdin);

            putc('\n', fileptr2);


        }

        //continue this till EOF is encountered

        c = getc(fileptr1);

    }

    fclose(fileptr1);

    fclose(fileptr2);

    remove("inventory.txt");

    rename("replica.c", "inventory.txt");

    fileptr1 = fopen("inventory.txt", "r");

    //reads the character from file

    c = getc(fileptr1);

    //until last character of file is encountered

    while (c != EOF)

    {

        printf("%c", c);

        //all characters are printed

        c = getc(fileptr1);

    }

    fclose(fileptr1);

    return 0;

}


int adddata(i) {

    fileptr2 = fopen("replica.c", "a");

    struct details
    {
        char name[20];
        char code[20];
        float price;
        long int quantity;
    };

    struct details item[1];

    fflush(stdin);

    printf("\nItem name: ");
    scanf("%s", &item[0].name);
    item[0].name[19] = '\0';
    fflush(stdin);

    printf("\nItem code: ");
    scanf("%s", &item[0].code);
    item[0].code[19] = '\0';
    fflush(stdin);

    printf("\nItem price: ");
    scanf("%f", &item[0].price);
    fflush(stdin);

    printf("\nItem quantity: ");
    scanf("%8ld", &item[0].quantity);
    fflush(stdin);
    fprintf(fileptr2, "\n  %3d   %-20s  %-20s  %-8ld   %9.2f  ", i-4, item[0].name, item[0].code, item[0].quantity, item[0].price);

    fclose(fileptr2);
    return 0;
}

1 个答案:

答案 0 :(得分:0)

问题解决了!最后一行没有'\ n',这就是为什么我必须使用&amp;&amp;和EOF 问题出在这个段落中。

    //till the line to be deleted comes,copy the content to other

    if (temp != delete_line)

    {

        putc(c, fileptr2);

    }

    else

    {

        while ((c = getc(fileptr1)) != '\n' && (c = getc(fileptr1)) != EOF)
        {

        }
            fclose(fileptr2);

            adddata(delete_line);

            temp++;

            fileptr2 = fopen("replica.c", "a");

            fflush(stdin);

            putc('\n', fileptr2);
    }