Thread: Couple errors please help :-D

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    25

    Couple errors please help :-D

    Error 1: backup.c:54: error: 'stdbackup' was not declared in this scope
    stdbackup declaration: void stdbackup(char* origPath, char* backupPath)
    call to stdbackup: stdbackup(dir_to_copy, strcat(dir_to_copy, "BackStd"));
    dir_to_copy declatartion: char dir_to_copy[MAX_DIR_PATH + 1];
    dir_to_copy assignment: getcwd (dir_to_copy, MAX_DIR_PATH + 1);

    1. Is dir_to_copy supposed to be a char*? Because I tried that and then I just got a bunch of errors saying cannot convert 'char**' to 'char*'.
    2. In the call to stdbackup will having the strcat in there allow me to send the two slightly different paths or will I have to put the new path in a new variable? char or char*?
    3. What do I do to fix that error?




    Error 2: backup.c:151: error: invalid conversion from 'int' to 'FILE*'
    error line: f_from=open(strcat(backupPath, entry->d_name), 777);
    f_from declaration: FILE *f_from;
    backupPath declaration: void sysbackup(char* origPath, char* backupPath) //backupPath is an argument taken in from the function call
    entry declaration: struct dirent *entry;
    entry assignment: entry = readdir (dir)
    dir declaration: DIR *dir;
    dir assignemtn: dir = opendir (".");

    1. Totally confused at what's going on. How do I fix it?
    2. Please don't tell me to change open to fopen, can't, it's homework and we can't use fopen.... have to use the system call




    Please make any suggestions or link to me to anywhere that might help.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    When an array is passed to a function, it passes a pointer to the first element in the array.

    so no, it shouldn't be a char * , unless you want an array of pointers.

    If you could post your code, that might make it easier to tell what problems there are.
    Last edited by robwhit; 03-05-2008 at 08:59 PM.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Quote Originally Posted by JJJIrish05 View Post
    Error 1: backup.c:54: error: 'stdbackup' was not declared in this scope
    stdbackup declaration: void stdbackup(char* origPath, char* backupPath)
    call to stdbackup: stdbackup(dir_to_copy, strcat(dir_to_copy, "BackStd"));
    dir_to_copy declatartion: char dir_to_copy[MAX_DIR_PATH + 1];
    dir_to_copy assignment: getcwd (dir_to_copy, MAX_DIR_PATH + 1);

    1. Is dir_to_copy supposed to be a char*? Because I tried that and then I just got a bunch of errors saying cannot convert 'char**' to 'char*'.
    2. In the call to stdbackup will having the strcat in there allow me to send the two slightly different paths or will I have to put the new path in a new variable? char or char*?
    3. What do I do to fix that error?
    1. It could be a char*, but you'd have to allocate memory. The way you have it now is potentially acceptable (I don't know what all your code looks like so I can't be certain).
    2. No, it won't do what you want. Use another string. The safest method would be to use a char*, allocate enough space based on the length of dir_to_copy plus your backup suffix (plus one for the null character), and write to that new string--I'd suggest sprintf() for this.
    3. I don't know. Not enough information (ie, your code) given. As a guess, make sure your prototype is declared globally (that's not absolutely necessary, but it's how things are done) and that it is declared before you use it. As even more of a guess, it looks like you're using a C++ compiler. You really should use a C compiler for C code.
    Quote Originally Posted by JJJIrish05 View Post
    Error 2: backup.c:151: error: invalid conversion from 'int' to 'FILE*'
    error line: f_from=open(strcat(backupPath, entry->d_name), 777);
    f_from declaration: FILE *f_from;
    backupPath declaration: void sysbackup(char* origPath, char* backupPath) //backupPath is an argument taken in from the function call
    entry declaration: struct dirent *entry;
    entry assignment: entry = readdir (dir)
    dir declaration: DIR *dir;
    dir assignemtn: dir = opendir (".");

    1. Totally confused at what's going on. How do I fix it?
    2. Please don't tell me to change open to fopen, can't, it's homework and we can't use fopen.... have to use the system call
    Look carefully at the prototype for open() (Try "man 2 open" to see the man page). Notice how it does not return a FILE*. C uses FILE* for file I/O, UNIX does not. You've also got the second argument to open() wrong. Read the man page, or your textbook, for more information.

    I'm trying not to directly give you the answers because this is homework.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    25
    my code is rather long... is there a limit at how long of code we're allowed to post? should i just attach a file?

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Generally, I'd post the code unless it's, say, over a thousand lines long. People, myself included, don't like the extra work entailed in clicking on an attachment.

    If you're worried about readability, colouring the code with a syntax highlighter like codeform can be nice.

    What's easier to read? This?
    Code:
    #include <stdio.h>
    
    int main(void) {
        printf("Hello, World!\n");
        return 0;
    }
    Or this?
    Code:
    #include <stdio.h>
    
    int main(void) {
        printf("Hello, World!\n");
        return 0;
    }
    Codeform can be run online here: dwks.theprogrammingsite.com/myprogs/cfonline.htm
    (Yes, it's true, I wrote codeform. So don't take me too seriously. But it really does make a difference for really large programs . . . there are other syntax highlighters around too, if you hate codeform or something.)

    As for limits . . . well, someone once posted the first million or so digits of PI, I think, so if there is a limit it's pretty high.
    Last edited by dwks; 03-05-2008 at 10:23 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    25
    ah..... well actually i did work those errors out.... but what are the system calls equivalent to fgets and fputs?

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You mean read() and write()?

    (BTW: they're POSIX file handling functions.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    25
    ok.... it compiles.... but it seems to be getting hung up somewhere and I can't figure out where..... any good programs to use to figure out where and why its getting hung up? Or anyone willing to run/look at my code and see if they can figure it out?

    Code:
    #include <stdio.h>		// Standard input/output routines    
    #include <dirent.h>		// readdir(), etc.                    
    #include <sys/stat.h>		// stat(), etc.                       
    #include <string.h>		// strstr(), etc.                     
    #include <unistd.h>		// getcwd(), etc.   
    #include <sys/types.h>
    #include <fcntl.h>
    
    #define MAX_DIR_PATH 1024 // Maximal full path length we support
    
    
    
    void 
    sysbackup(char* origPath, char* backupPath)
    {
      DIR *dir;			// Pointer to the scanned directory
      struct dirent *entry;		//  Pointer to one directory entry
      struct dirent *lastentry;
      char cwd[MAX_DIR_PATH + 1];	// Current working directory
      struct stat dir_stat;		// Used by stat()
      int f_from;			//stream of source file
      int f_to;			//stream of target file
      char *buf[MAX_DIR_PATH+1];   // input buffer
    
      // First, save the path of current working directory 
    
      printf("test \n");
       
      if (!getcwd (cwd, MAX_DIR_PATH + 1))
      {
    	  perror ("getcwd:");
          return;
      }
    
    
      // Open the directory to read
      
      dir = opendir (".");
      if (!dir)
      {
    	  fprintf (stderr, "Cannot read directory '&#37;s': ", cwd);
          perror ("");
          return;
      }
    
      /*=======================================================
       * 
       * Scan the directory, traversing each sub-directory, and each file / directory name. 
       *
       ========================================================*/
    
      while ((entry = readdir (dir)))
      {
    	  printf("while \n");
          // Check if the given entry is a directory
          if (stat (entry->d_name, &dir_stat) == -1)
    	  {
    		  perror ("stat:");
    		  continue;
    	  }
          
          // Skip the "." and ".." entries, to avoid loops
          
          if (strcmp (entry->d_name, ".") == 0)
    		  continue;
          
          if (strcmp (entry->d_name, "..") == 0)
    		  continue;
          
          /* Is this a directory? */
          if (S_ISDIR (dir_stat.st_mode))
    	  {
    		  printf("isdir \n");
    		    lastentry=entry;
    		    /* Change into the new directory */
    			if (chdir (entry->d_name) == -1)
    			{
    				fprintf (stderr, "Cannot chdir into '%s': ", entry->d_name);
    			    perror ("");
    			    continue;
    			}
    
    			mkdir(strcat(backupPath, lastentry->d_name), 777);
    	  
    	  		/* check this directory */
    			sysbackup(strcat(origPath, lastentry->d_name), backupPath);
    
    	        /* Finally, restore the original working directory. */
    			if (chdir ("..") == -1)
    			{
    				fprintf (stderr, "Cannot chdir back to '%s': ", cwd);
    			    perror ("");
    			    return;
    			}
    	  } else //if (S_ISREG (dir_stat.st_mode)) //If not directory assume its a file
    	  {
    		  printf("isreg \n");
    		  f_from=open(strcat(origPath, entry->d_name), 777);
    		  if (!f_from) 
    		  {
    			  fprintf(stderr, "Cannot open source file1: ");
    			  perror("");
    			  return;
    		  }
    		  f_to=creat(strcat(backupPath, entry->d_name), 777);
    		  if (!f_to) 
    		  {
    			  fprintf(stderr, "Cannot open target file: ");
    			  perror("");
    			  return;
    		  }
    		  while (read(f_from, buf, MAX_DIR_PATH+1))
    		  {
    			  if (write(f_to, buf, MAX_DIR_PATH+1) == EOF)
    			  {  /* error writing data */
    				  fprintf(stderr, "Error writing to target file2: %d:", f_to);
    				  perror("");
    				  return;
    			  }
    		  }
    
    	  }
      }
    }// End sysbackup()
    
    void 
    stdbackup(char* origPath, char* backupPath)
    {
      DIR *dir;			// Pointer to the scanned directory
      struct dirent *entry;		//  Pointer to one directory entry
      struct dirent *lastentry;
      char cwd[MAX_DIR_PATH + 1];	// Current working directory
      struct stat dir_stat;		// Used by stat()
      FILE* f_from;			//stream of source file
      FILE* f_to;			//stream of target file
      char buf[MAX_DIR_PATH+1];   // input buffer
    
      // First, save the path of current working directory 
       
      if (!getcwd (cwd, MAX_DIR_PATH + 1))
      {
    	  perror ("getcwd:");
          return;
      }
    
    
      // Open the directory to read
      
      dir = opendir (".");
      if (!dir)
      {
    	  fprintf (stderr, "Cannot read directory '%s': ", cwd);
          perror ("");
          return;
      }
    
      /*=======================================================
       * 
       * Scan the directory, traversing each sub-directory, and each file / directory name. 
       *
       ========================================================*/
    
      while ((entry = readdir (dir)))
      {
          // Check if the given entry is a directory
          if (stat (entry->d_name, &dir_stat) == -1)
    	  {
    		  perror ("stat:");
    		  continue;
    	  }
          
          // Skip the "." and ".." entries, to avoid loops
          
          if (strcmp (entry->d_name, ".") == 0)
    		  continue;
          
          if (strcmp (entry->d_name, "..") == 0)
    		  continue;
          
          /* Is this a directory? */
          if (S_ISDIR (dir_stat.st_mode))
    	  {
    		    lastentry=entry;
    		    /* Change into the new directory */
    			if (chdir (entry->d_name) == -1)
    			{
    				fprintf (stderr, "Cannot chdir into '%s': ", entry->d_name);
    			    perror ("");
    			    continue;
    			}
    
    			mkdir(strcat(backupPath, lastentry->d_name), 777);
    	  
    	  		/* check this directory */
    			stdbackup(strcat(origPath, lastentry->d_name), backupPath);
    
    	        /* Finally, restore the original working directory. */
    			if (chdir ("..") == -1)
    			{
    				fprintf (stderr, "Cannot chdir back to '%s': ", cwd);
    			    perror ("");
    			    return;
    			}
    	  } else //If not directory assume its a file
    	  {
    		  f_from=fopen(strcat(origPath, entry->d_name), "r");
    		  if (!f_from) 
    		  {
    			  fprintf(stderr, "Cannot open source file: ");
    			  perror("");
    			  return;
    		  }
    		  f_to=fopen(strcat(backupPath, entry->d_name), "w+");
    		  if (!f_to) 
    		  {
    			  fprintf(stderr, "Cannot open target file: ");
    			  perror("");
    			  return;
    		  }
    		  while (fgets(buf, MAX_DIR_PATH+1, f_from))
    		  {
    			  if (fputs(buf, f_to) == EOF)
    			  {  /* error writing data */
    				  fprintf(stderr, "Error writing to target file: ");
    				  perror("");
    				  return;
    			  }
    		  }
    	  }
      }
    }// End stdbackup()
    
    int main (int argc, char **argv)
    {
      char *option;		        // Option -sys or -std
      char *dir_path;		// directory path
      struct stat dir_stat;		// Used by stat()
      char dir_to_copy[MAX_DIR_PATH + 1];
      char cwd[MAX_DIR_PATH + 1];
    
      if (argc != 3 || !argv[1] || !argv[2])
      {
          fprintf (stderr, "Usage: %s <option> <directory path>\n",
    	       argv[0]);
          return(-1);
      }
      
      option = argv[1];
      dir_path = argv[2];
    
      // Make sure the given path refers to a directory
      
      if (stat (dir_path, &dir_stat) == -1)
      {
          perror ("stat:");
          return(-1);
      }
      
      if (!S_ISDIR (dir_stat.st_mode))
      {
    	  fprintf (stderr, "'%s' is not a directory\n", dir_path);
          return(-1);
      }
    
    // Change into the given directory
      
      if (chdir (dir_path) == -1)
      {
          fprintf (stderr, "Cannot change to directory '%s': ", dir_path);
          perror ("");
          return(-1);
      }
    
      getcwd (cwd, MAX_DIR_PATH + 1);
    
      if (strcmp(option, "-std") == 0)
      {
    	  mkdir(strcat(dir_to_copy, "BackStd"), 777);
    	  stdbackup(cwd, dir_to_copy);
      } else if (strcmp (option, "-sys") == 0)
      {
    	  mkdir(strcat(dir_to_copy, "BackSys"), 777);
    	  sysbackup(cwd, dir_to_copy);
      } else
      {
    	  fprintf (stderr, "'%s' is not a valid option, try again with -std or -sys\n", option);
    	  return(-1);
      }
    
      return 0;
    }

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Yay, codeform!

    ok.... it compiles.... but it seems to be getting hung up somewhere and I can't figure out where..... any good programs to use to figure out where and why its getting hung up?
    Yes -- GDB, the GNU debugger. If you're using GCC, you probably have it installed. Just type "gdb programname", and run your program. If it crashes, type "backtrace" and GDB will tell you where it crashed. If it gets stuck in a loop, press CTRL-C; GDB will catch the signal and stop your program, and then you can type "backtrace" again. (Type q to quit.)

    Suggestion: don't forget to use closedir().

    [edit]
    Code:
    			mkdir(strcat(backupPath, lastentry->d_name), 777);
    	  
    	  		/* check this directory */
    			stdbackup(strcat(origPath, lastentry->d_name), backupPath);
    You're modifying the value of the first argument to strcat() whenever you use this function. I think this may be causing problems.
    [/edit]
    Last edited by dwks; 03-06-2008 at 01:22 AM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    #define MAX_DIR_PATH 1024
    Unless you have a VERY specific reason for this, I would make this into MAX_PATH instead - MAX_PATH is a standard define that gives you the longest path-to-file that the system allows.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  2. Help with a couple compiler errors
    By s_ny33 in forum C Programming
    Replies: 1
    Last Post: 09-15-2005, 08:46 PM
  3. Errors when building program
    By tay_highfield in forum C Programming
    Replies: 2
    Last Post: 03-07-2003, 11:08 AM
  4. Errors when including winsock2.h
    By skiingwiz in forum Windows Programming
    Replies: 2
    Last Post: 12-27-2002, 07:32 PM
  5. Replies: 5
    Last Post: 11-13-2001, 04:38 PM