fstream and ofstream

Discussion in 'C++' started by bballmitch, Sep 1, 2005.

  1. bballmitch

    bballmitch Guest

    I have the following code

    #include <cstdlib>
    #include <iostream>
    #include <fstream>

    int score_one=2345, score_two=1800, score_three=1500, score_four=1300,
    score_five=1100;
    int score_six=1000, score_seven=900, score_eight=860, score_nine=700,
    score_ten=600;

    using namespace std;

    ifstream* openFile(istream& input)
    {
    for(;;)
    {
    ifstream* pFileStream = new ifstream("HighScores2.txt");
    return pFileStream;
    }
    }

    int main(int argc, char *argv[])
    {

    cout<<"Please enter an integer.\n";
    int score;
    cin>>score;
    cout<<"\n";
    ofstream my("HighScores2.txt");
    if(score>=score_one)
    {
    score_one=score;
    }
    my<<"1. "<<score_one<<endl;
    my<<"2. "<<score_two<<endl;
    my<<"3. "<<score_three<<endl;
    my<<"4. "<<score_four<<endl;
    my<<"5. "<<score_five<<endl;
    my<<"6. "<<score_six<<endl;
    my<<"7. "<<score_seven<<endl;
    my<<"8. "<<score_eight<<endl;
    my<<"9. "<<score_nine<<endl;
    my<<"10. "<<score_ten<<endl;

    ifstream* pFileStream = openFile(cin);
    char buffer[80];
    while(!pFileStream->eof() && pFileStream->good())
    {
    pFileStream->read(buffer, 80);
    int noBytes = pFileStream->gcount();
    for(int i=0; i<noBytes;i++)
    {
    cout<< buffer;
    }
    }
    int k=buffer[0];

    cout<<"\n\n"<<k<<" is the best number in the world.\n";


    system("PAUSE");
    return EXIT_SUCCESS;
    }


    but then when i cout the integer k, it ALWAYS has an output of 46.
    unless its above 2000, in which case its something else. but i can't
    figure out where 46 is coming from. i know like there is no practical
    reason for why im doing this, but there is. my questions are these...

    1. where is the 46 coming from?
    2. how do i figure out how many numbers/characters are in a line in a
    file?
    3. lets say that my file has the following in it...

    1. 2000
    2. 1800
    3. 1500

    how do i make it so that if a number is greater than 2000 then it
    becomes 1, and 2000 becomes two and so on and so forth? im able to do
    that once, but because every time im declaring the variables, it always
    restarts so that 1 is 2000 2 is 1800 etc.
    bballmitch, Sep 1, 2005
    #1
    1. Advertising

  2. bballmitch wrote:
    > I have the following code
    >
    > #include <cstdlib>
    > #include <iostream>
    > #include <fstream>
    >
    > int score_one=2345, score_two=1800, score_three=1500, score_four=1300,
    > score_five=1100;
    > int score_six=1000, score_seven=900, score_eight=860, score_nine=700,
    > score_ten=600;
    >
    > using namespace std;
    >
    > ifstream* openFile(istream& input)


    Why do you need an argument in that function?

    > {
    > for(;;)
    > {


    Why do you need this loop?

    > ifstream* pFileStream = new ifstream("HighScores2.txt");
    > return pFileStream;
    > }
    > }
    >
    > int main(int argc, char *argv[])
    > {
    >
    > cout<<"Please enter an integer.\n";
    > int score;
    > cin>>score;
    > cout<<"\n";
    > ofstream my("HighScores2.txt");
    > if(score>=score_one)
    > {
    > score_one=score;


    If it's equal, there is no real sense in assigning, is there?
    Only assign new value to 'score_one' if it's truly _smaller_ than
    'score'...

    > }
    > my<<"1. "<<score_one<<endl;
    > my<<"2. "<<score_two<<endl;
    > my<<"3. "<<score_three<<endl;
    > my<<"4. "<<score_four<<endl;
    > my<<"5. "<<score_five<<endl;
    > my<<"6. "<<score_six<<endl;
    > my<<"7. "<<score_seven<<endl;
    > my<<"8. "<<score_eight<<endl;
    > my<<"9. "<<score_nine<<endl;
    > my<<"10. "<<score_ten<<endl;


    So, supposedly here you filled the 'HighScores2.txt' with those lines...

    >
    > ifstream* pFileStream = openFile(cin);


    What is passing 'cin' supposed to do?

    BTW, you allocate 'pFileStream' in the free store, but never delete
    it...

    > char buffer[80];
    > while(!pFileStream->eof() && pFileStream->good())
    > {
    > pFileStream->read(buffer, 80);


    So, now you're reading 80 bytes back from the same file you just wrote.
    If the file is supposed to carry _lines_, why don't you use 'getline'?

    > int noBytes = pFileStream->gcount();
    > for(int i=0; i<noBytes;i++)
    > {
    > cout<< buffer;
    > }
    > }
    > int k=buffer[0];


    Here 'k' is the value of the very first character in the buffer. But
    what does the buffer contain?

    >
    > cout<<"\n\n"<<k<<" is the best number in the world.\n";
    >
    >
    > system("PAUSE");
    > return EXIT_SUCCESS;
    > }
    >
    >
    > but then when i cout the integer k, it ALWAYS has an output of 46.
    > unless its above 2000, in which case its something else. but i can't
    > figure out where 46 is coming from.


    So, why don't you take a look at the contents of 'buffer'?

    > i know like there is no practical
    > reason for why im doing this, but there is.


    That's a self-contradicting statement.

    > my questions are these...
    >
    > 1. where is the 46 coming from?


    From the file. If your system uses ASCII encoding, it's the period ('.').

    > 2. how do i figure out how many numbers/characters are in a line in a
    > file?


    You need to use 'getline' probably.

    > 3. lets say that my file has the following in it...
    >
    > 1. 2000
    > 2. 1800
    > 3. 1500
    >
    > how do i make it so that if a number is greater than 2000 then it
    > becomes 1, and 2000 becomes two and so on and so forth? im able to do
    > that once, but because every time im declaring the variables, it always
    > restarts so that 1 is 2000 2 is 1800 etc.
    >


    You need to do it one step at a time. Learn to insert your "score" into
    the "table" by shifting the values down. The simplest way is probably
    look from above to find where it fits (compare to all 10 scores in the
    "table", one by one) and shift others down.

    Write the algorithm of inserting your score into the table using a pencil
    and a sheet of paper. Forget C++ for a while. Figure out what you need
    to do _manually_, then put it in C++ terms.

    V
    Victor Bazarov, Sep 1, 2005
    #2
    1. Advertising

  3. bballmitch wrote:
    > I have the following code
    >


    There's an awful lot wrong with it, but I'll stick to your specific
    questions.

    >
    > but then when i cout the integer k, it ALWAYS has an output of 46.
    > unless its above 2000, in which case its something else. but i can't
    > figure out where 46 is coming from. i know like there is no practical
    > reason for why im doing this, but there is. my questions are these...
    >
    > 1. where is the 46 coming from?


    You are convering a character number[0] to an integer k. When you do
    that you get the integer encoding of the character (ever heard of
    ASCII?). I think that you want to convert the number in your file to an
    integer. That's a different operation entirely and the easiest way to do
    that is to read an integer

    int k;
    pFileStream >> k;

    Don't mess around reading characters when what you really want to do is
    read integers.

    > 2. how do i figure out how many numbers/characters are in a line in a
    > file?


    Read the whole line (like this)

    string line;
    getline(pFileStream, line);

    then examine all the characters of the line one by one. This is very
    complicated. I would rethink your need to do this if possible.

    > 3. lets say that my file has the following in it...
    >
    > 1. 2000
    > 2. 1800
    > 3. 1500
    >
    > how do i make it so that if a number is greater than 2000 then it
    > becomes 1, and 2000 becomes two and so on and so forth? im able to do
    > that once, but because every time im declaring the variables, it always
    > restarts so that 1 is 2000 2 is 1800 etc.
    >


    You need to learn about arrays (or vectors). You need to read the whole
    file into an array (or vector), then make the adjustments you need to
    the array (or vector) and then write out the whole array (or vector) to
    the file again. You cannot make the adjustments that you want to do to a
    file directly, you need to do it in memory.

    John
    John Harrison, Sep 1, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chase Bradford

    ostream and ofstream overloading

    Chase Bradford, Nov 16, 2003, in forum: C++
    Replies:
    3
    Views:
    1,141
    tom_usenet
    Nov 17, 2003
  2. Armando
    Replies:
    6
    Views:
    745
    Armando
    Jan 29, 2004
  3. Squid Seven

    ofstream * vs. ofstream

    Squid Seven, Jul 13, 2005, in forum: C++
    Replies:
    5
    Views:
    595
    Ivan Johansen
    Jul 14, 2005
  4. Ramesh
    Replies:
    5
    Views:
    773
    Ramesh
    Oct 17, 2008
  5. chiku

    fstream vs ofstream

    chiku, May 4, 2009, in forum: C++
    Replies:
    6
    Views:
    2,386
    Paul Carter
    May 5, 2009
Loading...

Share This Page