Thread: help withcreating sequential text file

  1. #31
    Registered User
    Join Date
    May 2008
    Posts
    72
    i have one last problem this day which is with the fread or printf :
    Code:
     #include <stdio.h>
    #include  <conio.h>
    #include  <string.h>
    typedef struct account
    
    {
         char *name;
         char *password;
         char *previleges;   
            
            
            
            }account;
    void account_write(char *);        
    void fill_account(account);
    void account_read(char *);
    
    
    
    
    
    int main(){
        account c;
         char *filename="account.txt";
         account_write(filename);
         account_read(filename);
        
        getch();
        return 0;
        
        }
        
    void fill_account(account *c)
    {
         printf("give the name of account");
         scanf("&#37;s",&c->name);
         printf("give the password");
         scanf("%s",&c->password);
         printf("give previliges");
         scanf("%s",&c->previleges);
         
         
    
         }
    
    void account_write(char *filename)
    {
         
        FILE  *file=fopen(filename,"ab");
        if(file!=NULL)
          { 
            account temp;
            fill_account(&temp);
            fwrite(&temp,sizeof (temp),1,file);
            
          
                      
                      
              } 
              
           fclose(file) ; 
     
         
         
         
         
         }
    
    void account_read(char *filename)
       {
    
         FILE *file=fopen(filename,"rb");
         if(file!=NULL)
         { printf("f");
           account c;          
           fread(&c,sizeof (c),1,file);   
                      
                    printf("%s, %s, %s",c.name,c.password,c.previleges );  
                       
         
         
         
         }
         fclose(file);
         }
    this progra is supposed to read what we add as an account but still it crashes hope anyone can help with this

  2. #32
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There is no room to write to name, password, or privileges -- they are just pointers and you never give them anywhere to point to.

    Note also that fwriting/freading pointers is a Bad Idea, since when your program stops and re-starts and tries to read in data, all that the file contains is the memory address where it used to live, and no actual data.

  3. #33
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I have a memory manager that I wrote one time that saves memory blocks in a file. If you do something similar to that, you could safely write out pointers. Long story short, tabstop is pointing out simply that what you are doing is probably different than what you are desiring to do.

  4. #34
    Registered User
    Join Date
    May 2008
    Posts
    72
    should i give up using structures as pointers and chose them, as strings?

  5. #35
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    In this case, maybe. If they are a fixed size you have to be writing them in a fixed size in order to properly fread() them.

    Otherwise you will have to parse the file. I guess it really depends on how you intend to read stuff back into your program. Given your level of experience programming (and I am making assumptions at this point)

    Example:
    Code:
    /* You don't want magic numbers */
    #define FIELD_SIZE  32;
    
    /* This is just me getting lazy since I don't want to rewrite too much of your code right now. */
    typedef char field[FIELD_SIZE];
    
    typedef struct account
    {
      /* These are all arrays, don't worry */
      field name, password, previleges;   
    }account;
    Now you can read and write these just using fread() and fwrite() respectively. Obviously this isn't the best way to secure your data. But I doubt that is your objective for the time being.

  6. #36
    Registered User
    Join Date
    May 2008
    Posts
    72
    please can anyone look at the problem of this program

    Code:
    #include <stdio.h>
    #include  <conio.h>
    #include  <string.h>
    typedef struct account
    
    {
         char name[20];
         char password[20];
         char previleges[20];   
            
            
            
            }account;
    void account_write(char *);        
    void fill_account(account);
    void account_search(char *);
    void print_account(account [],int);
    void view_account(char *,int n);
    void print_file(account [],int);
    void account_delete(char *);
    
    int main(){
        account c;
        int n;
        char *filename="account.txt";
        
       // account_write(filename);
        
         //account_search(filename);
        //view_account(filename,n);
         account_delete(filename);  
        getch();
        return 0;
        
        }
        
    void fill_account(account *c)
    {
         printf("give the name of account");
         scanf("&#37;s",&c->name);
         printf("give the password");
         scanf("%s",&c->password);
         printf("give previliges");
         scanf("%s",&c->previleges);
         
         
    
         }
    
    void account_write(char *filename)
    {
         
        FILE  *file=fopen(filename,"ab");
        fflush(file);
        if(file!=NULL)
          { 
            account temp;
            fill_account(&temp);
            fwrite(&temp,sizeof (temp),1,file);
            
          
                      
                      
              } 
              
           fclose(file) ; 
     
         
         
         
         
         }
    void account_search(char *filename)
       {int count=0,i=0; 
         char NAME[20];
         account t[20];
         FILE *file=fopen(filename,"rb");
         if(file!=NULL)
         {
                         
                                        
                              while(fread(&t[i],sizeof (account),1,file)==1)   
                                {   
                                    i++;                           
                                    count++; 
                                }                
         
                         
         
         }
         printf("give the name of account");
         scanf("%s",&NAME);
         for(int j=0;j<count;j++)
          {
             if(strcmp(t[j].name,NAME)==0)
               printf("%s \t %s\t %s \t",t[j].name,t[j].password,t[j].previleges);          
                  
                  }
              fclose(file);
        
         }
    
         
    void account_delete(char *filename)
    {
         
         
         
         int count=0,i=0; 
         char NAME[20];
         account t[20];
         FILE *file=fopen(filename,"wb");
         if(file!=NULL)
         {
                         
                                        
                              while(fread(&t[i],sizeof (account),1,file)==1)   
                                {   
                                    i++;                           
                                    count++; 
                                }                
         
                         
         
         }
         printf("give the name of account");
         scanf("%s",&NAME);
         for(int g=0;g<count;g++)
           {if(strcmp(t[g].name,NAME)==0)
               {  for(int h=g;h<count;h++)
                    {
                         t[h]=t[h+1];
                        }
                }    
            }  
            count--;  
              print_file(t,count);
             
              fclose(file);                                          
         
         
         
         }
    void print_file(account t[30],int n)
    {
         for(int i=0;i<n;i++)
        { printf("%s",t[i].name); }    
         }

  7. #37
    Registered User
    Join Date
    May 2008
    Posts
    72
    Code:
    void account_delete(char *filename)
    {
         
         
         
         int count=0,i=0; 
         char NAME[20];
         account t[20];
         FILE *file=fopen(filename,"wb");
         if(file!=NULL)
         {
                         
                                        
                              while(fread(&t[i],sizeof (account),1,file)==1)   
                                {   
                                    i++;                           
                                    count++; 
                                }                
         
                         
         
         }
         printf("give the name of account");
         scanf("&#37;s",&NAME);
         for(int g=0;g<count;g++)
           {if(strcmp(t[g].name,NAME)==0)
               {  for(int h=g;h<count;h++)
                    {
                         t[h]=t[h+1];
                        }
                }    
            }  
            count--;  
              print_file(t,count);
             
              fclose(file);
    this ffunction has a role of deleting the found account but it has two problems: to delete from the string t[] and to overwrite/write in the file

  8. #38
    Registered User
    Join Date
    May 2008
    Posts
    72
    please help

  9. #39
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And the symptoms of this problem are what, exactly?

  10. #40
    Registered User
    Join Date
    May 2008
    Posts
    72
    the delete doesn't work i wrote the problems: can't write to file and can't delete from t[]

  11. #41
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    One bug is this:
    Code:
    scanf("&#37;s",&NAME);
    Common mistake for some reason. Remove the &. NAME is already a pointer. And rename NAME to name. Don't use capitals for variable names. Nothing wrong but it is bad style

    I see you do this also in the rest of the code. Check your scanf(). The parameter has to have a type of char*. If you have an array like "char array[10]" then array is char*
    Last edited by C_ntua; 09-25-2008 at 04:17 PM.

  12. #42
    Registered User
    Join Date
    May 2008
    Posts
    72

    the problem is that i want to delete an account using this code any help?

    Quote Originally Posted by overlord21 View Post
    Code:
    void account_delete(char *filename)
    {
         
         
         
         int count=0,i=0; 
         char NAME[20];
         account t[20];
         FILE *file=fopen(filename,"wb");
         if(file!=NULL)
         {
                         
                                        
                              while(fread(&t[i],sizeof (account),1,file)==1)   
                                {   
                                    i++;                           
                                    count++; 
                                }                
         
                         
         
         }
         printf("give the name of account");
         scanf("%s",&NAME);
         for(int g=0;g<count;g++)
           {if(strcmp(t[g].name,NAME)==0)
               {  for(int h=g;h<count;h++)
                    {
                         t[h]=t[h+1];
                        }
                }    
            }  
            count--;  
              print_file(t,count);
             
              fclose(file);
    this ffunction has a role of deleting the found account but it has two problems: to delete from the string t[] and to overwrite/write in the file
    the problem is that i want to delete an account using this code any help?

  13. #43
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why do you think you can read from a file opened in mode "wb"?

  14. #44
    Registered User
    Join Date
    May 2008
    Posts
    72
    i'm required to read and write in a binary file

  15. #45
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by overlord21 View Post
    i'm required to read and write in a binary file
    Good for you. Have you bothered to look up fopen yet? Mode wb:
    truncate to zero length or create binary file for writing
    If your file is now zero bytes long, how many records do you think you can read from it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM

Tags for this Thread