I have written a code to append a file, tried different procedures, but problem is second file pointer cant' open a file.
Any solutions?
------------
void readfile()
{
char *error_file;
static char contents[LINESIZE];
fp =fopen(source_dir, "r");
fp1=fopen(dest_dir, "a"); /* problem lies here, it never open a file*/
if (fp && fp1 != NULL)
{
while ( get_item(contents) != EOF) /* stop at EOF */
{
printf("%s", contents);
fputs(contents,fp1);
}
}
else
if (fp==NULL) error_file = source_dir;
else if (fp1==NULL) error_file = dest_dir;
printf ("Error in opening %s", error_file);
fclose (fp);
fclose (fp1);
}
1. source_dir and dest_dir are names of variables which holds the complete names of files.
2. First line opens file for reading successfully, but the second one fails (in all modes).
3. Files for both pointers exists in its respective directory.
4. The drive is neither wirte-protected nor full.
The error was name of desination directory, i.e. c:\destination = 11 char
and the file naming do not allow more than 8 char in a file or a directory name, so the destination directory name was actually "c:\destin~1" and hence fp1 was unable to open a file.