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
503
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
    Psionic Roshambo @ Psionic Roshambo: https://www.ebay.com/itm/234405624530?chn=ps&norover=1&mkevt=1&mkrid=711-117182-37290-0&mkcid=2&m...