how to append a file?

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);
}

You're using fopen() incorrectly. It should be:

1
2
fp =fopen("source_dir.txt", "r"); // unless you're just using source_dir for example purposes
fp1=fopen("dest_dir.txt", "a");


Also, "r" unlike "a" does not create a file if the given file does not already exist.

Lastly, are you aware that if (fp && fp1 != NULL) is very different from if (fp != NULL && fp1 != NULL)?
Last edited on
Thanks Mcleano.

For your information :

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.

Any other suggestions.

Thanks.
You'll have to bare with me because my knowledge lies much greater in the c++.

Firstly try changing your code to this:

1
2
3
4
5
6
7
      fp =fopen(source_dir, "r");
      if(fp==NULL)
            printf("Problem with source_dir\n");

      fp1=fopen(dest_dir, "a");
      if(fp1==NULL)
            printf("Problem with dest_dir\n");


You should always check straight away whether there is a problem or not.
You can use any combination, but the result is same.

"destination file can not be opened"

Problem lies in fp1=fopen(dest_dir,"a");



Have you tried running your code through the debugger and seen what the value of dest_dir is when it is being used in the function?
how to check the value of dest_dir using debugger?

I have used debugger, but value is shown nowhere.
Where is the problem?

I have abandoned everything else, reduced the code to following, but the result is same; fp1 can not open file. Why?

confirmations : 1.csv exists in "C:\Source" and "C:\Destination".

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
main()
{

FILE *fp, *fp1;

fp = fopen("c:/source/1.csv","r");
if (fp == NULL) printf("\nError opening fp ");
else printf("\nFile opened successfully fp");

fp1 = fopen("c:/destination/1.csv","a");
if (fp1 == NULL) printf("\nError opening fp1");
else printf("\nFile opened successfully fp1");
getch();
return(0);
}

If anybody can help me?
Hmmm that is really odd :S I don't think this will be the problem but try closing fp before opening fp1.
The problem is solved.

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.

Anyway thanks for the help.
Topic archived. No new replies allowed.