【发布时间】:2019-11-02 03:01:44
【问题描述】:
在C语言中,如何将数组存储在文件(数据库)中以及如何在程序中访问它。在这个程序中,当我输入 ex.2 的索引号(包含 30)时,在我想将年龄减去 5 之后它显示 25 但是当我想更改索引号 2 时,它从 25 中减去,,,不是从给定的索引
#include<stdio.h>
#define PATH "/storage/emulated/0/c language/data2.txt"
int main()
{
FILE *file;
int age[] = {15,10,19,3}, 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]);
}
【问题讨论】:
标签: c arrays database file arraylist