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. fopen crashed when special characters found.

fopen crashed when special characters found.

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
9 Posts 7 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.
  • T Offline
    T Offline
    tagopi
    wrote on last edited by
    #1

    Hello everybody, i am trying to create a text file and trying to write something. my problem is with the file name. FILE *fp; fp = fopen("c:\test_1/2.txt,"wb"); this special character "/" is not accepting and fp is returning null, and it crashes the system if i try to write something in fp. i need to keep the file name as like this only. is there anyway to solve this issue? Thanks in advance. A. Gopinath.

    L C D A L 6 Replies Last reply
    0
    • T tagopi

      Hello everybody, i am trying to create a text file and trying to write something. my problem is with the file name. FILE *fp; fp = fopen("c:\test_1/2.txt,"wb"); this special character "/" is not accepting and fp is returning null, and it crashes the system if i try to write something in fp. i need to keep the file name as like this only. is there anyway to solve this issue? Thanks in advance. A. Gopinath.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      if that is a file named test_1/2.txt (and not file 2.txt in some test_1 folder) then the filename IS invalid. You'll have to choose a different name for your file. See here[^]. :)

      Luc Pattyn [My Articles] Nil Volentibus Arduum

      1 Reply Last reply
      0
      • T tagopi

        Hello everybody, i am trying to create a text file and trying to write something. my problem is with the file name. FILE *fp; fp = fopen("c:\test_1/2.txt,"wb"); this special character "/" is not accepting and fp is returning null, and it crashes the system if i try to write something in fp. i need to keep the file name as like this only. is there anyway to solve this issue? Thanks in advance. A. Gopinath.

        C Offline
        C Offline
        Chris Losinger
        wrote on last edited by
        #3

        tagopi wrote:

        is there anyway to solve this issue?

        yeah, don't call fwrite if your fp is NULL.

        image processing toolkits | batch image processing

        1 Reply Last reply
        0
        • T tagopi

          Hello everybody, i am trying to create a text file and trying to write something. my problem is with the file name. FILE *fp; fp = fopen("c:\test_1/2.txt,"wb"); this special character "/" is not accepting and fp is returning null, and it crashes the system if i try to write something in fp. i need to keep the file name as like this only. is there anyway to solve this issue? Thanks in advance. A. Gopinath.

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          tagopi wrote:

          is there anyway to solve this issue?

          Seeing as how that file cannot be created outside of code (e.g., right-click desktop, new text document, type name), I doubt it.

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

          1 Reply Last reply
          0
          • T tagopi

            Hello everybody, i am trying to create a text file and trying to write something. my problem is with the file name. FILE *fp; fp = fopen("c:\test_1/2.txt,"wb"); this special character "/" is not accepting and fp is returning null, and it crashes the system if i try to write something in fp. i need to keep the file name as like this only. is there anyway to solve this issue? Thanks in advance. A. Gopinath.

            A Offline
            A Offline
            Andrew Brock
            wrote on last edited by
            #5

            I think everyone here missed the real issue here. "c:\test_1/2.txt" There is a \t in the filename, which is converted to a tab character by the compiler. The file path getting passed into fopen is "c: est_1/2.txt" You need to escape this backslash with another backslash, so your filename becomes "c:\\test_1/2.txt". as for the forward slash, windows will interpret this as a path slash too, but you don't need to escape it because the compiler wont change it. So, the path you are accessing is C: > test_1 > 2.txt Additionally, fopen will not create the folder "test_1" if it does not exist, it will just fail to open it and return NULL.

            J 1 Reply Last reply
            0
            • A Andrew Brock

              I think everyone here missed the real issue here. "c:\test_1/2.txt" There is a \t in the filename, which is converted to a tab character by the compiler. The file path getting passed into fopen is "c: est_1/2.txt" You need to escape this backslash with another backslash, so your filename becomes "c:\\test_1/2.txt". as for the forward slash, windows will interpret this as a path slash too, but you don't need to escape it because the compiler wont change it. So, the path you are accessing is C: > test_1 > 2.txt Additionally, fopen will not create the folder "test_1" if it does not exist, it will just fail to open it and return NULL.

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #6

              That however wouldn't cause a crash. Whereas not testing the return value of the method probably does.

              A 1 Reply Last reply
              0
              • J jschell

                That however wouldn't cause a crash. Whereas not testing the return value of the method probably does.

                A Offline
                A Offline
                Andrew Brock
                wrote on last edited by
                #7

                OK, you are correct. The return value should always be checked for success, but if the filename was valid (and this is where the real issue is) the OP probably would have never noticed that, because it wouldn't be returning NULL.

                1 Reply Last reply
                0
                • T tagopi

                  Hello everybody, i am trying to create a text file and trying to write something. my problem is with the file name. FILE *fp; fp = fopen("c:\test_1/2.txt,"wb"); this special character "/" is not accepting and fp is returning null, and it crashes the system if i try to write something in fp. i need to keep the file name as like this only. is there anyway to solve this issue? Thanks in advance. A. Gopinath.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Hi, You are not able to create file name having special characters like "/,\,*" etc. If you want to confirm, just go on your desktop and try to create file having name "test_1/2", OS will not even allow to type '/' in file name. Happy Programming. Regards

                  1 Reply Last reply
                  0
                  • T tagopi

                    Hello everybody, i am trying to create a text file and trying to write something. my problem is with the file name. FILE *fp; fp = fopen("c:\test_1/2.txt,"wb"); this special character "/" is not accepting and fp is returning null, and it crashes the system if i try to write something in fp. i need to keep the file name as like this only. is there anyway to solve this issue? Thanks in advance. A. Gopinath.

                    T Offline
                    T Offline
                    tagopi
                    wrote on last edited by
                    #9

                    Thanks everybody for your replies. will give a try to replace "/" with some other. Thanks again. A. Gopinath.

                    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