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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. a fscanf error in MFC SDI

a fscanf error in MFC SDI

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++debugging
7 Posts 3 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.
  • C Offline
    C Offline
    CHYGO
    wrote on last edited by
    #1

    my code:

    #include "stdafx.h"
    #include <stdio.h>
    #include <fstream>

    void CParamDisplay::OnParamOpen()
    {
    // TODO: Add your control notification handler code here
    CFileDialog cfiledlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT
    , "Text Files (*.txt)|*.txt|All Files (*.*)|*.*||", this);

    if (IDOK == cfiledlg.DoModal())
    {
    	m\_filename = cfiledlg.GetPathName();
    	UpdateData(FALSE);
    	DisplayParam(m\_filename);
    }	
    

    }

    BOOL CParamDisplay::DisplayParam(CString filename)
    {
    UINT sNum = 0;
    CHAR paramId[MAX_BUF_SIZE] = _T("");
    CHAR paramUnit[MAX_BUF_SIZE] = _T("");
    FLOAT paramValue = 0;

    FILE\* pFile = fopen (filename, "r");
    
    if(pFile == NULL)
    	return FALSE;
    
    while(fscanf(pFile, "%d %s %s %f", sNum, paramId, paramUnit, paramValue) != EOF)
    {
         ...
    }
    
    fclose(pFile);
    
    return TRUE;
    

    }

    when i debug it, the app stopped at fscanf().and displayed:"Uhandled exception in XX.exe (MSVCRTD.DLL):0XC00000005:access violation".i can't fix it...(T_T)

    i 've tried ,then i have no regret

    G B 2 Replies Last reply
    0
    • C CHYGO

      my code:

      #include "stdafx.h"
      #include <stdio.h>
      #include <fstream>

      void CParamDisplay::OnParamOpen()
      {
      // TODO: Add your control notification handler code here
      CFileDialog cfiledlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT
      , "Text Files (*.txt)|*.txt|All Files (*.*)|*.*||", this);

      if (IDOK == cfiledlg.DoModal())
      {
      	m\_filename = cfiledlg.GetPathName();
      	UpdateData(FALSE);
      	DisplayParam(m\_filename);
      }	
      

      }

      BOOL CParamDisplay::DisplayParam(CString filename)
      {
      UINT sNum = 0;
      CHAR paramId[MAX_BUF_SIZE] = _T("");
      CHAR paramUnit[MAX_BUF_SIZE] = _T("");
      FLOAT paramValue = 0;

      FILE\* pFile = fopen (filename, "r");
      
      if(pFile == NULL)
      	return FALSE;
      
      while(fscanf(pFile, "%d %s %s %f", sNum, paramId, paramUnit, paramValue) != EOF)
      {
           ...
      }
      
      fclose(pFile);
      
      return TRUE;
      

      }

      when i debug it, the app stopped at fscanf().and displayed:"Uhandled exception in XX.exe (MSVCRTD.DLL):0XC00000005:access violation".i can't fix it...(T_T)

      i 've tried ,then i have no regret

      G Offline
      G Offline
      Garth J Lancaster
      wrote on last edited by
      #2

      what is MAX_BUF_SIZE defined as ? what does your data in the file look like ? the access violation reported is most commonly caused by overwriting memory somehow - not allocating it/enough - I can see you are allocating buffers for your char (string) arrays, but for instance if MAX_BUF_SIZE was 5 and you had 1 thisisacharastring thisisanothercharstring 1.44 you'd see that the first %s in fscanf would get thisisacharastring which is a bit longer than 5 'g'

      C 1 Reply Last reply
      0
      • C CHYGO

        my code:

        #include "stdafx.h"
        #include <stdio.h>
        #include <fstream>

        void CParamDisplay::OnParamOpen()
        {
        // TODO: Add your control notification handler code here
        CFileDialog cfiledlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT
        , "Text Files (*.txt)|*.txt|All Files (*.*)|*.*||", this);

        if (IDOK == cfiledlg.DoModal())
        {
        	m\_filename = cfiledlg.GetPathName();
        	UpdateData(FALSE);
        	DisplayParam(m\_filename);
        }	
        

        }

        BOOL CParamDisplay::DisplayParam(CString filename)
        {
        UINT sNum = 0;
        CHAR paramId[MAX_BUF_SIZE] = _T("");
        CHAR paramUnit[MAX_BUF_SIZE] = _T("");
        FLOAT paramValue = 0;

        FILE\* pFile = fopen (filename, "r");
        
        if(pFile == NULL)
        	return FALSE;
        
        while(fscanf(pFile, "%d %s %s %f", sNum, paramId, paramUnit, paramValue) != EOF)
        {
             ...
        }
        
        fclose(pFile);
        
        return TRUE;
        

        }

        when i debug it, the app stopped at fscanf().and displayed:"Uhandled exception in XX.exe (MSVCRTD.DLL):0XC00000005:access violation".i can't fix it...(T_T)

        i 've tried ,then i have no regret

        B Offline
        B Offline
        Bram van Kampen
        wrote on last edited by
        #3

        try: while(fscanf(pFile, "%d %s %s %f", **&**sNum, paramId, paramUnit, **&**paramValue)... :)

        Bram van Kampen

        G C 2 Replies Last reply
        0
        • B Bram van Kampen

          try: while(fscanf(pFile, "%d %s %s %f", **&**sNum, paramId, paramUnit, **&**paramValue)... :)

          Bram van Kampen

          G Offline
          G Offline
          Garth J Lancaster
          wrote on last edited by
          #4

          chuckle, I missed that one completely 'g'

          B 1 Reply Last reply
          0
          • G Garth J Lancaster

            chuckle, I missed that one completely 'g'

            B Offline
            B Offline
            Bram van Kampen
            wrote on last edited by
            #5

            Easy thing to Miss! Chased one similar to that last week in my own code for 2 hours. At least the OP set it to '0' (NULL) to begin with. I had not done so, and made it far more difficult to trace. :)

            Bram van Kampen

            1 Reply Last reply
            0
            • G Garth J Lancaster

              what is MAX_BUF_SIZE defined as ? what does your data in the file look like ? the access violation reported is most commonly caused by overwriting memory somehow - not allocating it/enough - I can see you are allocating buffers for your char (string) arrays, but for instance if MAX_BUF_SIZE was 5 and you had 1 thisisacharastring thisisanothercharstring 1.44 you'd see that the first %s in fscanf would get thisisacharastring which is a bit longer than 5 'g'

              C Offline
              C Offline
              CHYGO
              wrote on last edited by
              #6

              thank u, i've tried the Bram van Kampen's method,it works:)

              i 've tried ,then i have no regret

              1 Reply Last reply
              0
              • B Bram van Kampen

                try: while(fscanf(pFile, "%d %s %s %f", **&**sNum, paramId, paramUnit, **&**paramValue)... :)

                Bram van Kampen

                C Offline
                C Offline
                CHYGO
                wrote on last edited by
                #7

                :) it works.it takes me about three hours to try to solve it. thank u very mnuch.

                i 've tried ,then i have no regret

                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