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. Crazy fgets stuff [modified]

Crazy fgets stuff [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
help
10 Posts 5 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.
  • S Offline
    S Offline
    stevelam
    wrote on last edited by
    #1

    I am using this code: FILE * pFile; char filename [100]; pFile = fopen ("\\Settings\\temp.txt" , "rt"); if (pFile == NULL) perror ("Error opening file"); else { fgets (filename , 100 , pFile); fclose (pFile); } to get a filename that another part of my program has written. However whatever I put in the text file my program reads 100 Í characters. Now when I open up the text file myself it shows exactly what I want, i.e. not that :) This is driving me slightly crazy so any help would be great. P.S. If anyone wants the text file I am quite willing to e-mail it to you. -- modified at 10:15 Wednesday 23rd August, 2006

    Z R G T 4 Replies Last reply
    0
    • S stevelam

      I am using this code: FILE * pFile; char filename [100]; pFile = fopen ("\\Settings\\temp.txt" , "rt"); if (pFile == NULL) perror ("Error opening file"); else { fgets (filename , 100 , pFile); fclose (pFile); } to get a filename that another part of my program has written. However whatever I put in the text file my program reads 100 Í characters. Now when I open up the text file myself it shows exactly what I want, i.e. not that :) This is driving me slightly crazy so any help would be great. P.S. If anyone wants the text file I am quite willing to e-mail it to you. -- modified at 10:15 Wednesday 23rd August, 2006

      Z Offline
      Z Offline
      Zac Howland
      wrote on last edited by
      #2

      You said another part of your program is writing the file? Here are a few things to try: - Initialize your string: char filename[100] = {0}; - Set the file cursor to the beginning explicitly: fseekg(pFile, 0, SEEK_SET); - Make sure that the file is not currently open by the section of code that is suppose to be writing to it. You may be running into a race condition if you have 2 threads (for example) where thread 1 is writing to the file, but thread 2 is already trying to read from it before the write is complete.

      If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

      S 1 Reply Last reply
      0
      • S stevelam

        I am using this code: FILE * pFile; char filename [100]; pFile = fopen ("\\Settings\\temp.txt" , "rt"); if (pFile == NULL) perror ("Error opening file"); else { fgets (filename , 100 , pFile); fclose (pFile); } to get a filename that another part of my program has written. However whatever I put in the text file my program reads 100 Í characters. Now when I open up the text file myself it shows exactly what I want, i.e. not that :) This is driving me slightly crazy so any help would be great. P.S. If anyone wants the text file I am quite willing to e-mail it to you. -- modified at 10:15 Wednesday 23rd August, 2006

        R Offline
        R Offline
        Rage
        wrote on last edited by
        #3

        Ever tried with: pFile = fopen ("\\Settings\\temp.txt" , "r");

        ~RaGE();

        I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus

        S 1 Reply Last reply
        0
        • Z Zac Howland

          You said another part of your program is writing the file? Here are a few things to try: - Initialize your string: char filename[100] = {0}; - Set the file cursor to the beginning explicitly: fseekg(pFile, 0, SEEK_SET); - Make sure that the file is not currently open by the section of code that is suppose to be writing to it. You may be running into a race condition if you have 2 threads (for example) where thread 1 is writing to the file, but thread 2 is already trying to read from it before the write is complete.

          If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

          S Offline
          S Offline
          stevelam
          wrote on last edited by
          #4

          Right, I have made sure that the other program isn't writing to the file. Initializing the char just results in the program returning a blank string. Using fseek doesn't seem to make any difference. Do you think it could be a problem with the format of the file?

          Z 1 Reply Last reply
          0
          • R Rage

            Ever tried with: pFile = fopen ("\\Settings\\temp.txt" , "r");

            ~RaGE();

            I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus

            S Offline
            S Offline
            stevelam
            wrote on last edited by
            #5

            Yep, that doesnt fix it.

            R 1 Reply Last reply
            0
            • S stevelam

              Yep, that doesnt fix it.

              R Offline
              R Offline
              Rage
              wrote on last edited by
              #6

              How do you it returns only a bunch of Ì ? Is this in the debugger ? Because the snippet you've provided is almost the example of the use of the fgets function in the MSDN, and it should work properly. Have you tested it with another temp file ?

              ~RaGE();

              I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus

              S 1 Reply Last reply
              0
              • S stevelam

                I am using this code: FILE * pFile; char filename [100]; pFile = fopen ("\\Settings\\temp.txt" , "rt"); if (pFile == NULL) perror ("Error opening file"); else { fgets (filename , 100 , pFile); fclose (pFile); } to get a filename that another part of my program has written. However whatever I put in the text file my program reads 100 Í characters. Now when I open up the text file myself it shows exactly what I want, i.e. not that :) This is driving me slightly crazy so any help would be great. P.S. If anyone wants the text file I am quite willing to e-mail it to you. -- modified at 10:15 Wednesday 23rd August, 2006

                G Offline
                G Offline
                G_S
                wrote on last edited by
                #7

                stevelam wrote:

                fgets (filename , 100 , pFile);

                chage that to if( fgets (filename , 100 , pFile) == NULL ) { //ERROR } this prevent your prog from crashing. there is nothing wrong with that part of your code the file could have been created in binary mode. the buffer in this case filename could be currupted after the read op.

                G_S

                1 Reply Last reply
                0
                • R Rage

                  How do you it returns only a bunch of Ì ? Is this in the debugger ? Because the snippet you've provided is almost the example of the use of the fgets function in the MSDN, and it should work properly. Have you tested it with another temp file ?

                  ~RaGE();

                  I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus

                  S Offline
                  S Offline
                  stevelam
                  wrote on last edited by
                  #8

                  Right OK, I've found it, in the part of my program that wrote first there were some errors that corrupted the file. All is now working again, thanks for all of your help.

                  1 Reply Last reply
                  0
                  • S stevelam

                    Right, I have made sure that the other program isn't writing to the file. Initializing the char just results in the program returning a blank string. Using fseek doesn't seem to make any difference. Do you think it could be a problem with the format of the file?

                    Z Offline
                    Z Offline
                    Zac Howland
                    wrote on last edited by
                    #9

                    stevelam wrote:

                    Initializing the char just results in the program returning a blank string

                    That means that fgets isn't reading in anything for some reason. Check its return value to see if it is getting an error.

                    If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                    1 Reply Last reply
                    0
                    • S stevelam

                      I am using this code: FILE * pFile; char filename [100]; pFile = fopen ("\\Settings\\temp.txt" , "rt"); if (pFile == NULL) perror ("Error opening file"); else { fgets (filename , 100 , pFile); fclose (pFile); } to get a filename that another part of my program has written. However whatever I put in the text file my program reads 100 Í characters. Now when I open up the text file myself it shows exactly what I want, i.e. not that :) This is driving me slightly crazy so any help would be great. P.S. If anyone wants the text file I am quite willing to e-mail it to you. -- modified at 10:15 Wednesday 23rd August, 2006

                      T Offline
                      T Offline
                      toxcct
                      wrote on last edited by
                      #10

                      what don't you do that in a C++ way ? STL is full of good things you know


                      TOXCCT >>> GEII power

                      [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                      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