Thread: Trouble with Files

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    8

    Trouble with Files

    I need to 1.) allow to users to store all entries into a location/filename specified by user (or default location) and 2.) retrieve entries from said location.

    At the moment I can't retrieve files (pRead keeps coming up NULL). I think pWrite is saving the data to file, but b/c I can't retrieve the file I can't be sure of that. I think the issue is with letting the user specify the name of the File, but obviously I'm not sure. My compiler isn't showing any errors, but is there something wrong with my syntax or something? I'm hoping someone can help me narrow down the problem.

    Here are my functions:
    Code:
    //Store entries in file
    void StoreFile()
    {
         int iPick;
         char FileName[50];
         int i;
         
         
         printf("\n1) Name the file yourself or\n2) Use the default name(phone_book.txt)\n\nSelect your option (enter 1 or 2): ");
         scanf("%d", &iPick);
                                
         if (iPick == 2)
         {
         pWrite = fopen("phone_book.txt", "w");     
         }
         else
         {
             printf("What do you want to name the file?");
             scanf("%s", FileName);
             pWrite = fopen("FileName", "w");
         }
                                
         if (pWrite == NULL)
         {
         printf("\nFile cannot be opened\n");
         }
         else
         {
             for (i = 0; i < count; i++)
             {
                    fprintf(pWrite, "%s\t%s\t%s\n", lt[i].FirstName, lt[i].LastName, lt[i].PhoneNumber);
                    fclose(pWrite);
             }//End for loop
             
             printf("\nEntries saved to file\n");
         }   
          
    }//End Function
    
    //Retrieve entries from file
    void RetrieveFile()
    {
         char rFileName[50];
         int i;
         
         printf("\nName of file you want to retrieve? ");
         scanf("%s", rFileName);
    
         pRead = fopen("rFileName", "r");
    
         if (pRead == NULL)
         {
         printf("\nFile cannot be opened\n");
         }
         else
         {
             for (i = 0; i < count; i++)
             {
                    fscanf(pWrite, "%s\t%s\t%s\n", lt[i].FirstName, lt[i].LastName, lt[i].PhoneNumber);
                    printf("\n%s\t%s\t%s", lt[i].FirstName, lt[i].LastName, lt[i].PhoneNumber);
                    
             }//End for loop
         }    
    }//End Function
    Here's my entire program for reference.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    //Function prototypes
    void Add();
    void Delete();
    void Display();
    void Alphabetical();
    void Number();
    void Random();
    void DeleteAll();
    void StoreFile();
    void RetrieveFile();
    
    //Structure for Phonebook list
    typedef struct PhoneBookList {
            
            char *FirstName;
            char *LastName;
            char *PhoneNumber;
            
    } list;
    
    //Structure for delete
    typedef struct DeleteEntry {
            
            char *FirstName;
            char *LastName;
            
    } take;
    
    
    //Pointers to structures
    list *lt;
    take *tk;
    
    //Global variables
    int count = 0;
    int delCount = 0;
    
    //Files
    FILE *pWrite;
    FILE *pRead;
    
    int main(void) 
    { 
           //Variable declarations
           int iSelection;
           
           do {    //Print menu to screen, ask user for selection
              printf("\n\nPhone Book:\n\n");
              printf("1) Add friend\n");
              printf("2) Delete friend\n");
              printf("3) Display phone book\n");
              printf("4) Sort and display entries alphabetically by last name\n");
              printf("5) Find phone number for given name\n");
              printf("6) Randomly select contact\n");
              printf("7) Delete all contacts\n");
              printf("8) Store all entries in a file\n");
              printf("9) Retrieve entries from file\n");
              printf("10) Exit\n");
              printf("What do you want to do? ");
            
              scanf("%d", &iSelection);
            
              switch (iSelection) {
                           
                           case 1:  //Add entry
                                Add();  
                                break;
                           
                           case 2:  //Delete entry
                                Delete();
                                break;
                                
                           case 3:  //Display all entries
                                Display();
                                break;
                                
                           case 4: //Sort entries alphabetically
                                Alphabetical();
                                break;
                                
                           case 5: //Find phone number
                                Number();
                                break;
                                
                           case 6: //Randomly select contact
                                Random();
                                break;
                                
                           case 7: //Delete all contacts
                                DeleteAll();
                                break;
                                
                           case 8: //Store entries in a file
                                StoreFile();
                                break;
                                
                           case 9: //Retrieve entries from file
                                RetrieveFile();
                                break;
                                
                           case 10: //Break loop and close program
                                break;
                           
                           default:  //User entered an invalid number
                                printf("\nYou entered an invalid selection.  Try again.\n");
                                break;
                           
                           } //End Switch
                           
              } while (iSelection != 10);  //End Do While loop
              
              //Return used memory
              free(tk);
              free(lt);
              tk = NULL;
              lt = NULL;
              
              return 0;
              
    } //End Main
    
    //Function definitions
    
    //Add an entry
    void Add()
    
    { 
         char *leftName;
         
        if (count == 0) 
         {
               lt = (list *) malloc ((count*25) + 25);
         }
         else
         {
               lt = (list *) realloc (lt, (count*50) + 50);
         }
        
        if (lt == NULL)
        {
               printf("You cannot add more memory\n");
        }
        else
        {
               lt[count].FirstName = (char *) malloc(sizeof(char)*15);
               lt[count].LastName = (char *) malloc(sizeof(char)*15);
               lt[count].PhoneNumber = (char *) malloc(sizeof(char)*15);
               printf("\nEnter their First Name: ");
               scanf("%s", lt[count].FirstName);
               printf("\nEnter their Last Name: ");
               scanf("%s", lt[count].LastName);
               printf("\nEnter their Phone Number: ");
               scanf("%s", lt[count].PhoneNumber);
               
               printf("\nContact added\n");
               
        }
        
        count++;
        
    }//End Function
    
    //Delete an entry
    void Delete()
    
    
    {
         int i;
         int q = 0;
         char *userName;
         
         if (delCount == 0) 
         {
                   tk = (take *) malloc ((delCount*25) + 25);
         }
         else
         {
                   tk = (take *) realloc (tk, (delCount*1) + 1);
         }
         
         if (tk == NULL)
         {
                printf("This cannot be deleted (out of memory)\n");
         }
         else
         {
                tk[delCount].FirstName = (char *) malloc(sizeof(char)*15);
                tk[delCount].LastName = (char *) malloc(sizeof(char)*15);
                printf("\nEnter their First Name: ");
                scanf("%s", tk[delCount].FirstName);
                printf("\nEnter their Last Name: ");
                scanf("%s", tk[delCount].LastName);
         }
           
         for (i = 0; i < count; i++)
         {
             if (lt[i].FirstName == NULL && lt[i].LastName == NULL) continue;
             if (strcmp(lt[i].FirstName, tk[delCount].FirstName) == 0 && strcmp(lt[i].LastName, tk[delCount].LastName) == 0)
             {
                printf("\n%s %s has been deleted\n", lt[i].FirstName, lt[i].LastName);
                lt[i].FirstName = NULL;
                lt[i].LastName = NULL;
                lt[i].PhoneNumber = NULL;
                q = 1;
                break;
             }
         } //End for loop                       
             
         if (q != 1)
         {
         printf("\nThis person is not in the Phonebook\n");
         }
             
         delCount++;
         count--;
         
    }//End function
    
    //Display all phonebook entries
    void Display()
    {
         int i;
         
         printf("\nYour contacts:\n");
         
         for (i = 0; i < count; i++)
         {
             if (lt[i].FirstName != NULL && lt[i].LastName != NULL)
             {
                                 printf("\n%s %s: %s\n", lt[i].FirstName, lt[i].LastName, lt[i].PhoneNumber);
             }
         }//End for loop
         
         system("pause");
    
    }//End function
    
    //Sort entries alphabetically by last name.
    void Alphabetical()
    {
         int i;
         int j;
         char temp[50][50];
         
         printf("\nYour contacts:\n");
         
         for (i = 0; i < count; i++)
         {
             for (j = i + 1; j < count; j++)
             {
                if (strcmp(lt[i].LastName, lt[j].LastName) > 0)
                 {
                                            strcpy(temp[i], lt[i].LastName);
                                            strcpy(lt[i].LastName, lt[j].LastName);
                                            strcpy(lt[j].LastName, temp[i]);
                 }
             }
         }//End for loop
         
         for (i = 0; i < count; i++)
             printf("\n%s\n", lt[i].LastName);
             
             system("pause");
    
    }//End function  
    
    //Find phone number for given name
    void Number()
    {  
         int i;
         int q = 0;
         char fName[25];
         char lName[25];
         
         printf("\nEnter their First Name: ");
         scanf("%s", fName);
         printf("\nEnter their Last Name: ");
         scanf("%s", lName);
           
         for (i = 0; i < count; i++)
         {
             if (strcmp(lt[i].FirstName, fName) == 0 && strcmp(lt[i].LastName, lName) == 0)
             {
                printf("\n%s %s's phone number is:  %s\n", lt[i].FirstName, lt[i].LastName, lt[i].PhoneNumber);
                q = 1;
                break;
             }
         }
         
         if (q != 1)
         {
         printf("\nThis person is not in the Phonebook\n");
         }
         
         system("pause");
         
    }//End function
    
    //Randomly select contact
    void Random()
    {
          srand(time(NULL));  
          int iRandomNum;   
          
          iRandomNum = (rand() % count) + 1;
          
          printf("%s %s:  %s\n", lt[iRandomNum].FirstName, lt[iRandomNum].LastName, lt[iRandomNum].PhoneNumber);
          
          system("pause");
          
    }//End function
    
    //Delete everyone from phone book
    void DeleteAll()
    {
         int i;
         
         for (i = 0; i < count; i++)
         {
            do{
                
                lt[i].FirstName = NULL;
                lt[i].LastName = NULL;
                lt[i].PhoneNumber = NULL;
                break;
                
            } while (i <= count); 
         }
         
         printf("\nAll contacts deleted\n\n");
         
         system("pause");
         
    }//End function
              
    //Store entries in file
    void StoreFile()
    {
         int iPick;
         char FileName[50];
         int i;
         
         
         printf("\n1) Name the file yourself or\n2) Use the default name(phone_book.txt)\n\nSelect your option (enter 1 or 2): ");
         scanf("%d", &iPick);
                                
         if (iPick == 2)
         {
         pWrite = fopen("phone_book.txt", "w");     
         }
         else
         {
             printf("What do you want to name the file?");
             scanf("%s", FileName);
             pWrite = fopen("FileName", "w");
         }
                                
         if (pWrite == NULL)
         {
         printf("\nFile cannot be opened\n");
         }
         else
         {
             for (i = 0; i < count; i++)
             {
                    fprintf(pWrite, "%s\t%s\t%s\n", lt[i].FirstName, lt[i].LastName, lt[i].PhoneNumber);
                    fclose(pWrite);
             }//End for loop
             
             printf("\nEntries saved to file\n");
         }   
          
    }//End Function
    
    //Retrieve entries from file
    void RetrieveFile()
    {
         char rFileName[50];
         int i;
         
         printf("\nName of file you want to retrieve? ");
         scanf("%s", rFileName);
    
         pRead = fopen("rFileName", "r");
    
         if (pRead == NULL)
         {
         printf("\nFile cannot be opened\n");
         }
         else
         {
             for (i = 0; i < count; i++)
             {
                    fscanf(pWrite, "%s\t%s\t%s\n", lt[i].FirstName, lt[i].LastName, lt[i].PhoneNumber);
                    printf("\n%s\t%s\t%s", lt[i].FirstName, lt[i].LastName, lt[i].PhoneNumber);
                    
             }//End for loop
         }    
    }//End Function

  2. #2
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Get rid of the quotes around the file name variable every time you try to open a file. It is trying to open file rFileName not what's stored in that variable.

    Code:
    pRead = fopen(rFileName, "r");

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    8
    That got it working, thanks man.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trouble with .h and .cpp files
    By vincible in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2010, 11:55 PM
  2. Having trouble with getting input from files.
    By Matsuya in forum C++ Programming
    Replies: 4
    Last Post: 05-26-2009, 10:56 AM
  3. Trouble with linking files
    By magda3227 in forum C Programming
    Replies: 11
    Last Post: 06-18-2008, 01:00 AM
  4. Trouble sharing files between processes.
    By Yasir_Malik in forum Linux Programming
    Replies: 8
    Last Post: 09-16-2003, 02:08 PM
  5. Having some trouble with reading from files
    By bluebob in forum C Programming
    Replies: 19
    Last Post: 04-18-2002, 05:29 AM