Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

file reading help (sort of)

Status
Not open for further replies.

stevebiks

Programmer
Feb 13, 2005
10
US
hey well this is my first time psoting on one ot these things, so bear with me. I am very new to C (only about three to four months), so take it easy if my question is really dumb or has been asked a lot!!

anyway im am trying to see if i can read a file from one directory (im using Unix) and then copy it's contents into a file that is located in a different directory. Anyway this program works perfectly if the two files are in the same dirrectory, but no when they are seperate. What i need to know is how do you access another directory? i have tryed calling the inFile with read("/diffdirectory/text_file_i_need", "r") and in my out file it is just empty. so if anyone can help i would appreciate it!!! ill attach my code just in case...

-stevebiks


#include <stdio.h>

int main()
{
int outFile;
FILE *fid;
int len;
char c;
char *outFileName="out_test";

outFile = open(outFileName, "w");

fid=fopen("/diffdirectory/test_in", "r");
while((len=read(fid, &c, 1))>0)
write(outFile, &c, 1);

}
 
Well, if the out_test file is being created successfully, then the problem is probably in reading the test_in file. To get a clue as to what the error is, you should check the return value of your fopen command. If it is NULL, then the file was not opened, probably due to permissions or non-existence or something. Here's a quick example:
Code:
#include <errno.h>
...
if ((fid = fopen("/diffdirectory/test_in", "r")) == NULL)
   {
      fprintf(stderr, strerror(errno));
      return 1;
   }
...
 
Use absolute paths and check for errors when you try
to open the file. Your call to open is wrong.
Also why are you mixing stdio and standard unix i/o?

trysomething like this-
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define BSZ 256
#define VERBOSE
#define OFLAGS (O_WRONLY | O_CREAT | O_APPEND)
#define ERR(str) {printf("ERROR: %s\n",str); exit(1);}
#define makereadable(dst) {chmod(dst,(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));}

const char destination[] = "/home/mars/foo.txt";



int
main (int argc, char **argv)
{
  int fd, p;
  char buf[BSZ];
  FILE *the_open;
  if (argc != 2)
    {
      ERR ("Please specify a source file");
    }
  unlink (destination);
  if ((fd = open (destination, OFLAGS)) < 0)
    {
      perror ("open()");
      return -1;
    }
  if ((the_open = fopen (argv[1], "r")) == NULL)
    {
      perror ("fopen()");
      return -1;
    }
  bzero (buf, BSZ);
  while ((fgets (buf, BSZ, the_open)) != NULL)
    {
      p = write (fd, buf, strlen (buf));
#ifdef VERBOSE
      printf ("Wrote %d bytes to file at %d\n", p, fd);
#endif
    }
  makereadable (destination);
  fclose (the_open);
  close (fd);
  return 0;
}

HTH
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top