在C语言中,要在数据库(文件)中存储更多的值,请键入int并调用特定的行

在C语言中,要在数据库(文件)中存储更多的值,请键入int并调用特定的行,c,arrays,database,file,arraylist,C,Arrays,Database,File,Arraylist,在C语言中,如何在文件(数据库)中存储数组以及如何在程序中访问数组。在这个程序中,当我为ex.2(包含30)输入索引编号时,在我想将年龄减去5后,它显示25,但当我想更改索引编号2时,它从25中减去,,,而不是从给定的索引中减去 例如 我在数据库(data2.txt)文件中存储了值 这样地。。 10 20 30 40 我想超出或更新值(30),所以我可以做什么改变 #include <stdio.h> #define PATH "/storage/emulated/0/c

在C语言中,如何在文件(数据库)中存储数组以及如何在程序中访问数组。在这个程序中,当我为ex.2(包含30)输入索引编号时,在我想将年龄减去5后,它显示25,但当我想更改索引编号2时,它从25中减去,,,而不是从给定的索引中减去 例如 我在数据库(data2.txt)文件中存储了值 这样地。。 10 20 30 40 我想超出或更新值(30),所以我可以做什么改变

  #include <stdio.h>
    #define PATH "/storage/emulated/0/c language/data2.txt"
    int main()
    {
        FILE *file;
        int age[], s, i;
        printf("Enter the array index:");
        scanf("%d", &i);
        file = fopen(PATH, "r");
        if (file == NULL)
        {
            printf("files does not exist");
            return 1;
        }
        fscanf(file, "%d", &age[i]);
        fclose(file);
        printf("Enter how much age should to be subtracted:");
        scanf("%d", &s);
        file = fopen(PATH, "w");
        age[i] = age[i] - s;
        fprintf(file, "%d", age[i]);
        fclose(file);
        printf("%d", age[i]);
    }
#包括
#定义路径“/storage/emulated/0/c language/data2.txt”
int main()
{
文件*文件;
国际年龄[],s,i;
printf(“输入数组索引:”);
scanf(“%d”、&i);
file=fopen(路径“r”);
if(file==NULL)
{
printf(“文件不存在”);
返回1;
}
fscanf(文件“%d”和年龄[i]);
fclose(文件);
printf(“输入应该减去多少年龄:”);
scanf(“%d”和“&s”);
file=fopen(路径“w”);
年龄[i]=年龄[i]-s;
fprintf(文件“%d”,年龄[i]);
fclose(文件);
printf(“%d”,年龄[i]);
}

如果您不想重置文件,您应该使用“a”参数,因为现在当您想更新文件中的值时,您只保存了25(如在ex中),所有其他值都消失了(但“a”会在EOF中附加保存的值)。如果我没有弄错,您正试图从文件中读取3个变量,但在执行此操作时:

fscanf(file, "%d", &age[i]);
您只得到文件中的第一个变量。如果你想得到第三个,你需要调用这个函数2次以上

最好是在循环中将所有数据放入数组中,然后对数组进行操作

#include <stdio.h>
#define SIZE 40
    int main()
    {
        FILE *file;
        int age[SIZE], s,i, j=0;
        printf("Enter the array index:");
        scanf("%d", &i);
        file = fopen("data.txt", "r");
        if (file == NULL)
        {
            printf("files does not exist");
            return 1;
        }
        while(1){ //getting all data from file to array
          if(feof(file))
            break;
          fscanf(file, "%d", &age[j++]); 
        }
        for(int x=0;x<j;x++)
          printf("%d ",age[x]);
        fclose(file);
        printf("\nEnter how much age should to be subtracted:");
        scanf("%d", &s);
        file = fopen("data.txt", "w");
        age[i] -= s;
        for(int k=0;k<j;k++) //passing updated data to file
          fprintf(file, "%d ", age[k]);
        fclose(file);
    }
#包括
#定义尺寸40
int main()
{
文件*文件;
int age[SIZE],s,i,j=0;
printf(“输入数组索引:”);
scanf(“%d”、&i);
file=fopen(“data.txt”、“r”);
if(file==NULL)
{
printf(“文件不存在”);
返回1;
}
而(1){//从文件到数组获取所有数据
if(feof(文件))
打破
fscanf(文件“%d”,&age[j++]);
}

对于(int x=0;当您使用file=fopen(路径,“w”);您以前的数据表单文件已丢失。当您使用fscanf(文件“%d”和年龄[i]”时,新数据将写入其位置);您只从文件中获取第一个数据,而且当您定义年龄数组时,您应该定义它的大小。ro如何在数据库中存储更多值,我想在数据库中向c程序显示特定行..例如.10.20 30 40在数据库中我只想超过30如何编写程序我编写的编辑告诉我您是否希望它工作我喜欢那样