save file listing into array or something else C - c

I have this function
void file_listing(){
DIR *dp;
struct stat fileStat;
struct dirent *ep;
int file=0;
dp = opendir ("./");
if (dp != NULL){
while ((ep = readdir(dp))){
char *c = ep->d_name;
char d = *c; /* first char */
if((file=open(ep->d_name,O_RDONLY)) < -1){
perror("Errore apertura file");
}
if(fstat(file,&fileStat)){ /*file info */
perror("Errore funzione fstat");
}
if(S_ISDIR(fileStat.st_mode)){ /* directory NOT listed */
continue;
}
else{
if(d != '.'){ /* if filename DOESN'T start with . will be listed */
"save into someting" (ep->d_name);
}
else{
continue; /* altrimenti non lo listo */
}
}
}
(void) closedir (dp);
}
else{
perror ("Impossibile aprire la directory");
return 1;
}
}
I want to save into an array or a struct or a list or something else the result of file listing but i don't know how to do it.
Thanks in advance!

Here is a sample function that stores the file listing in array and returns the number of entries:
#include <string.h>
#include <stdio.h>
#include <dirent.h>
#include <malloc.h>
size_t file_list(const char *path, char ***ls) {
size_t count = 0;
size_t length = 0;
DIR *dp = NULL;
struct dirent *ep = NULL;
dp = opendir(path);
if(NULL == dp) {
fprintf(stderr, "no such directory: '%s'", path);
return 0;
}
*ls = NULL;
ep = readdir(dp);
while(NULL != ep){
count++;
ep = readdir(dp);
}
rewinddir(dp);
*ls = calloc(count, sizeof(char *));
count = 0;
ep = readdir(dp);
while(NULL != ep){
(*ls)[count++] = strdup(ep->d_name);
ep = readdir(dp);
}
closedir(dp);
return count;
}
int main(int argc, char **argv) {
char **files;
size_t count;
int i;
count = file_list("/home/rgerganov", &files);
for (i = 0; i < count; i++) {
printf("%s\n", files[i]);
}
}
Note that I iterate twice over the directory - first time to get the files count and second time to save the results. This won't work correctly if you add/remove files in the directory while this is being executed.

An other example, more cleaner but not thread safe because readdir() is not:
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
static void *realloc_or_free(void *ptr, size_t size) {
void *tmp = realloc(ptr, size);
if (tmp == NULL) {
free(ptr);
}
return tmp;
}
static int get_dirent_dir(char const *path, struct dirent **result,
size_t *size) {
DIR *dir = opendir(path);
if (dir == NULL) {
closedir(dir);
return -1;
}
struct dirent *array = NULL;
size_t i = 0;
size_t used = 0;
struct dirent *dirent;
while ((dirent = readdir(dir)) != NULL) {
if (used == i) {
i += 42; // why not?
array = realloc_or_free(array, sizeof *array * i);
if (array == NULL) {
closedir(dir);
return -1;
}
}
array[used++] = *dirent;
}
struct dirent *tmp = realloc(array, sizeof *array * used);
if (tmp != NULL) {
array = tmp;
}
*result = array;
*size = used;
closedir(dir);
return 0;
}
static int cmp_dirent_aux(struct dirent const *a, struct dirent const *b) {
return strcmp(a->d_name, b->d_name);
}
static int cmp_dirent(void const *a, void const *b) {
return cmp_dirent_aux(a, b);
}
int main(void) {
struct dirent *result;
size_t size;
if (get_dirent_dir(".", &result, &size) != 0) {
perror("get_file_dir()");
}
qsort(result, size, sizeof *result, &cmp_dirent);
for (size_t i = 0; i < size; i++) {
printf("%s\n", result[i].d_name);
}
free(result);
}

Related

How to save list of directory in global Array in C?

