如何使用c中的檔案更新用戶資訊檔案內容:Belick 44 Miami Sara 21 Boston John 24 Chicago name age city
我想更改 Sara 的年齡,例如:55 因此,檔案將更新為如下所示 Belick 44 Miami Sara 55 Boston John 24 Chicago
#include <stdio.h>
#include <string.h>
int main (void) {
FILE *ptr;
int age;`enter code here`
char name[50];
char n[50];
int newAge;
char city[50];
ptr = fopen("update.txt", "r ");
if (ptr==NULL) {
printf("Unable to open the file...\n");
}
/*
The content of the file:
Belick 44 Miami
Sara 21 Boston
John 24 Chicago
name age city
I would like to change Sara's age ex: 55
so, the file will be updated as shown
Belick 44 Miami
Sara 55 Boston
John 24 Chicago
*/
else
{
do {
printf("your name: ");
scanf("%s", n);
printf("Enter your new age: ");
scanf("%d", &newAge);
fscanf(ptr,"%s %d %s", name, &age, city);
age = newAge;
fprintf(ptr,"%s %d %s\n", name, age, city);
}
while(strcmp(n, name)!=0);
fclose(ptr);
}
return 0;
}
uj5u.com熱心網友回復:
第一項任務是確定實作既定目標所需的步驟。大多數評論都指出了這一點。同樣從評論中可以看出,任務可以通過許多不同的方式實作。以下代碼并不完整,但其中的部分確實可以編譯并運行。其目的是為您的既定目標提供入門指南。
“如何使用c中的檔案更新用戶資訊檔案的內容”
下面假設".\\names.txt"本地目錄中有一個檔案,其內容類似于您的示例:
Belick 44 Miami
Sara 21 Boston
John 24 Chicago
根據需要更改。
請注意,在學習 C 中使用 struct 物件比使用鏈表更早,因此本示例使用 struct ,因為我沒有跡象表明您會習慣使用鏈表。但是,如果您需要擴展您的既定目標以包括洗掉或添加記錄,那么考慮使用鏈表而不是基本結構物件是值得的,因為記憶體管理對于串列而言比結構物件更內在,當添加或洗掉記錄。
#define MAX_PATH 260
typedef struct {
char name[80];
int age;
char city[80];
} record_s;
record_s * read_records(const char *filename, int count);
void line_count(const char *fn, int *count);
int main(int argc, char *argv[])
{
//read file into memory (array of struct)
record_s *records = read_records(".\\names.txt", &count);
//write user prompt to know what record field to edit (name, age, city)
//write scan statements to read in user input choices
//create function to pass pointer to struct object to search and edit field
//create function to write updated records to same file
//
//finally, free memory created in first function
free(records);
return 0;
}
//read file into 'count' instances of struct
record_s * read_records(const char *filename, int *count)
{
int lines;
int i = 0;
char line[MAX_PATH] = {0};
char *tok = NULL;
char *delim = {" \n\t"};
record_s *recs = NULL;
FILE *fp = fopen(filename, "r");
if(fp)
{
//get count of lines in file
line_count(filename, &lines);
//create count instances of struct
recs = malloc(sizeof(*recs)*lines);
if(recs)
{ //populate instances of struct with record fields
while(fgets(line, sizeof line, fp))
{
tok = strtok(line, delim);
if(tok)
{
strcpy(recs[i].name, tok);
tok = strtok(NULL, delim);
if(tok)
{
recs[i].age = atoi(tok);
tok = strtok(NULL, delim);
if(tok)
{
strcpy(recs[i].city, tok);
}
}
}
i ;
}
}
fclose(fp);
*count = lines;
}
return recs;
}
//get count of lines in file
void line_count(const char *fn, int *count)
{
int cnt = 0;
char line[MAX_PATH] = {0};
FILE *fp = fopen(fn, "r");
if(fp)
{
while(fgets(line, sizeof line, fp))
{
cnt ;
}
fclose(fp);
}
*count = cnt;
}
如需進一步幫助:
- 控制臺選單示例
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/382502.html
上一篇:在xml檔案中追加字串
下一篇:VBADIR命令出現故障
