Subj : Re: fstream arrays To : borland.public.cpp.borlandcpp From : "D Kat" Date : Wed Mar 10 2004 12:49 pm Can you give me info on Memcheck? 9 times out of ten any of my problems come down to buffer overwrites (one of the first things I thoroughly checked on this issue...). I really think C is a beautiful language EXCEPT for it being perfectly happy to let you write beyond assigned space.... DKat "Dwayne" wrote in message news:403d1775$1@newsgroups.borland.com... > Hello D Kat, > /////////////////////CODE//////////////////////////////// > ofstream out_files[NSUB];//file to write data to > get_subj_fp(int file) > { > char name[20]; > > strcpy(name,"data/"); > strcat(name, sub_name[file]); > strcat(name,"."); > strcat(name,cdn); > out_files[file].open(name); > if (!out_files[file]) > { > cerr << "\nerror on file " << sub_name[file] << "\n"; > return(1); > } > return(0); > } > > > A few other things. > > 1. Clear the variable "Name". Assign it to all blanks I realize that > strcpy puts a NULL terminating character on the end of the copy, but it may > help in the debugging. > 2. make sure you use "Forward Slashes", not back slashes. in the "copy" and > "Cat" commands. That way the compiler will not think it is a "special" > character... like \0. or \\. > 3. This may sound crazy, but it may solve your problem.... Build your > string with a strins command. Do not use the Strcat. You may be building > it backwards from what you are doing, but hey! the result is the same.... > 4. Are you using something like Memcheck? There is a very good chance that > you are overwriting your memory someplace. and you may not be seeing it > until now. Strcat is a dangerous command for overwriting memory if you are > not careful. > > strcpy(name,"data/"); > strcat(name, sub_name[file]); > strcat(name,"."); > strcat(name,cdn); > > changes to: > strcpy(name,cdn); > strins(".",name,0); > strins("sub_name[file],name,0); > strins("data/",name,0); > > Here is a source for a strins.... > > int strins(char *r, char *s, int p) > { > int c,d; > d=0; > c=-1; > while(d { > for(c=strlen(s);c>=p+d;c--) > { > *(s+c+1)=*(s+c); > } > *(s+p+d)=*(r+d); > d++; > c=0; > } > return(c); > }; > > .