Like Tree1Likes
  • 1 Post By AndiPersti

C Programming, I am new learning C, program using fread, fwrite, fseek

This is a discussion on C Programming, I am new learning C, program using fread, fwrite, fseek within the C Programming forums, part of the General Programming Boards category; I need help writing a program in C that records my monthly expenses, and list my monthly expenses, and also ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    1

    C Programming, I am new learning C, program using fread, fwrite, fseek

    I need help writing a program in C that records my monthly expenses, and list my monthly expenses, and also I can delete an item that is recorded on file, so far I am able to record and view the file, but I am unable to delete, and when it records on file, it is doubling the recording. Please help!

    Flavio

    Code below of what I got so far

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <process.h>
    
     void Expense_type (int);
     
     void Expense_type2 (int option);
     
     int main (void)
     {
         int option;
         
         printf("\n Please choose an option below\n\n"
             "\n (1) Launch income / expense \n"
             "\n (2) Cancel Expense || Edit entry \n"
             "\n (3) See types of launch\n"
             "\n (4) Register types of launch\n"
             "\n (0) Exit\n");
             scanf("%d", &option);
             
             if (option > 0 && option <= 5)
              {
                 Expense_type(option);
                 
                 }
              else 
                
                printf("\nOption invalid!");
                
                system ("pause");
                
                return 0;
                
    }
    
          void Expense_type (int option)
          {
               int option2, found = 0;
               
               char purchase[20];
               
               float amount, sum;
               
               FILE *p;
               
               while (option == 1)
               {
                     printf("\nPlease choose an option to add your Expenses:\n\n"
                        "\n (1) Include \n"
                        "\n (2) Make a change \n"
                        "\n (3) Exclude an item \n"
                        "\n (4) Consult \n"
                        "\n (5) List all  \n"
                        "\n (0) Exit \n\n");
                     scanf("%d", &option2);
               
       // *******************************************************************************************************
                
                     if (option2 == 1)
                     {
                                 
                       p = fopen("CADASTRO.DAT","a+"); /* apend file (add text to
                        a file or create a file if it does not exist.*/
                        if (!p)
                        {
                           printf("File could not be opened\n\n");
                                  }
                                  
                        printf("\n Purchase Type: ");
                        scanf("%s", purchase);
                        
                        printf("\n How much: ");
                        scanf("%f", &amount);
                     
                     fprintf(p, "%s\t%f\n", purchase, amount);
                     fclose (p);
                     
                     } // Option 1  if statement 
                    
        //***************************************************************************************************************       
                     
                 if (option2 == 2)
                       
                       {
                              p = fopen("CADASTRO.DAT","a+"); /* apend file (add text to
                        a file or create a file if it does not exist.*/
                        if (!p)
                        {
                           printf("File could not be opened\n\n");
                                  }
                          
                         // ???? Como alterar o file???
                         
                         
                         } // chave que fecha option2 == 2
                         
        //*********************************************************************************************************************                
                   if (option == 3)
                    {
                                 p = fopen("CADASTRO.DAT","a+"); /* apend file (add text to
                        a file or create a file if it does not exist.*/
                        if (!p)
                        {
                           printf("File could not be opened\n\n");
                                  }
                                  
                                  
                        // Excluir algo do arquivo 
                        
                        
                        } // chave que fecha option == 3
                        
       //**************************************************************************************************************************                 
                  if (option == 4)
                    {
                                 p = fopen("CADASTRO.DAT","a+"); /* apend file (add text to
                        a file or create a file if it does not exist.*/
                        if (!p)
                        {
                           printf("File could not be opened\n\n");
                                  }
                    
                    
                      //Consultar o arquivo CADASTRO.DAT 
                      
                      
                      } // chave que fecha option 4
                      
                       
          //*****************************************************************************************             
                     
                  if (option2 == 5)
                     {
                       p = fopen("CADASTRO.DAT","r"); // open file para listar tudo     
                        
                       if (p == NULL)
                       {
                             perror("\nError when trying to open file CADASTRO.DAT\n");
                             
                             }
                        else      
                     
                               while (!feof(p) )
                               {                   
                                  fscanf(p, "%s ", purchase);
                                  fscanf(p, "%f", &amount);
                                  
                                  sum += amount;
                                 
                                  printf("\n Tipo de Compra: %s  Valor R$: %.2f \n", purchase, amount);
                                                 
                                  }
                             
                              printf("\n Total Value: %.2f \n", sum);
                           
                           
                           fclose(p);
                        
                       }
                       
          //***************************************************************************************************************
                 
                 if (option2 == 0)
                      {
                                  exit (0) ;
                                  }                  
                 
                    
                    }  //bracket do while
                    
          } // Close function number 2

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Your indentation sucks.

    If you don't post code which is easy to read, many people here will ignore you.

    Bye, Andreas
    Last edited by AndiPersti; 11-23-2012 at 02:46 PM. Reason: Forgot to add link
    xeon321 likes this.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    It doesn't look like a complete program. I don't see where you attempt to delete a record. I don't understand how you would delete a record in a file. What is you method?

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    82
    You sure bro?

    line 47
    Code:
     while(option == 1) {

    line 170
    Code:
    }  //bracket do while
    Are those 2 lines suppose to be option2 or option?

    line 99
    Code:
    if(option == 3)
    line 115
    Code:
    if(option == 4)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fread, fwrite
    By thescratchy in forum C Programming
    Replies: 2
    Last Post: 03-17-2010, 10:01 AM
  2. fread & fseek not advancing buffer
    By RobotGymnast in forum C++ Programming
    Replies: 6
    Last Post: 10-28-2008, 05:38 PM
  3. Advantages of fseek() and fread() ?
    By DarrenY in forum C Programming
    Replies: 7
    Last Post: 04-03-2007, 01:26 PM
  4. help with fseek and fwrite
    By monkee in forum C Programming
    Replies: 6
    Last Post: 04-30-2004, 07:00 PM
  5. fwrite and fread
    By condorx in forum C Programming
    Replies: 5
    Last Post: 06-08-2002, 01:01 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21