I have this code that print list of directory.
How to save that list of directory in global array and print that global array from main function ?
#include <stdio.h>
#include <dirent.h>
void listFiles(const char* dirname)
{
DIR* dir = opendir(dirname);
if(dir == NULL)
{
return ;
}
struct dirent* entity ;
entity = readdir(dir);
while(entity != NULL)
{
printf("%s\n", entity->d_name);
entity = readdir(dir);
}
closedir(dir);
return;
}
int main()
{
listFiles(".");
return 0;
}
Using global variables is a terrible habit. Also declaring functions as void if they should return something meaningful. In your case dynamically allocate the array and return it to the caller. Also try to reduce the number of return points in the function (ideally only one) by using positive checks instead of negative ones.
struct dirent *listFiles(const char* dirname, size_t *size)
{
struct dirent *entity, *result = NULL;
DIR* dir = NULL;
if(size)
{
dir = opendir(dirname);
*size = 0;
}
if(dir)
{
while((entity = readdir(dir)))
{
struct dirent *tmp;
tmp = realloc(result, sizeof(*result) * (*size + 1));
if(!tmp) {/* error handling */ }
else
{
result = tmp;
memcpy(result + *size, entity, sizeof(*result));
*size += 1;
}
}
closedir(dir);
}
return result;
}
int main(void)
{
size_t size;
struct dirent *dir = listFiles(".", &size);
if(size && dir)
{
for(size_t x = 0; x < size; x++)
{
printf("%s\n", dir[x].d_name);
}
}
free(dir);
}
https://godbolt.org/z/PPTcsd4E8
Instead memcpy you can also assign the structures:
result[*size] = *entity;

Double pointer in the function

I am trying to insert strings in the binary search tree.
So what I am to trying is,
parsing strings from a file(contains instruction set) and then inserting in the function
insertOpcodeFromFile().
So this function will execute
(*node) = Node_insert(&node,instruction).
the node will be the root of binary tree which is located in main function.
So in simple way to explain, I want to manipulate(insert) the root pointer in the main function by using double pointer in the other function contain insert function.
I have a simple understanding about the pointer, but in this situation, I need to use more than double pointer I think.
please explain me about the double pointer clearly using this example.
Here is my code(I commenting out insert_node)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef BINARYTREE_H_
#define BINARYTREE_H_
typedef struct node *NodePtr;
typedef struct node {
char *word;
int count;
NodePtr left;
NodePtr right;
} Node;
NodePtr Node_alloc();
NodePtr Node_insert(NodePtr node_ptr, char *word);
void clearArray(char a[]);
void insertOpcodeFromFile(FILE *opcodeFile, NodePtr *node);
void Node_display(NodePtr);
char *char_copy(char *word);
#endif
int main(int argc, const char * argv[]) {
FILE * opFile;
FILE * progFile;
struct node *root = NULL;
if ( argc != 4) { // # of flag check
fprintf(stderr, " # of arguments must be 4.\n" );
exit(1);
}
opFile = fopen ( argv[1], "r");
if(opFile == NULL)
{
fprintf(stderr,"There is no name of the opcode file\n");
exit(1);
}
progFile = fopen ( argv[2], "r");
if(progFile == NULL)
{
fprintf(stderr,"There is no name of the program file \n");
exit(1);
}
insertOpcodeFromFile(opFile, &root);
//Node_display(root);
}/* main is over */
void insertOpcodeFromFile(FILE *opcodeFile, NodePtr *node)
{
int fsize = 0;
int lengthOfInst = 0;
int c;
int i;
char buffer[100];
fsize = getFileSize(opcodeFile);
enum flag {ins,opc,form};
int flag = ins;
char instruction[6];
unsigned int opcode = 0;
unsigned char format;
while (c != EOF)
{
c = fgetc(opcodeFile);
buffer[i++] = c;
if (c == 32){
switch (flag) {
case ins:
flag = opc;
memcpy(instruction,buffer,i);
instruction[i] = '\0';
clearArray(buffer);
i = 0;
// printf("인스트럭션 : %s\n",instruction );
break;
case opc:
flag = form;
opcode = atoi(buffer);
clearArray(buffer);
i = 0;
// printf("옵코드 : %d\n",opcode );
break;
default:
break;
}/* end of switch */
}/* end of if(space) */
if((c == 10) || (c == EOF))
{
if (flag == form)
{
format = buffer[0];
clearArray(buffer);
i = 0;
// printf("포멧: %c\n", format);
}
flag = ins;
//node = Node_insert(node,instruction);
}
}
//Node_display(node);
}
int getFileSize(FILE *opcodeFile)
{ int fsize = 0;
fseek(opcodeFile,0, SEEK_SET);
fseek(opcodeFile,0, SEEK_END);
fsize = (int)ftell(opcodeFile);
fseek(opcodeFile,0, SEEK_SET);
return fsize;
}
int countUntilSpace(FILE *opcodeFile, int currentPosition)
{ char readword[1];
char *space = " ";
char *nextLine = "/n";
int i = 0;
//printf("현재: %d\n",currentPosition );
while(1)
{
fread(readword, sizeof(char),1,opcodeFile);
i++;
if(strcmp(readword,space) == 0 || strcmp(readword,nextLine) == 0)
{
//printf("break\n");
break;
}
}
fseek(opcodeFile,currentPosition ,SEEK_SET);
//printf("끝난 현재 :%d\n",ftell(opcodeFile) );
//printf("%I : %d\n",i );
return i - 1;
}
void clearArray(char a[])
{
memset(&a[0], 0, 100);
}
NodePtr Node_alloc()
{
return (NodePtr) malloc(sizeof(NodePtr));
}
NodePtr Node_insert(NodePtr node_ptr, char *word)
{
int cond;
if (node_ptr == NULL) {
node_ptr = Node_alloc();
node_ptr->word = char_copy(word);
node_ptr->count = 1;
node_ptr->left = node_ptr->right = NULL;
} else if ((cond = strcmp(word, node_ptr->word)) == 0) {
node_ptr->count++;
} else if (cond < 0) {
node_ptr->left = Node_insert(node_ptr->left, word);
} else {
node_ptr->right = Node_insert(node_ptr->right, word);
}
return node_ptr;
}
void Node_display(NodePtr node_ptr)
{
if (node_ptr != NULL) {
Node_display(node_ptr->left);
printf("%04d: %s\n", node_ptr->count, node_ptr->word);
Node_display(node_ptr->right);
}
}
char *char_copy(char *word)
{
char *char_ptr;
char_ptr = (char *) malloc(strlen(word) + 1);
if (char_ptr != NULL) {
char_ptr = strdup(word);
}
return char_ptr;
}
In this case, in main(),
Node *root;
Why do you need to use a "double" pointer ( Node ** ) in functions that alter root is because root value as to be set in these functions.
For instance, say you want to allocate a Node and set it into root.
If you do the following
void alloc_root(Node *root) {
root = malloc(sizeof (Node));
// root is a function parameter and has nothing to do
// with the 'main' root
}
...
// then in main
alloc_root( root );
// here main's root is not set
Using a pointer to pointer (that you call "double pointer")
void alloc_root(Node **root) {
*root = malloc(sizeof (Node)); // note the *
}
...
// then in main
allow_root( &root );
// here main's root is set
The confusion comes probably from the Node *root in main, root being a pointer to a Node. How would you set an integer int i; in a function f? You would use f(&i) to call the function f(int *p) { *p = 31415; } to set i to the value 31415.
Consider root to be a variable that contains an address to a Node, and to set its value in a function you have to pass &root. root being a Node *, that makes another *, like func(Node **p).

