fwrite problem

Hello I have three courses files which are eng101.txt fiz101.txt and mat101.txt

Every file includes the student names who are attending these courses:
For ex:

mat101.txt
MAT101 MATHEMATICS1
1001001 MUHAMMET DORUK
1001002 ADEM SANLIALP
1001003 NAZIFE SAHIN
1001004 OZGUR SEKER

eng101.txt
ENG101 ENGLISH1
1001064 SUMEYYE KUBRA SOGUT
1001065 EYUP SUKAN
1001066 NAZIRE BELKIN
1001067 SIBEL UYSAL

fiz101.txt
FIZ101 PHYSICS1
1001001 MUHAMMET DORUK
1001007 DILEK ERKMEN
1001011 SEMRA DOGRU
1001012 AYGUL YILMAZ
1001013 EMEL ARI

I'm trying to integrate (build a plural linked list) these course files as final.txt like this:
1001001 MUHAMMET DORUK MAT101 MATHEMATICS1 FIZ101 PHYSICS1
1001002 ADEM SANLIALP MAT101 MATHEMATICS1
....
And I did it mostly but I have a problem

I'm using strcpy and strcat functions to seperate names, surnames, student numbers, course names and course codes...

My "read from source files" function is like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct register{
char no[8];
char name[30];
char surname[30];
char courseC[10];
char courseN[50];
};
char sentence[100];
char* c;
FILE *book;
register reg;
c=fgets(sentence,100,book);
pc = strtok (book," ,.-	");
while (pc != NULL)
{strcpy(reg.no,pc); }
pc = strtok (NULL, " ,.-	");
}//end of while 


When I watch the results on the screen, there is no problem..
But When I try to integrate them in another txt file which is final.txt

The results are like this:

1001001 MUHAMMET ÿ( ÕŒ•vÍ>È àı~øş( jDORUK È;@ €½F Àş( {iD `ş( Èş( MAT101  $ğMATHEMATICS1
 È;@ ¼F àş( ËjD ş( øş( ˆ~F À@G 
   1001001 MUHAMMET ÿ( ÕŒ•vÍ>È àı~øş( jDORUK È;@ €½F Àş( {iD `ş( Èş( FIZ101  $ğPHYSICS1
 S1
 È;@ ¼F àş( ËjD ş( øş( ˆ~F À@G 
   1001064 SUMEYYE KUBRA •vÍ>È àı~øş( jSOGUT È;@ €½F Àş( {iD `ş( Èş( ENG101  $ğENGLISH1
 S1
 È;@ ¼F àş( ËjD ş( øş( ˆ~F À@G 


I think this happens cos of NULL char.

Here is also my write function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
void List::write(){
struct register{
char no[8];
char name[30];
char surname[30];
char courseC[10];
char courseN[50];
};

register reg;
student names;
course *n;
if (!(book=fopen("final.txt","w+"))){
cerr<<"The file couldnt open"<<endl;return;

}
names=start;
while(names){
n=names->coursepoint;
while(n){
strcpy(reg.no,names->stdno);
strcpy(reg.surname,names->stdsurname);
strcpy(reg.name,names->stdname);
fwrite(&reg,sizeof(register),1,book);
n=n->next;
}
names=names->next;

}
fclose(book);
}


So how can I put a NULL char at end of a word?
Or maybe u think the problem caused by something else then please tell me...
Last edited on
anyone?
register is a C++ keyword so you can't use it as a struct name.

Put a null char at the start of the stringsreg.no[0] = '\0';

In your read function you never open the file and it doesn't look like you passing correct arguments to strtok. The first argument should be a char*.
hmm...
Topic archived. No new replies allowed.