Thread: Regarding File IO

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    8

    Regarding File IO

    Hmmm, I have a phonebook program but, I don't know how to store my previously recorded entries so that whenever I open my phonebook again the previously recorded files are still there for access.

    Replies will be highly appreciated

    Thanks in advance!!

    FILE IO and Linked Lists are the topic of this activity.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    First you should post your attempt.Then the story goes on

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    8
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    typedef struct phonebook {
        char name[30];
        char tel[15];
        int age;
        struct phonebook *next;
    } phonebook_t;
    
    
    void AddEntry (void); 
    void DeleteEntry (void);
    void SearchEntry (void);   
    void ListAll(void);
    void writePCText(void);
    
    
    phonebook_t *head= NULL;
    
    
    int main() {
        
        int choice;
    
    
        while (choice!=5) {
        printf("\nWelcome to PHONEBOOK MENU:\n\n");
        printf("> [1] Add Entry\n");
        printf("> [2] Delete Entry\n");
        printf("> [3] Search for Entry\n");
        printf("> [4] Show all Entries\n");
        printf("> [5] Exit\n\n");
        printf("ENTER CHOICE: ");
        scanf("%d",&choice);
            switch (choice) {
                case 1: AddEntry();
                break;
                case 2: DeleteEntry();
                break;
                case 3: SearchEntry();
                break;
                case 4: ListAll();
                break;
                case 5:
                    writePCText(); 
                     exit(1);
                break;
               
                default:
                        printf ("Invalid INPUT!\n"); 
            }
        
        }
        return 0;
    }    
    
    
    void AddEntry (void){
        phonebook_t *new_name;
        
        new_name= (phonebook_t *)malloc (sizeof (phonebook_t));
        if (new_name == NULL) {
            printf ("Out of memory!\n");
            exit (-1);
        }
        printf ("\n\nName: ");
        getchar();
        fgets(new_name->name,30,stdin); 
        printf ("Phone Number: ");
        fgets(new_name->tel,15,stdin); 
        printf ("Age (Numerical Value):  ");
        scanf("%d",&new_name->age);
        
        new_name->next= head;
        head= new_name;
    }    
    
    
    void DeleteEntry (void) {
        phonebook_t *del_ptr;   
        phonebook_t *prev_ptr;  
        char del_name[30]; 
        
        printf ("Enter Name to be Deleted:  ");
        getchar();
        fgets(del_name,30,stdin); 
        if (head == NULL) {
            printf ("\nName not registered in the list!\n");
            return;
        }
        if (strcmp(head->name, del_name) == 0) {
           printf("\n%s Deleted!\n",del_name);
            del_ptr = head;
            head = head->next;
            free(del_ptr);
            return; 
        }
        prev_ptr = head;
        while (prev_ptr->next != NULL) {
            if (strcmp(prev_ptr->next->name,del_name) == 0) {
            printf("\n%s Deleted!\n",del_name);
                del_ptr= prev_ptr->next;
                prev_ptr->next= del_ptr->next;
                free(del_ptr);
                return;
            }
            prev_ptr= prev_ptr->next;
        }
        printf ("Name not found in the list!\n");
    }
    
    
    void SearchEntry (void){
        char name[30];  
        phonebook_t *search_ptr;
        printf ("\nSearch for a Name (Exact): ");
        getchar();
         fgets(name,30,stdin); 
        search_ptr= head;
        while (search_ptr != NULL) {
            if (strcmp (search_ptr->name, name) == 0) {  
                printf("\n%sFound!\n",name);
                printf ("Phone Number: %s\n", search_ptr->tel);
                printf ("Age: %d\n", search_ptr->age);
                return;
            }
            search_ptr= search_ptr->next;
        }
        printf ("\nName not registered in the list!\n");
    }
    
    
      void ListAll(void)
    {
        phonebook_t *tmp_ptr;
        printf ("\nAll Names Registered:\n");
        tmp_ptr= head;
        while (tmp_ptr != NULL) {
            printf ("\nName: %s\n",tmp_ptr->name);
            printf ("Phone Number: %s\n",tmp_ptr->tel);
            printf ("Age: %d\n\n\n",tmp_ptr->age);
            tmp_ptr= tmp_ptr->next;
        }
    }
    
    
    void writePCText(void){
          FILE *out;
    
    
          out = fopen("hehe.txt", "w" );
          phonebook_t *tmp_ptr;
          tmp_ptr=head;
          while(tmp_ptr!=NULL){
    		fprintf(out,"%s" , tmp_ptr->name);
    		fprintf(out,"%s", tmp_ptr->tel);
    		fprintf(out,"%d", tmp_ptr->age);
    		tmp_ptr= tmp_ptr->next;
        }
        
         printf("Thank you! Hope to see you soon!");
         exit(1);
         }



    Here is my code )

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    You already have a function you can use to write to a file ...

    Try changing ListAll() to accept a FILE *.
    Then either send stdout for listing on the screen, or a pointer to an opened file for listing on that particular file

    Code:
    void ListAll(FILE *handle) {
        /* ... */
        fprintf(handle, "whatever");
        /* .... */
    }
    and call it like

    Code:
    ListAll(stdout);
    or
    Code:
    FILE *h;
    h = fopen("whatever", "w");
    if (h) {
        ListAll(h);
        fclose(h);
    } else /* error */;
    --------

    Of course you still have to write the function to read from the file.

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    8
    Thank you very much, I'll also use that but my problem is that I need to save my previous entries so that whenever I open my phonebook again, I can use the delete, search and list function for my previously recorded data.

  6. #6
    Registered User poornaMoksha's Avatar
    Join Date
    Sep 2011
    Location
    India
    Posts
    41
    Quote Originally Posted by GangNam View Post
    Thank you very much, I'll also use that but my problem is that I need to save my previous entries so that whenever I open my phonebook again, I can use the delete, search and list function for my previously recorded data.
    What you can do is that whenever you start/restart this phonebook program, read all the entries from 'hehe.txt' (if exists) into the link list and then perform all the operations on this link list. This way you always have all the data in your link list. Then finally when your phonebook exits, you can flush all the data from link list to the text file again.

  7. #7
    Registered User
    Join Date
    Aug 2012
    Posts
    8
    I can't seem to read the entries from the txt file >.<

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    What have you tried?

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 09-25-2011, 12:22 AM
  2. Replies: 3
    Last Post: 03-08-2010, 02:43 PM
  3. Replies: 2
    Last Post: 03-04-2010, 04:19 AM
  4. Replies: 3
    Last Post: 11-21-2006, 07:26 PM
  5. Replies: 4
    Last Post: 07-06-2006, 02:53 AM