0

As i said in the title. This is how my program starts: I call this function to create a file (if it doesn't previously exist), and if it does exist it will take the info in the file and stock them into a struct, that i created. You will find the code below.

But here is the part that still i can't found solution to, if i delete all the files and launch the program, the file will be created, i go simultaneously to the folder and i can delete them by myself with no problem (the program still working in the background).

SO the issue is not in the first IF where the file got closed. But when i launch the program again with the file already exists (so the program will go for the ELSE IF and collect the data ) here starts the problem, when i launch the program and simultaneously go to the folder to try and delete the file manually an error msg pop up and tell me that the file is being opened in a program (the program still working in the background). Despite that i'm using the the fclose(fc).

So where is the problem in my script? i really spent days trying to solve this problem. and i need to solve because next in my program i need to be able to remove the file (so i can name another one with his name) here is the script

int file_fc(classe c[])
{
    FILE * fc;
    int count=0;
    char buffer[500];

    if ((fc=fopen("Fclasse.txt","r"))==NULL)
        {
    fc=fopen("Fclasse.txt","w");
    fprintf(fc,"Code Classe  Libellé Classe  Spécialite    Capacité\n");
       fclose(fc);
       fclose(fc);
        }


     else
     {
        fc=fopen("Fclasse.txt","r");
         {
            fgets(buffer,300, fc);
            fgets(buffer,300, fc);

         while(feof(fc) == 0)
              {
             sscanf(buffer,"%11d %16s %16s %5d",&c[count].code, c[count].lib, c[count].spec, &c[count].capa);
             count++;
             fgets(buffer,300, fc);
               }
         }
       fclose(fc);

     }


return (count);

ediiit::: changed the script to this

   fc=fopen("Fclasse.txt","r");
if (fc==NULL)
    {
   fc=fopen("Fclasse.txt","w");
fprintf(fc,"Code Classe  Libellé Classe  Spécialite    Capacité\n");
   fclose(fc);
    }


 else
 {
        fgets(buffer,300, fc);
        fgets(buffer,300, fc);

     while(feof(fc) == 0)
          {
         sscanf(buffer,"%11d %16s %16s %5d",&c[count].code, c[count].lib, c[count].spec, &c[count].capa);
         count++;
         fgets(buffer,300, fc);
           }
 }
fclose(fc);

and it's working perfectly. what i still don't understand if i do this as i was doing before

if(fc=fopen("fclasse.txt","r")==NULL)

where am i supposed to put the fclose()? inside the if? at the end of the function ??

Or simply do i need to count how many times i opened the file and at the end before closing the file, i write Fclose() as many time as i opened (i mean the place where i put fclose doesn't matter) ??

Laga San
  • 1
  • 2
  • 4
    Please start your sentences with uppercase letter. Otherwise your text is unnecessarily hard to read. – user694733 Dec 20 '19 at 12:32
  • 1
    Please indent your code properly – Jabberwocky Dec 20 '19 at 12:36
  • Let me try to first understand what you wanted to do. If your intention was to check if the file is open then don't open the file in read mode in the if condition! Instead use access, https://linux.die.net/man/2/access, method to check if the file is already open! If you put fopen in the 'if' condition it will end up opening the file! and you will need to close it twice in case you opened it inside the if block again! – Vignesh Vedavyas Dec 20 '19 at 12:45
  • 1
    It's also very unclear what the program is supposed to do if the file does not exist. In that case you seem to create a new file containing only the line `"Code Classe Libellé Classe Spécialite Capacité\n"`. And what then? It's pointless to read from that file.... – Jabberwocky Dec 20 '19 at 12:48
  • 1
    Also see [**Why is “while ( !feof (file) )” always wrong?**](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) You are not properly detecting the end of the file, so you will read and `sscanf()` the last line in the file twice. – Andrew Henle Dec 20 '19 at 13:05
  • @Jabberwocky that's just the first line, Which is translated to `"class code class name specialty capacity" ` my bad i should have translated them to make more sense. Next in the program, i will enter the data in the file. – Laga San Dec 20 '19 at 13:28

2 Answers2

4

You open the file three times, throwing away two of the opened objects. Then you only close once. The two others stay open until the program closes.

You open it first time in the if, then you open it again in else if and then third time inside the block. Only the first one should be there.

Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74
4

I didn't check your whole code but there is at least a problem here:

 else if ((fc=fopen("Fclasse.txt","r"))!=NULL)
 {
   fc=fopen("Fclasse.txt","r");

You're opening the file twice, the first time in the if condition, and the second time right after overwriting fc.

You rather want something like this:

 else
 {
    fc = fopen("Fclasse.txt", "r");
    if (fc != NULL)
    {
      // work with fc
    }
    ...
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • The `else` clause is for when the first open succeeded. – Jonathan Leffler Dec 20 '19 at 13:35
  • @JonathanLeffler he edited his code in the meantime (sigh), so this answer doesn't make much sens anymore... – Jabberwocky Dec 20 '19 at 13:42
  • Oh — sorry, I didn't think to check that. The inserted code is the bit before the 'edit' marker, not after as one might expect. Golly, people make life hard for those who would help them! – Jonathan Leffler Dec 20 '19 at 13:47
  • @JonathanLeffler sigh... yes, so true. – Jabberwocky Dec 20 '19 at 13:48
  • @JonathanLeffler and Jabberwocky i do apologize, this is literally my first question/post here, so i thought i need to edit my original post to let other people know that it is okay i found the solution and what was the solution is.... i will make sure to pay more attention next time to those details :) – Laga San Dec 20 '19 at 14:25
  • @LagaSan instead of _editing_ your question you can _answer_ your own question. – Jabberwocky Dec 20 '19 at 14:26