Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 制作了一个编辑行的程序_C_File - Fatal编程技术网

C 制作了一个编辑行的程序

C 制作了一个编辑行的程序,c,file,C,File,我已经设法制作了一个程序,可以从一个文件中编辑一行表格。 但问题是,每当我输入要编辑的最后一行号时,我的程序就会崩溃 这是文本文件,我用来编辑 ***** INVENTORY ***** ------------------------------------------------------------------------- S.N. | NAME | CODE | QUA

我已经设法制作了一个程序,可以从一个文件中编辑一行表格。 但问题是,每当我输入要编辑的最后一行号时,我的程序就会崩溃

这是文本文件,我用来编辑

                          ***** 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;
}

问题解决了!最后一行没有“\n”,这就是我必须使用&&和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);
    }

我建议生成一个仍然显示错误行为的最小程序。这会让别人更容易帮助你。你当然不应该打电话给fflushstdin。为什么我不应该打电话给fflushstdin?我还能做什么?我是一个菜鸟。。。只需开始学习编程几天:为了让我们回答这个问题,你必须告诉我们你到底希望FlushStdin做什么。按enter键进行面向行的输入,使用像getline这样的面向行的输入例程,你的生活会轻松得多。与FGET相比,它有许多优点。主要是1为正在读取的行动态分配内存的能力,2返回读取的字符数。很高兴你的问题解决了。