Home c++ read characters from the C++ file

read characters from the C++ file

Author

Date

Category

You need to read the N-n-number of characters from the file. The question is worth: how to find out the length of the string in the file (when the words are separated by a space, then only 1 word is read, which goes to a space and all)
Baked FSTREAM file streams


Answer 1, Authority 100%

Here is an example of a hill reading from the file:

# include & lt; iostream & gt;
#Include & lt; fstream & gt;
Using Namespace STD;
INT MAIN () {
  Char SIM;
  Ifstream MyFile ("Example.txt");
  INT COUNT = 19;
  int i = 0;
  if (myfile.is_open ())
  {
    While (MyFile.get (SIM) & amp; & amp; I & LT; COUNT)
    {
      COUT & LT; & LT; SIM;
      I ++;
    }
    MyFile.Close ();
  }
  ELSE COUT & LT; & LT; "Unable to Open File";
  Return 0;
}

output

this is a line.
THI

If you want to find the number of characters in the file:

int main () {
STREAMPOS BEGIN, END;
Ifstream MyFile ("Example.txt");
begin = myfile.tellg ();
MyFile.Seekg (0, iOS :: END);
End = MyFile.tellG ();
MyFile.Close ();
COUT & LT; & LT; "LENGTH:" & lt; & lt; (End - Begin) & lt; & lt; "Symbols. \ n";
}

Answer 2

Use I \ OstringStream for this. The root of that The answer has already been given.

answer

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