Avatar of sctt_tiger
sctt_tiger
 asked on

fopen is failing

#includ<stdio.h>

FILE *f,*g;
void main()
{
   //Some code
   f=fopen("abc.log", "wt");
     if(f==NULL)
      DisplayError("\nabc.log Cant created ");
   WriteData(f);
   fclose(f);
   // more code
   g=fopen("xyz.log","wt");
      if(g==NULL)
            DisplayError("\nxyz.log cant be created");
      
}// main ends

void DisplayError( char *message) /* Routine for displaying the error messages.  */
{      printf("%s\n", message);
      printf("Program stopped !!\n");
      exit(0);
}


Problem
2nd fopen always fails. i have tried it many times by changing file name like abc.txt, abc.dat  file1.txt etc.
1st fopen succeeds.

please explain, why, what are various reasons that fopen can fail.
while running above program, no other file is opend.
i can create other files from DOS prompt, i mean no problem regarding disk space.
please chekc.
Thanks.
C++

Avatar of undefined
Last Comment
sctt_tiger

8/22/2022 - Mon
sunnycoder

Hello sctt_tiger,

Is this the exact code? Can you verify if you have g=NULL in place of  g==NULL?
Does the file exist and you have permissions for writing to it?

Regards,
sunnycoder
sunnycoder

Another possibility, is the file already open in a text editor or some other application?
sctt_tiger

ASKER
checked all this.
file does not exist.
it is like
    if(f==NULL)
    if (g==NULL)

i have the permissions to create/write file because i am running this program on my PC.
interestingly, i tried it at my LapTop as well as at my DeskTop.
but in both cases, error is at same line.
2nd fopen fails, getting g=NULL and then entring in DisplayError function and then exiting.

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
sctt_tiger

ASKER
one point to mention, when first writedata writes data in file, succeeds, data is very large, about 40,000 bytes.

but i dont think, it should affect.
sunnycoder

Can you pls post the exact code so that we can rule out the possibility of a typo?
40,000 bytes is not huge amount of data and moreover you are writing it to a different file ... both should be unrelated.
sctt_tiger

ASKER
ok, here comes. exact code of function in which i am opening file and writing data from 2 dim array is, it is little different from the one i wrote u in my original problem.
this is exact code :

void PrintArray(unsigned char**arr, unsigned long nr, unsigned long nc)
{
      FILE *f;
      unsigned long i,j;
      printf("\n Inside PrintFilteredArray with rows,col= %lu,%lu",nr,nc); //ok
      f=fopen("abc.txt","wt"); //ok
      if(f==NULL) //becomes true
            DisplayError("\nFiltrd.txt Cant Be Opened ");
//what ever is next shoud not matter.

      for(i=0;i
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
sunnycoder

So you are opening the same file twice?
sunnycoder

you are overwriting the contents of the same file multiple times ... probably you would either like to pass the filename as parameter to this function or use a naming scheme such as temp001.txt, temp002.txt and so on or open the file in append mode for writing the arrays.
Infinity08

>> //what ever is next shoud not matter.

What does this mean ? Is there some other code there ?

How is the original code you showed related to the PrintArray function ?

Where and how is the PrintArray function called ? With what parameters ?

In other words : we need more code. Preferrably a copy-paste of all the code.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
sctt_tiger

ASKER
when i wrote //Whatever comes next, does not matter.
it simply means, error occurs before reaching here, so whatever is next, is not executed, so shud not matter.

Ok, now, Actually,
i have 2 such functions both proces arr, but after call to PrintArray1 which prints arr in a file, closes a file, function ends.
then back in main, arr is processed,
and then
PrintArray2 is called
both functions have similar signature and body, only file name is different.
when i call PrintArray2, and it tries to create file it fails.

body of both functions is :

void PrintArray1(unsigned char**arr, unsigned long nr, unsigned long nc)
{
      FILE *f;
      unsigned long i,j;
      printf("\n Inside PrintFilteredArray with rows,col= %lu,%lu",nr,nc);
      f=fopen("abc.txt","wt");
      if(f==NULL)
            DisplayError("\nabc.txt Cant Be Opened ");
      for(i=0;i
Infinity08

>> i tried to print error number using
>> cout<<"\nError code is " << ferror(g) ;

if ferror returns a non-zero value, you should use perror to show what the error was :

        if (ferror(g)) {
            perror(NULL);
        }


>> when i call PrintArray2, and it tries to create file it fails.

Can you show the exact code that calls these two functions ? Don't leave anything out ;) It might be important.
sctt_tiger

ASKER
ok, Thansks,  Please wait.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
sctt_tiger

ASKER
I tried
        f=fopen("c:/abc.txt","w");
      cout<<"\n Error code is " << ferror(f)<<"\n";
      char error_msg[255];
      sprintf( error_msg, "Error opening file '%s'", f);
        perror( error_msg );

and got this :
 
Error code is 16
Error opening file '(null)': Not enough memory


now what does this "Not enough memory" mean here.
i m not running any software except OS (XP) and C++ IDE, there may be some system softwares running at back.
i am having 256 MB RAM in my laptop.
sctt_tiger

ASKER
no doubt my arr is very large it is about 100 x 100 of unsigned char.
and i am having 2 more such arrays with different names simultaneously.
sctt_tiger

ASKER
Further,
if i replace my code with this in PrintArray2, it runs.
      ofstream f("c:/test.txt");
for(int i=0;i
Your help has saved me hundreds of hours of internet surfing.
fblack61
SOLUTION
Infinity08

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
sunnycoder

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
sctt_tiger

ASKER
i have converted it to
    ofstream f("abc.txt");
      for(int i=0;i
sctt_tiger

ASKER
Please look at another question we(3 friends working in a team) have posted at
https://www.experts-exchange.com/questions/23583889/Coding-for-Median-Filter.html