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. int, read from text file

int, read from text file

Scheduled Pinned Locked Moved C / C++ / MFC
question
8 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.
  • J Offline
    J Offline
    jonavon blakly
    wrote on last edited by
    #1

    i need to be able to read a line from a text file and store what is read in int.

    RaviBeeR E A 3 Replies Last reply
    0
    • J jonavon blakly

      i need to be able to read a line from a text file and store what is read in int.

      RaviBeeR Offline
      RaviBeeR Offline
      RaviBee
      wrote on last edited by
      #2

      And your question is...? /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.com

      U 1 Reply Last reply
      0
      • RaviBeeR RaviBee

        And your question is...? /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.com

        U Offline
        U Offline
        uus99
        wrote on last edited by
        #3

        Try this, //File reading---- CFileException ex; //catch exceptions CFile myfile; //Open file myfile.Open("c:/b.txt", CFile::modeRead, &ex); //Get length int z=myfile.GetLength(); //create temp buffer with all data including line breaks char* data=new char[z]; //read data myfile.Read(data,z); //Extract only data, remove line breaks int* value=new int[z/3]; for(int i=0;i

        J 1 Reply Last reply
        0
        • J jonavon blakly

          i need to be able to read a line from a text file and store what is read in int.

          E Offline
          E Offline
          eli15021979
          wrote on last edited by
          #4

          what do you man when you say that you want to store the data in int. do you want to store the number of bytes you read from a file in int'or do you want to store the data in int(in that case -> ???????)? regards, Eli

          J 1 Reply Last reply
          0
          • E eli15021979

            what do you man when you say that you want to store the data in int. do you want to store the number of bytes you read from a file in int'or do you want to store the data in int(in that case -> ???????)? regards, Eli

            J Offline
            J Offline
            jonavon blakly
            wrote on last edited by
            #5

            i want to store the data in int.

            1 Reply Last reply
            0
            • U uus99

              Try this, //File reading---- CFileException ex; //catch exceptions CFile myfile; //Open file myfile.Open("c:/b.txt", CFile::modeRead, &ex); //Get length int z=myfile.GetLength(); //create temp buffer with all data including line breaks char* data=new char[z]; //read data myfile.Read(data,z); //Extract only data, remove line breaks int* value=new int[z/3]; for(int i=0;i

              J Offline
              J Offline
              jonavon blakly
              wrote on last edited by
              #6

              what header file to i need for this code?

              U 1 Reply Last reply
              0
              • J jonavon blakly

                what header file to i need for this code?

                U Offline
                U Offline
                uus99
                wrote on last edited by
                #7

                No header is required. Stdafx.h is sufficient i think. CFile is a class of MFC. Refer to MSDN

                1 Reply Last reply
                0
                • J jonavon blakly

                  i need to be able to read a line from a text file and store what is read in int.

                  A Offline
                  A Offline
                  Alton Williams
                  wrote on last edited by
                  #8

                  Assuming that your file has CR/LF at end of each line. The code below might be easier CStdioFile myFile("Sample.txt",CFile::modeRead ); CString myBuffer; int myInteger = 0; LPCTSTR pMyBuffer, pMyBufferEnd; myFile.ReadString(myBuffer); while(!myBuffer.IsEmpty()){ pMyBuffer = myBuffer; pMyBufferEnd = pMyBuffer + myBuffer.GetLength(); cout << pMyBuffer << endl; for( pMyBuffer;pMyBuffer <= pMyBufferEnd; pMyBuffer++) { if((*pMyBuffer < '0') ||(*pMyBuffer > '9')) { cout << myInteger << (LPCTSTR) " "; myInteger = 0; } else { myInteger *= 10; myInteger += *pMyBuffer - 48; } } cout << endl; myFile.ReadString(myBuffer); } myFile.Close(); I'll e-mail you a demo. It's a console app (don't worry it supports MFC!) Good luck, happy programming, Alton

                  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