| | |
file problem
![]() |
•
•
Join Date: Jun 2009
Posts: 14
Reputation:
Solved Threads: 0
Thanks It opened when I put run again I have another problem I'm using two linked lists and I opened two files for each one but when I write list to file It writes both lists in the two files .
here is the code
void save (school *c,school *m)
{
fpta=fopen("school.txt", "w");
fptb=fopen("school1.txt", "w");
c=head;
m=head;
while(c!=NULL)
{
fprintf(fpta,"\n%d\n%s\n%s\n%d\n%s\n%d\n%f",c->id,c->name,c->address,
c->phone,c->sex,c->level,c->average);
c=c->next;
}
fclose(fpta);
printf("\nFile one has been saved");
while (m!=NULL)
{
fprintf(fptb,"\n%d\n%s\n%s\n%d\n%s\n%d\n%f",m->id,m->name,m->address,
m->phone,m->sex,m->level,m->average);
m=m->next;
}
fclose(fptb);
printf("\nFile two has been saved");
getch();
}
here is the code
void save (school *c,school *m)
{
fpta=fopen("school.txt", "w");
fptb=fopen("school1.txt", "w");
c=head;
m=head;
while(c!=NULL)
{
fprintf(fpta,"\n%d\n%s\n%s\n%d\n%s\n%d\n%f",c->id,c->name,c->address,
c->phone,c->sex,c->level,c->average);
c=c->next;
}
fclose(fpta);
printf("\nFile one has been saved");
while (m!=NULL)
{
fprintf(fptb,"\n%d\n%s\n%s\n%d\n%s\n%d\n%f",m->id,m->name,m->address,
m->phone,m->sex,m->level,m->average);
m=m->next;
}
fclose(fptb);
printf("\nFile two has been saved");
getch();
}
>I have another problem I'm using two linked lists and I opened two files for each one but when I write list to file It writes both lists in the two files .
You are using c and m as two pointers to mark the two lists right? Thats the reason you are passing them.But why this? by this you are making both c and m point to head so whats the use of passing them to the function ???
Well you have opened your files fpta and fptb in mode "w" write mode and so it would delete all the contents of it next time you open it and write fresh so have a look at it.
You are using c and m as two pointers to mark the two lists right? Thats the reason you are passing them.But why this?
c++ Syntax (Toggle Plain Text)
c=head; m=head;
Well you have opened your files fpta and fptb in mode "w" write mode and so it would delete all the contents of it next time you open it and write fresh so have a look at it.
Last edited by csurfer; Jun 27th, 2009 at 10:59 pm.
I Surf in "C"....
•
•
Join Date: Jun 2009
Posts: 14
Reputation:
Solved Threads: 0
I used a condition according to level number and it worked .but I think the problem is with add function .I'll post the structure and add function .If anyone could correct it.
here is the code
typedef struct school {
int id;
char name[80];
char address[80];
int phone;
char sex[10];
int level;
float average;
struct school *next;
}school;
struct school*last=NULL,*head=NULL;
struct school *student1;//pointer to list one
struct school *student2;//pointer to list two
void add(struct school *one)
{
/*struct*/
one = (school *) malloc (sizeof(school));
printf("\n\t\t Add The Information To system " );
printf("\n\t\t\tID : ");
scanf(" %d", &one->id);
printf("\t\t\tNAME : ");
scanf("%s" , one->name);
printf("\t\t\tAddress : ");
scanf(" %s",one->address);
printf("\t\t\tphone : ");
scanf("%d" , &one->phone);
printf("\t\t\tsex : ");
scanf("%s" , one->sex);
printf("\t\t\tlevel : ");
scanf("%d" , &one->level);
printf("\t\t\taverage : ");
scanf("%f" , &one->average);
one->next = NULL;
if (head == NULL)
{
last = one;
head = one;
}
else
{
last->next = one;
last = one;
}
printf("\n\n******************************");
return;
}
here is the code
typedef struct school {
int id;
char name[80];
char address[80];
int phone;
char sex[10];
int level;
float average;
struct school *next;
}school;
struct school*last=NULL,*head=NULL;
struct school *student1;//pointer to list one
struct school *student2;//pointer to list two
void add(struct school *one)
{
/*struct*/
one = (school *) malloc (sizeof(school));
printf("\n\t\t Add The Information To system " );
printf("\n\t\t\tID : ");
scanf(" %d", &one->id);
printf("\t\t\tNAME : ");
scanf("%s" , one->name);
printf("\t\t\tAddress : ");
scanf(" %s",one->address);
printf("\t\t\tphone : ");
scanf("%d" , &one->phone);
printf("\t\t\tsex : ");
scanf("%s" , one->sex);
printf("\t\t\tlevel : ");
scanf("%d" , &one->level);
printf("\t\t\taverage : ");
scanf("%f" , &one->average);
one->next = NULL;
if (head == NULL)
{
last = one;
head = one;
}
else
{
last->next = one;
last = one;
}
printf("\n\n******************************");
return;
}
sam511,
I think you tried to open a data file with an editor. There are two types of data file - Text and Binary.
Show us your complete code. Don't forget; wrap up source code with BB code tags.
Another post ?
http://www.daniweb.com/forums/thread200229.html
I think you tried to open a data file with an editor. There are two types of data file - Text and Binary.
Show us your complete code. Don't forget; wrap up source code with BB code tags.
Another post ?
http://www.daniweb.com/forums/thread200229.html
Last edited by adatapost; Jun 28th, 2009 at 3:11 am.
![]() |
Similar Threads
- C++ seeking data to file problem (C++)
- Editing a file problem (Python)
- File I/O Problem: Incomplete or Missing Content During Writeline (Python)
- Function & writing to file problem (Python)
- TSP read input file problem (C++)
- New member with a system file problem... (Windows NT / 2000 / XP)
- file problem (C)
- Basic File I/O problem (C++)
- problem in reading a file (Shell Scripting)
Other Threads in the C Forum
- Previous Thread: need help solving basic graphics
- Next Thread: add to two linked lists
Views: 375 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for C
api array arrays binary binarysearch c++ c/c++ char character cheating code coke command conversion convert copy database decimal directory download dude dynamic ebook error exec factorial fgets file fork function functions getline givemetehcodez grade graphics hardware homework i/o input insert int integer lazy line linked linkedlist linux list lists loop malloc matrix memory multi mysql no-effort numbers output path pointer pointers problem process program programming programs read readfile recursion recursive recv recvblocked reverse scan scanf sdl shape single socketprograming socketprogramming sockets sorting spoonfeeding stdin string strings strtok structures student system turbo-c txt unix user variable visualstudio voidmain() whythiscodecausesegmentationfault win32 windows