Prefix tree implementation for C

I am trying to write a C program that provides autocomplete suggestions looking at the contents of a directory. I have most of it done, however I get errors where directory names contain symbols that are not alphabets (such as . , / _). I get errors whenever I try to change things, including array size and looping values. Could you please take a look at my code and suggest what changes I need to make to include symbols in autocomplete suggestions.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#define MAX 1000
struct node {
int data;
struct node *array[26];
};
struct node* new_node(struct node *h)
{
int i;
h = malloc(sizeof (struct node));
for (i = 0; i < 24; ++i)
h->array[i] = 0;
return h;
}
struct node* insert(struct node *h, char *c, int value)
{
int i;
if (strlen(c) == 0)
return h;
if (h == NULL)
h = new_node(h);
struct node *p = h;
for (i = 0; i < strlen(c); ++i) {
if (p->array[c[i] - 'a'] == NULL)
p->array[c[i] - 'a'] = malloc(sizeof (struct node));
p = p->array[c[i] - 'a'];
}
p->data = value;
return h;
}
int dfs(struct node *h, char *dat)
{
int i;
char k[1000] = {0};
char a[2] = {0};
if (h == NULL)
return 0;
if (h->data > 0)
printf("Match: %s\n", dat);
for(i = 0; i < 26; ++i) {
strcpy(k, dat);
a[0] = i + 'a';
a[1] = '\0';
strcat(k, a);
dfs(h->array[i], k);
}
}
void search(struct node *h, char *s, char *dat)
{
int i;
char l[1000] = {0};
char a[2];
strcpy(l, dat);
if (strlen(s) > 0) {
a[0] = s[0];
a[1] = '\0';
if(h->array[a[0]-'a'] != NULL) {
strcat(dat, a);
search(h->array[a[0]-'a'], s+1, dat);
} else
printf("No Match \n");
} else {
if (h->data != 0)
printf("Match: ");
for (i = 0; i < 26; ++i) {
strcpy(l, dat);
a[0] = i + 'a';
a[1] = '\0';
strcat(l, a);
dfs(h->array[i], l);
}
}
}
struct node* read_keys(struct node *h, char *file)
{
char s[MAX];
FILE *a = fopen(file, "r");
if (a == NULL)
printf("Error while opening file");
else
while (feof(a) == 0) {
fscanf(a, "%s", s);
h = insert(h, s, 1);
}
return h;
}
int main()
{
DIR *d;
struct dirent *dir;
char direct[MAX];
printf("Enter a folder name: ");
scanf("%s",direct);
d = opendir(direct);
FILE *f = fopen("file.txt", "w");
if (f == NULL)
{
printf("Error opening file!\n");
exit(1);
}
if (d)
{
while ((dir = readdir(d)) != NULL)
{
fprintf(f,"%s\n", dir->d_name);
}
}
fclose(f);
char c[1000];
FILE *fptr;
if ((fptr = fopen("file.txt", "r")) == NULL)
{
printf("Error! opening file");
exit(1);
}
FILE* file = fopen("file.txt", "r");
char line[1000];
while (fgets(line, sizeof(line), file)) {
printf("%s", line);
}
fclose(fptr);
printf("Enter beginning of filename \n");
struct node *h = 0;
h = read_keys(h, "file.txt");
char s[MAX];
scanf("%s", s);
char dat[1000] = "";
search(h, s, dat);
return 0;
}

