C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-24-2008, 02:27 PM   #31
Registered User
 
Join Date: May 2008
Posts: 70
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("%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
overlord21 is offline   Reply With Quote
Old 09-24-2008, 02:44 PM   #32
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 09-24-2008, 03:31 PM   #33
Banned
 
master5001's Avatar
 
Join Date: Aug 2001
Location: Visalia, CA, USA
Posts: 3,699
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.
master5001 is offline   Reply With Quote
Old 09-24-2008, 04:25 PM   #34
Registered User
 
Join Date: May 2008
Posts: 70
should i give up using structures as pointers and chose them, as strings?
overlord21 is offline   Reply With Quote
Old 09-24-2008, 04:33 PM   #35
Banned
 
master5001's Avatar
 
Join Date: Aug 2001
Location: Visalia, CA, USA
Posts: 3,699
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.
master5001 is offline   Reply With Quote
Old 09-25-2008, 02:29 PM   #36
Registered User
 
Join Date: May 2008
Posts: 70
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("%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); }    
     }
overlord21 is offline   Reply With Quote
Old 09-25-2008, 02:31 PM   #37
Registered User
 
Join Date: May 2008
Posts: 70
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
overlord21 is offline   Reply With Quote
Old 09-25-2008, 02:31 PM   #38
Registered User
 
Join Date: May 2008
Posts: 70
please help
overlord21 is offline   Reply With Quote
Old 09-25-2008, 03:27 PM   #39
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
And the symptoms of this problem are what, exactly?
tabstop is offline   Reply With Quote
Old 09-25-2008, 03:44 PM   #40
Registered User
 
Join Date: May 2008
Posts: 70
the delete doesn't work i wrote the problems: can't write to file and can't delete from t[]
overlord21 is offline   Reply With Quote
Old 09-25-2008, 04:10 PM   #41
Registered User
 
C_ntua's Avatar
 
Join Date: Jun 2008
Posts: 1,134
One bug is this:
Code:
scanf("%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.
C_ntua is offline   Reply With Quote
Old 09-25-2008, 04:17 PM   #42
Registered User
 
Join Date: May 2008
Posts: 70
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?
overlord21 is offline   Reply With Quote
Old 09-25-2008, 04:28 PM   #43
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Why do you think you can read from a file opened in mode "wb"?
tabstop is offline   Reply With Quote
Old 09-25-2008, 04:37 PM   #44
Registered User
 
Join Date: May 2008
Posts: 70
i'm required to read and write in a binary file
overlord21 is offline   Reply With Quote
Old 09-25-2008, 04:43 PM   #45
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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:
Quote:
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?
tabstop is offline   Reply With Quote
Reply

Tags
files, program impleùmentation, random, sequential

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 07:09 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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