about C programming...

2007-01-29 3:53 pm
In a C program, I want to read in a number of files and manipulate the data in these files, what should I do? Can I read in the files as some sort of "file array"? Help please~~~ many thanks!!

回答 (1)

2007-01-29 7:26 pm
✔ 最佳答案
As long as I understand, C does not support manipulating a list of files, but you can use a structure to help, e.g.:

typedef struct {
FILE* pt;
char* name;
char status;
} File_desciptor;

File_desciptor file[100];

void open_all () {
int i;
for (i=0;i<100;i++) {
file[i].pt = fopen(file[i].name, "rw+"); file[].status = OPEN;
//assume you have define a value of OPEN
}
}

void write_to(char* name, int k) {
int i;
for (i=0;i<100;i++) if (strcmp(name, file[i].name) == 0) break;
if (i >= 100) printf("file does not exist");
fprintf(file[i], "%D", k);
}
...


收錄日期: 2021-04-23 16:43:40
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20070129000051KK00408

檢視 Wayback Machine 備份