Trouble with Malloc and Linked List

I have a function that reads a text file filled with a word on every line. Here is an example of a text file I'm using
and
but
five
follows
four
has
is
like
line
lines
littlest
not
once
one
only
other
six
the
three
twice
two
word
words
Code:
typedef struct node node_t;
struct node {
char data[MAX_WORD];
int term;
node_t *next;
};
node_t *head;
int
int_struct(int lines){
FILE *fp;
char ch;
int n = 0, i, switch_num=1, test_first=0, test_first_2=0;
node_t *node, *curr_add;
fp = fopen("text.txt", "r");
node = (node_t*)malloc(sizeof(node_t));
for (i=1; i<=lines; i++){
switch_num = 1;
n=0;
if (test_first != 0){
if (test_first_2){
node = (node_t*)malloc(1000000);
}
test_first_2=1;
while ((ch = getc(fp)) != '\n'){
node -> term = i;
node -> data[n] = ch;
n++;
}
curr_add -> next = node;
curr_add = node;
}
else{
test_first = 1;
head = curr_add = node;
}
}
curr_add -> next = NULL;
fclose(fp);
return num;
}
What I want to do is to read each word and add it to a linked list.
However I am having trouble with malloc (at the moment I just add in a lot of bytes) and need advice on how to properly use it inside the function I have. I've done a general search and tried my best to try and do what most examples do. But I still can't seem to get my function working. For example, every time I execute the program it will read and add all the words into the linked list. However, the program crashes on the last word, and returns NULL. If anyone is able to point me in the right direction, I'd be very grateful.
Issues
There are no checks for return values. Particularly, fopen and malloc
may return NULL. If they do, you'll catch a segmentation fault error on the
first attempt to access the returned value.
Overcomplicated logic. You don't need these switch_num, test_first and test_first_2
variables (see sample code below).
No need in getc when you're reading a text file line-by-line - use
fgets instead.
Too many memory allocations. You don't need more than sizeof(node_t) + length of the line bytes per line.
The allocated memory is not freed. The dynamic memory should be freed as
soon as it is not needed.
Example using linked list
The following reads a text file into a linked list. Memory is allocated for
each list item, and for each line in the file resulting in n * 2 memory
allocations, where n is the number of lines in the file.
#include <stdio.h>
#include <stdlib.h>
#include <string.h> /* strerror, strdup */
#include <errno.h>
typedef struct _node {
unsigned line;
char *data;
struct _node *next;
} node_t;
static void
destroy_list(node_t *list)
{
node_t *node;
for (node = list; node; node = node->next) {
if (node->data != NULL)
free(node->data);
free(node);
}
}
static node_t *
create_list_item(const char *data, unsigned line)
{
node_t *node = calloc(1, sizeof(node_t));
if (node == NULL) {
fprintf(stderr, "calloc: %s\n", strerror(errno));
} else {
node->line = line;
node->data = strdup(data);
if (node->data == NULL) {
fprintf(stderr, "strdup: %s\n", strerror(errno));
free(node);
node = NULL;
}
}
return node;
}
/* Returns pointer to new linked list */
static node_t *
read_file(FILE *fp, char *buf, size_t buf_len)
{
node_t *list = NULL;
node_t *prev = NULL;
node_t *node;
unsigned i;
for (i = 0; fgets(buf, buf_len, fp); prev = node) {
if ((node = create_list_item(buf, ++i)) == NULL) {
fprintf(stderr, "calloc: %s\n", strerror(errno));
break;
}
if (list == NULL)
list = node;
if (prev != NULL)
prev->next = node;
}
return list;
}
static void
print_list(const node_t *list)
{
const node_t *node;
for (node = list; node; node = node->next)
printf("%d: %s", node->line, node->data);
}
int main(int argc, char const* argv[])
{
const char *filename = "text.txt";
char buf[1024] = {0};
FILE *fp = NULL;
node_t *list = NULL;
if (NULL == (fp = fopen(filename, "r"))) {
fprintf(stderr, "failed to open file %s: %s\n",
filename, strerror(errno));
return 1;
}
list = read_file(fp, buf, sizeof(buf));
fclose(fp);
if (list) {
print_list(list);
destroy_list(list);
}
return 0;
}
Example using dynamic array
It is inefficient to allocate memory for each line (twice) in the file,
not only because the system calls (malloc, realloc, etc.) are costly,
but also because the items are placed non-contiguously. Accessing contiguous
region of memory is usually faster.
In the following code, the linked list is replaced with dynamic array. We
initialize memory for 10 lines at once. The size is increased as necessary.
#include <stdio.h>
#include <stdlib.h>
#include <string.h> /* strerror, strdup */
#include <errno.h>
typedef struct _node {
size_t line;
char *data;
} node_t;
static void
destroy_array(node_t *array, size_t size)
{
size_t i;
node_t *item;
for (i = 0; i < size; i++) {
item = &array[i];
if (item->data)
free(item->data);
}
free(array);
}
static void
print_array(node_t *array, size_t size)
{
size_t i;
node_t *item;
for (i = 0; i < size; i++) {
item = &array[i];
if (item->data) {
printf("%ld: %s", item->line, item->data);
}
}
}
static node_t *
read_file(FILE *fp, char *buf, size_t buf_len,
const size_t array_step, size_t *array_size)
{
node_t *item;
node_t *array = calloc(array_step, sizeof(node_t));
size_t size = 0;
if (array == NULL) {
fprintf(stderr, "calloc:%s\n", strerror(errno));
return array;
}
while (fgets(buf, buf_len, fp)) {
if (size && size % array_step == 0) {
array = realloc(array, sizeof(node_t) * (array_step + size));
if (array == NULL) {
fprintf(stderr, "realloc:%s\n", strerror(errno));
break;
}
}
item = &array[size++];
item->line = size;
item->data = strdup(buf);
if (item->data == NULL) {
fprintf(stderr, "strdup: %s\n", strerror(errno));
break;
}
}
*array_size = size;
return array;
}
int main(int argc, char const* argv[])
{
node_t *array;
const size_t array_step = 10;
size_t array_size;
const char *filename = "text.txt";
char buf[1024] = {0};
FILE *fp = NULL;
if (NULL == (fp = fopen(filename, "r"))) {
fprintf(stderr, "failed to open file %s: %s\n",
filename, strerror(errno));
return 1;
}
array = read_file(fp, buf, sizeof(buf), array_step, &array_size);
fclose(fp);
if (array) {
print_array(array, array_size);
destroy_array(array, array_size);
}
return 0;
}
Note the changes in node_t structure.

