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. how to read character by character from text file in c++?

how to read character by character from text file in c++?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestionc++
7 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
    sajedevahedi
    wrote on last edited by
    #1

    hi there I would appreciate if someone could tell me how can I read character by character from a file? I did some research about it but there weren't work in mine for example: it couldn't verified infile although i use fstream library or is.get none of them working

    char singlecharacter;
    int singleCharacter ;
    ifstream file ("vurudi");
    file.is_open() ;
    infile.get(singlecharacter);

    J L S L 4 Replies Last reply
    0
    • S sajedevahedi

      hi there I would appreciate if someone could tell me how can I read character by character from a file? I did some research about it but there weren't work in mine for example: it couldn't verified infile although i use fstream library or is.get none of them working

      char singlecharacter;
      int singleCharacter ;
      ifstream file ("vurudi");
      file.is_open() ;
      infile.get(singlecharacter);

      J Offline
      J Offline
      jeron1
      wrote on last edited by
      #2

      Maybe this[^] link will help.

      S 1 Reply Last reply
      0
      • J jeron1

        Maybe this[^] link will help.

        S Offline
        S Offline
        sajedevahedi
        wrote on last edited by
        #3

        tnx for your replay. as I said I used the code which I mention before from this link but it didnt work

        J 1 Reply Last reply
        0
        • S sajedevahedi

          tnx for your replay. as I said I used the code which I mention before from this link but it didnt work

          J Offline
          J Offline
          jeron1
          wrote on last edited by
          #4

          You are going to have to get more specific on what's not working, are you able to open the file? Posting the whole program would probably help.

          1 Reply Last reply
          0
          • S sajedevahedi

            hi there I would appreciate if someone could tell me how can I read character by character from a file? I did some research about it but there weren't work in mine for example: it couldn't verified infile although i use fstream library or is.get none of them working

            char singlecharacter;
            int singleCharacter ;
            ifstream file ("vurudi");
            file.is_open() ;
            infile.get(singlecharacter);

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

            sajedevahedi wrote:

            none of them working

            Unfortunately that does not help us to help you. Please be specific about the results you expect versus the results, or errors, you receive. In the meantime you may find http://msdn.microsoft.com/en-us/library/f5tsy854.aspx#vclrfthegetfunctionanchor12[^] useful.

            Veni, vidi, abiit domum

            1 Reply Last reply
            0
            • S sajedevahedi

              hi there I would appreciate if someone could tell me how can I read character by character from a file? I did some research about it but there weren't work in mine for example: it couldn't verified infile although i use fstream library or is.get none of them working

              char singlecharacter;
              int singleCharacter ;
              ifstream file ("vurudi");
              file.is_open() ;
              infile.get(singlecharacter);

              S Offline
              S Offline
              Software_Developer
              wrote on last edited by
              #6

              Just put the file.get into a while statement.

              #include
              #include
              #include

              using namespace std;

              int main()
              {

              char singlecharacter ;
               
              ifstream file ("vurudi.txt");
              file.is\_open() ;
              
              while(file.get(singlecharacter))
              {
              	cout<
              
              1 Reply Last reply
              0
              • S sajedevahedi

                hi there I would appreciate if someone could tell me how can I read character by character from a file? I did some research about it but there weren't work in mine for example: it couldn't verified infile although i use fstream library or is.get none of them working

                char singlecharacter;
                int singleCharacter ;
                ifstream file ("vurudi");
                file.is_open() ;
                infile.get(singlecharacter);

                L Offline
                L Offline
                leon de boer
                wrote on last edited by
                #7

                There are a number of ways to do this depending on your preference and setup of C++ program Using standard windows API:

                HANDLE Handle;
                char Ch;
                unsigned long Li;

                Handle = CreateFile("c:\\yourfile.txt",
                GENERIC_READ, 0, 0, OPEN_EXISTING,
                FILE_ATTRIBUTE_NORMAL, 0); // Try to open file
                if (Handle != INVALID_HANDLE_VALUE){ // Check file opened
                ReadFile(Handle, &Ch, 1, &Li, 0); // Read a character

                }
                CloseHandle(Handle); // Close the file

                Using the standard ifstream unit:

                #include <fstream>

                char Ch;

                ifstream myFile;
                myFile.open("c:\\yourfile.txt"); // Try to open file
                if (myFile.is_open()) { // Check file opened
                myFile.read(Ch, sizeof(Ch)); // Read a character
                }
                myFile.Close(); // Close the file

                Using the standard MFC CFile assuming you are using MFC:

                UINT nActual = 0;
                char Ch;
                CFile myFile;

                if ( myFile.Open( _T("c:\\yourfile.txt"), CFile::modeRead | CFile::shareDenyWrite ) )
                {
                nActual = myFile.Read( Ch, sizeof(Ch) );

                }
                myFile.Close();

                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