Thread: if not exist, failed

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    11

    if not exist, failed

    Hello
    I try a function "if not exist" but this will give my Warnings, please what would meant this warnings?

    Code:
    #include<stdio.h>
    int main() {
       FILE *fname;
       if (fname = fopen("a.txt", "r")) {
          fclose(fname);
          printf("file exists");
       } else {
          printf("file doesn't exist");
       }
    }

    to compile using "geany" and "GCC" for Windows.

    Warning from Compiler
    Code:
    gcc -Wall -c "ifex.c" (im Verzeichnis: C:\Temp\tcc)
    ifex.c: In function 'main':
    ifex.c:4:8: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
        5 |    if (fname = fopen("a.txt", "r")) {
          |        ^~~~~
    Kompilierung erfolgreich beendet.

    please, if there problems with the GCC Compiler ? i see also
    that in the meantime the Compiler Option i need to change from GCC 2 g++, and then the file will blow up over 2-3mb, for 10 lines Code?

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    That warning is legit. The compiler thinks you meant to use '==' instead of '='.
    Two options:
    Code:
    ...
    if ((fname = fopen("a.txt", "r"))) {
    ...
    or
    Code:
    #include <stdio.h>
    
    int main(void) {
       FILE *fname = NULL;
    
       fname = fopen("a.txt", "r");
    
       if (fname != NULL) {
          fclose(fname);
          printf("file exists");
       } else {
          printf("file doesn't exist");
       }
       return 0;
    }

  3. #3
    Registered User
    Join Date
    Oct 2018
    Posts
    11
    Thanks Rstanley
    I made your changes and are running very well thanks meny time.

    Strange thing are also, i uninstall Geany my Compiler Environment, and install after that codeblocks, here are running both version my&our without any Warnings.

    I would thanks also for your fast answer!
    Regards
    Last edited by mauric; 07-20-2020 at 10:45 AM.

  4. #4
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    66
    Or see this example.

    Code:
    /** opnclose.c */
    #include <stdio.h>        /*  printf, fopen, fclose  */
    #include <stdlib.h>       /*  exit                   */
    
    #define READ "r"
    
    int main(int argc, char *argv[])
     {
      FILE *fz;
    
      if (argc != 2)                              
       {
        printf("\n\nSyntax: opnclose datei[.ext]");
        exit(0);
       }
    
      if ((fz = fopen(argv[1], READ)) == NULL)     
       printf("\n\nFailure:could not open file %s\n", argv[1]);
      else
       {
        fclose(fz);                               
        printf("\n\nFile %s closed.", argv[1]);
       }
     return 0; 
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The name ___ does not exist...
    By Xfer in forum C# Programming
    Replies: 4
    Last Post: 01-08-2011, 12:24 PM
  2. When does an action exist?
    By Yarin in forum General Discussions
    Replies: 18
    Last Post: 10-18-2009, 01:50 PM
  3. Does this exist?
    By Kennedy in forum Tech Board
    Replies: 2
    Last Post: 11-23-2006, 03:04 AM
  4. Types already exist!!!! WHY???
    By Arkanos in forum C# Programming
    Replies: 3
    Last Post: 03-08-2006, 04:08 AM
  5. does uint4 exist?
    By rosgreco in forum C++ Programming
    Replies: 1
    Last Post: 06-11-2002, 11:44 AM

Tags for this Thread