Filling Gtk Tree Store from a file

I'm trying to fill a gtk tree store from a linked list but i get the segmentation fault (core dumped) problem here is my code File to struct is a linked list filled a struct to a professor and a next the professor is defined by a name matiere password pseudo...etc
store = gtk_list_store_new (NUM_COLUMNNS,G_TYPE_INT,G_TYPE_STRING,G_TYPE_STRING,G_TYPE_STRING,G_TYPE_STRING,G_TYPE_INT);
File_to_struct *p=head;
/* add data to the list store */
while(p!=NULL)
{
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter,
COLUMNN_ID,p->professeur.ID,
COLUMNN_NOM,p->professeur.nom,
COLUMNN_MATIERE,p->professeur.matiere,
COLUMNN_PSEUD,p->professeur.pseudo,
COLUMNN_PASS,p->professeur.password,
COLUMNN_VALIDE,p->professeur.valide,
-1);
p=p->suivant;
}
FILE fichier=fopen("professeur.txt","r");
Prof professeur;
File_to_struct *tete=(File_to_struct)malloc(sizeof(File_to_struct));
tete=NULL;
rewind(fichier);
while((!feof(fichier)))
{
fscanf(fichier,"\n%s %s %s %s %d %d\n",professeur.nom,professeur.matiere,professeur.pseudo,professeur.password, &pro‌​fesseur.valide,&professeur.ID);
tete=inserer(tete,professeur); }
typedef struct prof{
int ID;
int valide;
char nom[40];
char matiere[40];
char password[40];
char pseudo[40]; }Prof;
typedef struct file_to_struct{
Prof professeur;
struct file_to_struct *suivant; }File_to_struct;
You have problems when filling the list, this is an example of how to do it correctly
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct Prof
{
int ID;
int valide;
char nom[40];
char matiere[40];
char password[40];
char pseudo[40];
} Prof;
typedef struct File_to_struct
{
Prof professeur;
struct File_to_struct *suivant;
} File_to_struct;
File_to_struct *
inserer(File_to_struct *Debut, Prof profs)
{
File_to_struct *nv;
File_to_struct *actuel;
nv = malloc(sizeof(File_to_struct));
if (nv == NULL)
return Debut;
nv->suivant = NULL;
nv->professeur = profs;
actuel = Debut;
if (Debut == NULL)
Debut = nv;
else
{
while (actuel->suivant != NULL)
actuel = actuel->suivant;
actuel->suivant = nv;
}
return Debut;
}
char **
splitString(const char *const text, const char *const delimiter, int *count)
{
char *copy;
char *pointer;
char *token;
char *saveptr;
char **list;
if ((text == NULL) || (count == NULL) || (delimiter == NULL))
return NULL;
copy = strdup(text);
*count = 0;
pointer = copy;
list = NULL;
while ((token = strtok_r(pointer, delimiter, &saveptr)) != NULL)
{
void *auxiliary;
auxiliary = realloc(list, (1 + *count) * sizeof(char *));
if (auxiliary == NULL)
{
while (*count >= 0)
{
free(list[*count]);
*count -= 1;
}
free(copy);
free(list);
return NULL;
}
list = auxiliary;
list[*count] = strdup(token);
*count += 1;
pointer = NULL;
}
free(copy);
return list;
}
Prof
extractProfesseur(const char *const line)
{
char **list;
int count;
Prof prof;
memset(&prof, 0, sizeof(prof));
list = splitString(line, " ", &count);
if (count < 6)
{
while (--count >= 0)
free(list[count]);
free(list);
return prof;
}
prof.ID = strtol(list[4], NULL, 10);
prof.valide = strtol(list[5], NULL, 10);
strncpy(prof.nom, list[0], 39);
strncpy(prof.matiere, list[1], 39);
strncpy(prof.pseudo, list[2], 39);
strncpy(prof.password, list[3], 39);
while (--count >= 0)
free(list[count]);
free(list);
return prof;
}
int
main()
{
FILE *fichier;
File_to_struct *tete;
File_to_struct *actuel;
char line[256];
fichier = fopen("professeur.txt","r");
if (fichier == NULL)
return -1; /* n'pouvez pas de ouvrir le fichier */
tete = NULL;
rewind(fichier);
while (fgets(line, sizeof(line), fichier) != NULL)
{
Prof professeur;
professeur = extractProfesseur(line);
tete = inserer(tete, professeur);
}
actuel = tete;
while (actuel != NULL)
{
Prof prof;
File_to_struct *previous;
prof = actuel->professeur;
previous = actuel;
actuel = actuel->suivant;
printf("%s %s %s %s %d %d\n", prof.nom, prof.matiere,
prof.pseudo, prof.password, prof.valide, prof.ID);
free(previous);
}
fclose(fichier);
return 0;
}

Resources