Follow us on Twitter!
Don't judge the unknown - Grindordie
Saturday, April 19, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 32
Guests Online: 32
Members Online: 0

Registered Members: 82831
Newest Member: FL4SHC0D3R
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

C++ questions


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 30-04-11 19:32
**UPDATE**
Is it possible to print out all the data that f.open has gained before using f.close() to completely close the file and finalize the File Writing???

ex.
Code

int main()
  {
  ofstream f ("test.txt", ios::app);
  string data = "Stuff to store in text document.";
  int i=10;

  while (i>0)
    {
    f << data << "\n";
    i--;
    }

  cout << f;

  f.close();
  return 0;
  }





Edited by on 01-05-11 01:41
Author

RE: C++ questions


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 30-04-11 19:56
Froger wrote:
I figured f.close() would mark the beginning of the text document update

It... does. But I guess it's hard to notice in your program since it quits right after it closes it.


Author

RE: C++ questions


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 30-04-11 20:08
COM wrote:
Froger wrote:
I figured f.close() would mark the beginning of the text document update

It... does. But I guess it's hard to notice in your program since it quits right after it closes it.



Then may I ask this, why in the example below, does it not update?

Code

int main()
  {
  int i=10;

  while (i>0)
    {
    string text;
    ofstream f ("test.txt", ios::out);

    getline(cin, text);
    f << text << "\n";
    f.close();
    }
  return 0;
  }





Also COM, thanks for the reply. It is much appreciated. I have viewed like 20 different tutorials on file writing and just couldn't exactly find what
I was looking for. I guess I really don't know how to look for what I am looking for on google.
Author

RE: C++ questions


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 30-04-11 20:15
Froger wrote:
Then may I ask this, why in the example below, does it not update?

Code

int main()
  {
  int i=10;

  while (i>0)
    {
    string text;
    ofstream f ("test.txt", ios::out);

    getline(cin, text);
    f << text << "\n";
    f.close();
    }
  return 0;
  }




Well you might want to try to use ios::app so it doesn't wipe the text file clean every time it reopens. ios::out is pretty redundant since you're opening an ofstream anyhow.


Author

RE: C++ questions


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 30-04-11 20:20
COM, you have solved the problem for me. appending the text file would be an obviously be a better choice than out. Lol. I have been overlooking this for hours. I guess it is good to sometimes give things a break. Again thanks COM for the help.
Author

RE: **UPDATED**


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 01-05-11 01:43
Bumping thread, look at original post.
Author

RE: C++ questions

starofale
Member



Posts: 218
Location: England
Joined: 05.12.07
Rank:
Moderate
Posted on 01-05-11 01:46
Froger wrote:
Bumping thread, look at original post.

It would have been better to just put the new information in a new post and leave the original.
That way other people can understand what was going on and it might have helped people who have the same problem.


Try a new search engine
Author

RE: C++ questions


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 01-05-11 01:53
starofale wrote:
Froger wrote:
Bumping thread, look at original post.

It would have been better to just put the new information in a new post and leave the original.
That way other people can understand what was going on and it might have helped people who have the same problem.


I will take that into consideration next time. Sorry if I caused you any problems/frustrations.
Author

RE: C++ questions


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 01-05-11 04:01
Froger wrote:
Is it possible to print out all the data that f.open has gained before using f.close() to completely close the file and finalize the File Writing???

As starofale said, changing the initial post was pretty stupid.
Anyhow, good question, I've never actually tried that... Looked around a bit quickly and you could do something akin to this:

Code
int main()
{
    string fuckyou;
    fstream ohnoes("ohnoes.txt", ios::in|ios::out|ios::trunc);
    if(!ohnoes.good())
    {
        cout<<"wtfx"<<endl;
        return 1;
    }
    do
    {
        getline(cin, fuckyou);
        ohnoes<<fuckyou;
    }while(!fuckyou.empty());
    ohnoes.seekp(0, ios_base::beg);
    char *outp=new char[ohnoes.rdbuf()->in_avail()+1];
    outp[ohnoes.rdbuf()->in_avail()]=0;
    ohnoes.read(outp, ohnoes.rdbuf()->in_avail());
    cout<<outp<<endl;
    delete[] outp;
    ohnoes.close();
    return 0;
}



Apparently the ofstream class isn't meant to, you know... be read from.