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. crash with push_back for vector

crash with push_back for vector

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsquestion
6 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.
  • N Offline
    N Offline
    nss
    wrote on last edited by
    #1

    What am I doing wrong? I want to read the names from a file and put them onto a vector of char*......it crashes at the pushback. Each file name is on a different line. I did a dir/b in dos and got a txt file with a list of file names (a bunch of .bmps) from the directory. I assume theres carriage returns at the end of each line. But maybe not. There are 18 letters in the name08FEB096_9571s.bmp, and carriage return line feed is 1 byte (MSDN).

    CFile file;
    
    std::vector fileVec;
    
    file.Open("c:\\\\miss\\\\SImages.txt", CFile::modeRead, 0);
    
    char\* pBuf;
    
    char folder\[200\];
    
    strcpy(folder , "C:\\\\miss\\\\");
    
    for (int i = 0; i <96; i++)
    {
    
    	pBuf = new char\[200\];
    
    	file.Read( pBuf,20);
    
    	strcat(folder, pBuf);
    
    	fileVec.push\_back(folder);
    
    	delete\[\] pBuf;
    }
    
    
    file.Close();
    
    C S J 3 Replies Last reply
    0
    • N nss

      What am I doing wrong? I want to read the names from a file and put them onto a vector of char*......it crashes at the pushback. Each file name is on a different line. I did a dir/b in dos and got a txt file with a list of file names (a bunch of .bmps) from the directory. I assume theres carriage returns at the end of each line. But maybe not. There are 18 letters in the name08FEB096_9571s.bmp, and carriage return line feed is 1 byte (MSDN).

      CFile file;
      
      std::vector fileVec;
      
      file.Open("c:\\\\miss\\\\SImages.txt", CFile::modeRead, 0);
      
      char\* pBuf;
      
      char folder\[200\];
      
      strcpy(folder , "C:\\\\miss\\\\");
      
      for (int i = 0; i <96; i++)
      {
      
      	pBuf = new char\[200\];
      
      	file.Read( pBuf,20);
      
      	strcat(folder, pBuf);
      
      	fileVec.push\_back(folder);
      
      	delete\[\] pBuf;
      }
      
      
      file.Close();
      
      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      looks like the board ate some of your code. how is your vector defined? (what is it a vector of?) -c


      WWT2D?

      Fractals

      N 1 Reply Last reply
      0
      • C Chris Losinger

        looks like the board ate some of your code. how is your vector defined? (what is it a vector of?) -c


        WWT2D?

        Fractals

        N Offline
        N Offline
        nss
        wrote on last edited by
        #3

        It was avector of CString. However my problem was that I was using CFile when CStdioFile was more suitable and I had overwritten some buffers, but the poor vector got blamed.... Thanks.:)

        1 Reply Last reply
        0
        • N nss

          What am I doing wrong? I want to read the names from a file and put them onto a vector of char*......it crashes at the pushback. Each file name is on a different line. I did a dir/b in dos and got a txt file with a list of file names (a bunch of .bmps) from the directory. I assume theres carriage returns at the end of each line. But maybe not. There are 18 letters in the name08FEB096_9571s.bmp, and carriage return line feed is 1 byte (MSDN).

          CFile file;
          
          std::vector fileVec;
          
          file.Open("c:\\\\miss\\\\SImages.txt", CFile::modeRead, 0);
          
          char\* pBuf;
          
          char folder\[200\];
          
          strcpy(folder , "C:\\\\miss\\\\");
          
          for (int i = 0; i <96; i++)
          {
          
          	pBuf = new char\[200\];
          
          	file.Read( pBuf,20);
          
          	strcat(folder, pBuf);
          
          	fileVec.push\_back(folder);
          
          	delete\[\] pBuf;
          }
          
          
          file.Close();
          
          S Offline
          S Offline
          Solomon Wu
          wrote on last edited by
          #4

          CFile file; change std::vector fileVec; to std::vector<string> fileVec; Move strcpy(folder,"c:\\miss\\"); to For Statement

          for (int i = 0; i <96; i++) {
          strcpy(folder , "C:\\miss\\");
          pBuf = new char[200];
          file.Read( pBuf,20);
          strcat(folder, pBuf);
          fileVec.push_back(folder);
          delete[] pBuf;
          }

          Learning and Working

          1 Reply Last reply
          0
          • N nss

            What am I doing wrong? I want to read the names from a file and put them onto a vector of char*......it crashes at the pushback. Each file name is on a different line. I did a dir/b in dos and got a txt file with a list of file names (a bunch of .bmps) from the directory. I assume theres carriage returns at the end of each line. But maybe not. There are 18 letters in the name08FEB096_9571s.bmp, and carriage return line feed is 1 byte (MSDN).

            CFile file;
            
            std::vector fileVec;
            
            file.Open("c:\\\\miss\\\\SImages.txt", CFile::modeRead, 0);
            
            char\* pBuf;
            
            char folder\[200\];
            
            strcpy(folder , "C:\\\\miss\\\\");
            
            for (int i = 0; i <96; i++)
            {
            
            	pBuf = new char\[200\];
            
            	file.Read( pBuf,20);
            
            	strcat(folder, pBuf);
            
            	fileVec.push\_back(folder);
            
            	delete\[\] pBuf;
            }
            
            
            file.Close();
            
            J Offline
            J Offline
            Jambolo
            wrote on last edited by
            #5

            Nobody explained the reason for the crash...Without the unimportant stuff, here is what the code looks like. Do you see the problem? char folder[200]; strcpy(folder , "C:\\miss\\"); for (int i = 0; i <96; i++) { ... strcat(folder, pBuf); ... }

            N 1 Reply Last reply
            0
            • J Jambolo

              Nobody explained the reason for the crash...Without the unimportant stuff, here is what the code looks like. Do you see the problem? char folder[200]; strcpy(folder , "C:\\miss\\"); for (int i = 0; i <96; i++) { ... strcat(folder, pBuf); ... }

              N Offline
              N Offline
              ns
              wrote on last edited by
              #6

              Yup. I saw finally....my poor "folder" variable was getting concatenated way beyond its allocated space... Thanks Appreciate your help, ns

              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