Home c++ Content output from TXT file using GET | C++

Content output from TXT file using GET | C++

Author

Date

Category

My code:

# include & lt; iostream & gt;
#Include & lt; String & GT;
#Include & lt; fstream & gt;
Using Namespace STD;
Void OutputDatabase ()
{
  STRING PATH = "MyFile.txt";
  ifstream fin;
  Fin.open (Path);
  if (fin.is_open ()) {
    COUT & LT; & LT; "File Was Open :)" & lt; & lt; Endl;
    char ch;
    While (Fin.get (CH))
    {
      COUT & LT; & LT; ch;
    }
  }
  ELSE {
    COUT & LT; & LT; "!!! Error !!! File Not Open!" & lt; & lt; Endl;
  }
  FIN.Close ();
}
INT MAIN ()
{
  OutputDatabase ();
  Return 0;
}

In the given code, it displays all words in a row, I need to display one by one, and moved to another only by pressing the key.


Answer 1, Authority 100%

I may not quite task understood, but if you can simply check that the current symbol is a separator, and if so, then wait for the button pressing, the example led below:

void outputdatabase ()
{
  Const char delimetr = '';
  STRING PATH = "123.TXT";
  ifstream fin;
  Fin.open (Path);
  if (fin.is_open ())
  {
    COUT & LT; & LT; "File Was Open :)" & lt; & lt; Endl;
    char ch;
    While (Fin.get (CH))
    {
      if (CH == DELIMETR)
      {
        cin.get ();
      }
      COUT & LT; & LT; ch;
    }
  }
  ELSE.
  {
    COUT & LT; & LT; "!!! Error !!! File Not Open!" & lt; & lt; Endl;
  }
  FIN.Close ();
}
INT MAIN ()
{
  OutputDatabase ();
  Return 0;
}

Answer 2, Authority 100%

If you are fundamentally important to read the file using get () , try this:

# include & lt; iostream & gt;
#Include & lt; String & GT;
#Include & lt; fstream & gt;
#Include & lt; Locale & gt; // for STD :: Isspace ()
BOOL IS_SEPARATOR (STD :: IfStream :: int_Type Symbol)
{
  Return Std :: Isspace (std :: ifstream :: traits_type :: to_char_type (symbol), std :: locale ());
}
Void OutputDatabase ()
{
  STD :: STRING PATH = "MyFile.txt";
  STD :: IFStream Fin (Path);
  if (fin.is_open ())
  {
    // File End Symbol
    auto eof = std :: ifstream :: traits_type :: eof ();
    While (True)
    {
      // Look at what the next character in the file without removing it.
      AUTO SYMBOL = FIN.PEEK ();
      // If the next character is the end of the file, then you finish reading the file.
      if (Symbol == EOF)
        Break;
      // If the following character is a separator symbol, then
      if (IS_Separator (Symbol))
      {
        // Read the separator symbol, but do not derive.
        FIN.GET ();
        Continue;
      }
      // The next symbol is not the end of the file and the non-separator symbol.
      // Waiting for the press Enter.
      While (STD :: CIN.GET ()! = '\ n')
        ;
      // Read the characters of the word from the file and display them.
      While (True)
      {
        // count the symbol from the file.
        AUTO SYMBOL = FIN.GET ();
        // If an error or a believed character occurred when reading, the separator symbol, then ...
        if (Symbol == EOF || IS_Separator (Symbol))
          Break; // ... stop the conclusion of the word.
        COUT & LT; & LT; std :: ifstream :: traits_type :: to_char_type (Symbol);
      }
    }
    FIN.Close ();
  }
}

Answer 3

// g ++ -wall -Wextra -Wpedantic -std = C++ 11 printwords.cpp -o printwords
#Include & lt; iostream & gt;
#Include & lt; String & GT;
#Include & lt; fstream & gt;
Using Namespace STD;
Void OutputDatabase ()
{
  STRING PATH = "MyFile.txt";
  ifstream fin;
  Fin.open (Path);
  if (fin.is_open ()) {
    COUT & LT; & LT; "File Was Open :), Press Enter" & lt; & lt; Endl;
    char ch;
    // Flag that the word has not yet been printed
    BOOL WORDEMPTY = TRUE;
    While (Fin.get (CH)) { 
COUT & LT; & LT; ch;
     If (Isspace (CH)) {
      If (not WORDEMPTY) {
       WORDEMPTY = TRUE;
       // if the word has already printed
       // Looking for the Enter key
       Char C;
       CIN.GET (C);
       Continue; }}
     ELSE.
      // Letter Printing
      WORDEMPTY = FALSE; }
   // Close the file only when they opened
   FIN.Close (); }
  ELSE.
   // Print in the error stream
    CERR & LT; & LT; "!!! Error !!! File Not Open!" & lt; & lt; Endl;
}
INT MAIN ()
{
  OutputDatabase ();
  Return 0;
}

Answer 4

# include & lt; iostream & gt;
#Include & lt; fstream & gt;
INT MAIN ()
{
  std :: ifstream input {"myfile.txt", std :: ios :: in};
  if (! input.is_open ()) {
    STD :: CERR & LT; & LT; "CAN't Open File, Terminating." & lt; & lt; STD :: ENDL;
    Return 1;
  }
  STD :: STRING WORD;
  While (Input & GT; & GT; Word) {
    STD :: CIN.GET ();
    STD :: COUT & LT; & LT; Word & lt; & lt; STD :: ENDL;
  }
  Return 0;
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions