Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. FILE* problem

FILE* problem

Scheduled Pinned Locked Moved C / C++ / MFC
c++xmlhelpquestionannouncement
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DevelopmentNoob
    wrote on last edited by
    #1

    Hi, I'm now using the File* to save the data into text file. But i notice that if the file i want to save is in open mode, then the program will crash. Any way to prevent it? Below is snap of my code.. In *.h file

    FILE *handle;

    in *.cpp file

    fopen_s(&handle,cPathName,"wt");
    fprintf(handle,"<?xml version=\"1.0\"?>\n") // crash at here

    _ A J 3 Replies Last reply
    0
    • D DevelopmentNoob

      Hi, I'm now using the File* to save the data into text file. But i notice that if the file i want to save is in open mode, then the program will crash. Any way to prevent it? Below is snap of my code.. In *.h file

      FILE *handle;

      in *.cpp file

      fopen_s(&handle,cPathName,"wt");
      fprintf(handle,"<?xml version=\"1.0\"?>\n") // crash at here

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      fopen_s has a return value that you must check for errors. This way you will write only if the open operation was successful, thereby avoiding the crash.

      «_Superman_»
      I love work. It gives me something to do between weekends.

      Microsoft MVP (Visual C++)

      Polymorphism in C

      1 Reply Last reply
      0
      • D DevelopmentNoob

        Hi, I'm now using the File* to save the data into text file. But i notice that if the file i want to save is in open mode, then the program will crash. Any way to prevent it? Below is snap of my code.. In *.h file

        FILE *handle;

        in *.cpp file

        fopen_s(&handle,cPathName,"wt");
        fprintf(handle,"<?xml version=\"1.0\"?>\n") // crash at here

        A Offline
        A Offline
        Aescleal
        wrote on last edited by
        #3

        Are you checking that handle actually points to something? i.e. check the handle for being NULL. If you call any file function with a zero pointer it might very well crash if it dereferences a NULL pointer. Cheers, Ash

        1 Reply Last reply
        0
        • D DevelopmentNoob

          Hi, I'm now using the File* to save the data into text file. But i notice that if the file i want to save is in open mode, then the program will crash. Any way to prevent it? Below is snap of my code.. In *.h file

          FILE *handle;

          in *.cpp file

          fopen_s(&handle,cPathName,"wt");
          fprintf(handle,"<?xml version=\"1.0\"?>\n") // crash at here

          J Offline
          J Offline
          Jackson2010
          wrote on last edited by
          #4

          Please check if the "handle" is NULL.

          fopen_s(&handle,cPathName,"wt");
          if (handle != NULL)
          {
          fprintf(handle,"<?xml version=\"1.0\"?>\n")
          }

          Working is a happy thing! Enjoy free partition manager!

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups