C++ - how would I do this?

Nyap

HTML Noob
OP
Banned
Joined
Jan 13, 2016
Messages
971
Trophies
0
Age
55
Location
That Chaos Site
XP
483
Country
so I know what my problem is I just don't know how to fix it
I want to avoid using std::string although I know that would fix my problem easily.
Code:
protected:
  //Checks if string is dynamically allocated
   bool isDynamic{};
   //Size of the string
  int bufferSize{};
  //Pointer to the string
  char* Buffer;

//Extract a line from an ifstream
void getline(ifstream& input)
{
  char oneBuffer;
  bufferSize=0;
  for (int i{}; oneBuffer!='\n' && input; ++i, ++bufferSize)
        input >> oneBuffer;
  Buffer=new char[bufferSize];
  input.close();
  input.open("lol.txt");
  for (int i{}; i<=bufferSize; ++i)
  {
         input >> oneBuffer;
         Buffer[i]=oneBuffer;
  }
}
Note that this isn't the full code, I just included what's necessary to know (tell me if you need more)
so the problem is that >> doesn't extract whitespace. As a result, lets say you have this in lol.txt:
Hello World
it would print this
HelloWorldd
how do I fix this?
 
Last edited by Nyap,

DaniloLemes

Member
Newcomer
Joined
Oct 4, 2012
Messages
9
Trophies
0
XP
105
You want to just read a line and print it? 'Object oriented and logical' talking, you should just read the entire line and then store it somewhere and print.

--------------------- MERGED ---------------------------

If I am right and you just want to read line by line, you can do this way:
Code:
int main () {
  string line;
  ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while ( getline (myfile,line) )
    {
      cout << line << '\n';
    }
    myfile.close();
  }

  else cout << "Unable to open file";

  return 0;
}
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • K3Nv2 @ K3Nv2:
    Spent like 5 hours on switch one never touched it again
  • Psionic Roshambo @ Psionic Roshambo:
    Sentinel of the stary skies
  • K3Nv2 @ K3Nv2:
    Ds is 20 years old this year
  • Psionic Roshambo @ Psionic Roshambo:
    So MJ no longer wants to play with it?
  • K3Nv2 @ K3Nv2:
    He put it down when the 3ds came out
  • SylverReZ @ SylverReZ:
    @K3Nv2, RIP Felix does great videos on the PS3 yellow-light-of-death.
  • Jayro @ Jayro:
    Eventhough the New 3DS XL is more powerful, I still feel like the DS Lite was a more polished system. It's a real shame that it never got an XL variant keeping the GBA slot. You'd have to go on AliExpress and buy an ML shell to give a DS phat the unofficial "DS Lite" treatment, and that's the best we'll ever get I'm afraid.
    +1
  • Jayro @ Jayro:
    The phat model had amazingly loud speakers tho.
    +1
  • SylverReZ @ SylverReZ:
    @Jayro, I don't see whats so special about the DS ML, its just a DS lite in a phat shell. At least the phat model had louder speakers, whereas the lite has a much better screen.
    +1
  • SylverReZ @ SylverReZ:
    They probably said "Hey, why not we combine the two together and make a 'new' DS to sell".
  • Veho @ Veho:
    It's a DS Lite in a slightly bigger DS Lite shell.
    +1
  • Veho @ Veho:
    It's not a Nintendo / iQue official product, it's a 3rd party custom.
    +1
  • Veho @ Veho:
    Nothing special about it other than it's more comfortable than the Lite
    for people with beefy hands.
    +1
  • Jayro @ Jayro:
    I have yaoi anime hands, very lorge but slender.
  • Jayro @ Jayro:
    I'm Slenderman.
  • Veho @ Veho:
    I have hands.
  • BakerMan @ BakerMan:
    imagine not having hands, cringe
    +1
  • AncientBoi @ AncientBoi:
    ESPECIALLY for things I do to myself :sad:.. :tpi::rofl2: Or others :shy::blush::evil:
    +1
  • The Real Jdbye @ The Real Jdbye:
    @SylverReZ if you could find a v5 DS ML you would have the best of both worlds since the v5 units had the same backlight brightness levels as the DS Lite unlockable with flashme
  • The Real Jdbye @ The Real Jdbye:
    but that's a long shot
  • The Real Jdbye @ The Real Jdbye:
    i think only the red mario kart edition phat was v5
  • BigOnYa @ BigOnYa:
    A woman with no arms and no legs was sitting on a beach. A man comes along and the woman says, "I've never been hugged before." So the man feels bad and hugs her. She says "Well i've also never been kissed before." So he gives her a kiss on the cheek. She says "Well I've also never been fucked before." So the man picks her up, and throws her in the ocean and says "Now you're fucked."
    BigOnYa @ BigOnYa: A woman with no arms and no legs was sitting on a beach. A man comes along and the woman says...