dimanche 4 octobre 2020

How to read a specific amount of characters

I can get the characters from console with this codes: Displays 2 characters each time in a new line

#include <iostream>
#include <fstream>
#include <string>

using namespace std;
int main()
{
    char ch[3] = "";

    ifstream file("example.txt");

    while (file.read(ch, sizeof(ch)-1))
    {
       cout << ch << endl;
    }

    return 0;
}

My problem is, if the set of characters be odd it doesn't displays the last character in the text file!

my text file contains this: abcdefg

it doesn't displays the letter g in the console its displaying this:

  • ab
  • cd
  • ef

I wanna display like this:

  • ab
  • cd
  • ef
  • g

I wanna use this to read 1000 characters at a time for a large file so i don't wanna read character by character, It takes a lot of time, but it has a problem if u can fix it or have a better suggestion, share it with me

Aucun commentaire:

Enregistrer un commentaire