473,513 Members | 2,469 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

fgetc/fpostc crashes with long filenames

1 New Member
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
1 1949
horace1
1,510 Recognized Expert Top Contributor
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

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

Similar topics

4
1730
by: Anthony Lansbergen | last post by:
Hi, My (pre)compiler(s) (borland C++ builder 5 and g++) both seems to have a problem with the new include format. In stead of #include <iostream.h> I want to say #include <iostream>. This...
13
15092
by: William L. Bahn | last post by:
I'm sure this has been asked before, and I have looked in the FAQ, but I'm looking for an explanation for the following: The functions pairs: gets()/fgets() puts()/fputs() printf()/fprintf()...
6
2265
by: Kobu | last post by:
Do the "larger" input functions like scanf, gets, fgets use fgetc to take input or an operating system call function like read() (I know it could be any "way", but I'm trying to find out how it's...
1
3908
by: Pieter | last post by:
I want to be able to split regular MAX_PATH length filenames and 32K UNICODE filenames using the \\?\ naming convention. Are there any equivalents to _tsplitpath() for long names, or am I left to...
3
3313
by: cinsky | last post by:
Hi, While reading ISO C Standard, I found follow text in 7.19.8.1: size_t fread(void *restrict ptr, size_t SIZE, ...) ... For each object, SIZE calls are made to the fgetc function and the...
0
2497
by: chongming | last post by:
Hi, i want to display all the filenames on browser. However i found that if there are many filenames in that folder, result will be it will display a long list of filenames on that browser. My...
9
5127
by: primeSo | last post by:
// FIRST int main(void){ int c, i = 0; char cArray; while( (c = getchar()) != '\n' && c != EOF){ cArray = c; i ++; }
2
4384
by: Praesidium | last post by:
I'm having a bit of a problem running a C program I'm working on (compiled in cygwin). There's a way to go, as I'm intent on making sure each bit works before moving on to the next. As it appears...
2
1930
by: =?Utf-8?B?SmVzcGVyLCBEZW5tYXJr?= | last post by:
Hi, I work in a company where we tend to use very deep folder structures and long descriptive file names due to the nature of the analysis that we make. I've made a program that recursively...
0
7259
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7158
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5683
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5085
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1592
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.