![]() |
| | #31 |
| Registered User Join Date: May 2008
Posts: 70
| 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);
}
|
| overlord21 is offline | |
| | #32 |
| and the Hat of Guessing 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 | |
| | #33 |
| Banned 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 | |
| | #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 | |
| | #35 |
| Banned 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;
|
| master5001 is offline | |
| | #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 | |
| | #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);
|
| overlord21 is offline | |
| | #38 |
| Registered User Join Date: May 2008
Posts: 70
| please help |
| overlord21 is offline | |
| | #39 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| And the symptoms of this problem are what, exactly? |
| tabstop is offline | |
| | #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 | |
| | #41 |
| Registered User Join Date: Jun 2008
Posts: 1,134
| One bug is this: Code: scanf("%s",&NAME);
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 | |
| | #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:
| |
| overlord21 is offline | |
| | #43 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Why do you think you can read from a file opened in mode "wb"? |
| tabstop is offline | |
| | #44 |
| Registered User Join Date: May 2008
Posts: 70
| i'm required to read and write in a binary file |
| overlord21 is offline | |
| | #45 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Good for you. Have you bothered to look up fopen yet? Mode wb: Quote:
| |
| tabstop is offline | |
![]() |
| Tags |
| files, program impleùmentation, random, sequential |
| Thread Tools | |
| Display Modes | |
|
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 |