By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,805 Members | 1,659 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,805 IT Pros & Developers. It's quick & easy.

fgetc/fpostc crashes with long filenames

P: 1
Hi! This is my first post on the web ever!

To make it short, I made a routine to change certain bytes in a file in binary mode. It all worked perfectly, until I decided to use a long name directory and it crashes. I made certain check points to find out exactly where it crashes and why. Here is my subroutine code:

Expand|Select|Wrap|Line Numbers
  1. void binary_edit (char *cur_dir) {
  2.     FILE *fp,*fp2;
  3.     char filename[100],filename2[100];
  4.     int temp,cur_bit=0;
  5.     sprintf(filename ,"%s\\file1.bin",cur_dir);
  6.     sprintf(filename2,"%s\\file2.bin",cur_dir);
  7.     fp=fopen(filename,"rb");
  8.     fp2=fopen(filename2,"wb");
  9.  
  10.     temp=(int)fgetc(fp); //doesn't get here if filename=>41
  11.     while (!feof(fp)) {
  12.         switch (cur_bit) {
  13.             case 50:fputc(0,fp2);break;
  14.             case 51:fputc(1,fp2);break;
  15.             case 52:fputc(2,fp2);break;
  16.             case 53:fputc(3,fp2);break;
  17.             case 54:fputc(4,fp2);break;
  18.             case 55:fputc(5,fp2);break;
  19.             default:fputc(temp,fp2);
  20.         }
  21.         temp=(int)fgetc(fp);
  22.     }
  23.  
  24.      //doesn't get here if filename2=>39
  25.     fclose(fp);
  26.     fclose(fp2);
  27.     unlink(filename);
  28.     SleepEx(1000,false);
  29.     rename(filename2,filename);
  30. }
  31.  
Thanking you in advance

Mario
Nov 19 '06 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 1,510
the maximum path length should be defined by PATH_MAX in limits.h, see
http://www.opengroup.org/onlinepubs/009695399/functions/fopen.html

try the following program
Expand|Select|Wrap|Line Numbers
  1. /* display maximum path and file name length */
  2.  
  3. #include <stdio.h>
  4. #include <limits.h>
  5.  
  6. int main()
  7. {
  8.        printf(" maximum path length %d ", PATH_MAX);
  9.        printf(" maximum name length %d ", NAME_MAX);
  10.        getchar();getchar();getchar();getchar();
  11.        }
  12.  
  13.  
some compilers don't define NAME_MAX and some define neither of them
Nov 19 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.