Problem with ofstream...
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Problem with ofstream...

  1. #1
    Join Date
    Dec 2008
    Posts
    8

    Problem with ofstream...

    Hi knowledgeable C++ programmers!
    I'm having problems with this program because it is giving me an error about how the file cannot read #include <ofstream>

    please do take a look at the program...

    // Program 16.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include <string>
    #include <ofstream>
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;

    int main ( )
    {
    int i;
    struct record
    {
    char name[15];
    float amt;
    };
    record master[10] = {{"Helen", 10}, {"Julie", 20}, {"Lena", 30}, {"Alan", 40}, {"Annie", 50}, {"May", 60},
    {"Lee", 70}, {"Sam", 80}, {"June", 90}, {"Bill", 100}};
    record trans[7] = {{"Lena", 10}, {"Julie", 5.75}, {"Lee", 15.02}, {"Ed", 40}, {"Julie", 10.00}, {"Art", 5.00},
    {"Bill", 7.32}};
    ofstream foutmaster("master.dat");
    ofstream fouttrans("trans.dat");

    for (i=0; i<10; i++)
    {
    foutmaster << master.name[i] << master.amt[i] << endl;
    }

    for (i=0; i<7; i++)
    {
    fouttrans << trans.name[i] << trans.amt[i] << endl;
    }

    foutmaster.close("master.dat");
    fouttrans.close("trans.dat");

    }

    I'm using Visual C++ 2005

    1>------ Build started: Project: Program 16, Configuration: Debug Win32 ------
    1>Compiling...
    1>Program 16.cpp
    1>c:\documents and settings\wonwoo\my documents\visual studio 2005\projects\program 16\program 16\program 16.cpp(6) : fatal error C1083: Cannot open include file: 'ofstream': No such file or directory
    1>Build log was saved at "file://c:\Documents and Settings\Wonwoo\My Documents\Visual Studio 2005\Projects\Program 16\Program 16\Debug\BuildLog.htm"
    1>Program 16 - 1 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    Attached Images Attached Images  

  2. #2
    Join Date
    Mar 2002
    Location
    NY, USA
    Posts
    12,097

    Re: Problem with ofstream...

    WHY do you think it should be able to find that header???
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009
    In theory, there is no difference between theory and paractice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Dec 2008
    Posts
    8

    Re: Problem with ofstream...

    oh crap you're right. lol
    okay that problem is solved.... but then I got 4 big errors telling me this...
    error in the text file....


    // Program 16.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include <string>
    #include <fstream>
    #include <iostream>
    using std:fstream;
    using std::ifstream;
    using std::cout;
    using std::cin;
    using std::endl;

    int main ( )
    {
    int i;
    struct record
    {
    char name[15];
    float amt;
    };
    record master[10] = {{"Helen", 10}, {"Julie", 20}, {"Lena", 30}, {"Alan", 40}, {"Annie", 50}, {"May", 60},
    {"Lee", 70}, {"Sam", 80}, {"June", 90}, {"Bill", 100}};
    record trans[7] = {{"Lena", 10}, {"Julie", 5.75}, {"Lee", 15.02}, {"Ed", 40}, {"Julie", 10.00}, {"Art", 5.00},
    {"Bill", 7.32}};
    ofstream foutmaster("master.dat");
    ofstream fouttrans("trans.dat");

    for (i=0; i<10; i++)
    {
    foutmaster << master.name[i] << master.amt[i] << endl;
    }

    for (i=0; i<7; i++)
    {
    fouttrans << trans.name[i] << trans.amt[i] << endl;
    }

    foutmaster.close("master.dat");
    fouttrans.close("trans.dat");

    }

    I just can't figure out how to solve this problem....
    Attached Files Attached Files
    I am using Visual C++ 2005 edition.
    I'm a high school student taking college courses to learn to program.
    I am planning on taking computer Science as a major, but I'll need a lot of effort and help.

    Thank you

  4. #4
    Join Date
    May 2006
    Posts
    21

    Re: Problem with ofstream...

    Quote Originally Posted by ch91woo View Post
    oh crap you're right. lol

    record master[10] = {{"Helen", 10}, {"Julie", 20}, {"Lena", 30}, {"Alan", 40}, {"Annie", 50}, {"May", 60},
    {"Lee", 70}, {"Sam", 80}, {"June", 90}, {"Bill", 100}};

    for (i=0; i<10; i++)
    {
    foutmaster << master.name[i] << master.amt[i] << endl;
    }
    I just can't figure out how to solve this problem....
    The "master" which you defined is an array of struct record.
    So, if you want to print the every element of master, I think you must do it like this:
    fourmaster << master[i].name << master[i].amt << endl;

    By the way, it is recommended that you move the definition of "record" out of the main()
    Last edited by gfzhang; December 7th, 2008 at 09:00 PM.

  5. #5
    Join Date
    Dec 2008
    Posts
    8

    Re: Problem with ofstream...

    ooohh okay. I fixed that.
    Now I have to close the file.
    I tried fouttrans.close();
    foutmaster.close();
    and fout.close();
    but none of these worked....
    how do I close these .dat files?

    // Program 16.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include <string>
    #include <fstream>
    #include <iostream>
    using std:fstream;
    using std::ifstream;
    using std::cout;
    using std::cin;
    using std::endl;

    int main ( )
    {
    int i;
    struct record
    {
    char name[15];
    float amt;
    };
    record master[10] = {{"Helen", 10}, {"Julie", 20}, {"Lena", 30}, {"Alan", 40}, {"Annie", 50}, {"May", 60},
    {"Lee", 70}, {"Sam", 80}, {"June", 90}, {"Bill", 100}};
    record trans[7] = {{"Lena", 10}, {"Julie", 5.75}, {"Lee", 15.02}, {"Ed", 40}, {"Julie", 10.00}, {"Art", 5.00},
    {"Bill", 7.32}};
    ofstream foutmaster("master.dat");
    ofstream fouttrans("trans.dat");

    for (i=0; i<10; i++)
    {
    foutmaster << master[i].name << master[i].amt << endl;
    }

    for (i=0; i<7; i++)
    {
    fouttrans << trans[i].name << trans[i].amt << endl;
    }
    }
    I am using Visual C++ 2005 edition.
    I'm a high school student taking college courses to learn to program.
    I am planning on taking computer Science as a major, but I'll need a lot of effort and help.

    Thank you

  6. #6
    Join Date
    May 2006
    Posts
    21

    Re: Problem with ofstream...

    Code:
    fouttrans.close();
    foutmaster.close();
    is enough.

    However, recommends you to use ofstream like this:
    Code:
    ofstream foutmaster;
    foutmaster.open("master.data", ... );
    ..........
    foutmaster.close();
    Last edited by gfzhang; December 7th, 2008 at 10:54 PM.

  7. #7
    Join Date
    Dec 2008
    Posts
    8

    Re: Problem with ofstream...

    oh okay. thanks. that's got the problem solved
    I am using Visual C++ 2005 edition.
    I'm a high school student taking college courses to learn to program.
    I am planning on taking computer Science as a major, but I'll need a lot of effort and help.

    Thank you

  8. #8
    Join Date
    May 2007
    Posts
    798

    Re: Problem with ofstream...

    Quote Originally Posted by gfzhang View Post
    However, recommends you to use ofstream like this:
    Code:
    ofstream foutmaster;
    foutmaster.open("master.data", ... );
    ..........
    foutmaster.close();
    why?

    Why not just his:
    Code:
    ofstream foutmaster("master.data", ... );
    // read data, etc.
    // when goes out of scope, it will close file automatically.

  9. #9
    Join Date
    Dec 2008
    Posts
    8

    Re: Problem with ofstream...

    Quote Originally Posted by STLDude View Post
    why?

    Why not just his:
    Code:
    ofstream foutmaster("master.data", ... );
    // read data, etc.
    // when goes out of scope, it will close file automatically.
    wait does that mean when the file doesn't close the VSC++ program brings up an error?
    I am using Visual C++ 2005 edition.
    I'm a high school student taking college courses to learn to program.
    I am planning on taking computer Science as a major, but I'll need a lot of effort and help.

    Thank you

  10. #10
    Join Date
    May 2006
    Posts
    21

    Re: Problem with ofstream...

    Quote Originally Posted by STLDude View Post
    why?

    Why not just his:
    Code:
    ofstream foutmaster("master.data", ... );
    // read data, etc.
    // when goes out of scope, it will close file automatically.

    It is "recommend", but not necessary.
    A good manner of coding, I think.
    Open it before using it, close it after using it.
    Otherwise, sometime in a big program, if another function want to access
    the ***.data, and foutmaster has not been closed yet, maybe problem or trouble.

  11. #11
    Join Date
    Dec 2008
    Posts
    8

    Re: Problem with ofstream...

    Quote Originally Posted by gfzhang View Post
    It is "recommend", but not necessary.
    A good manner of coding, I think.
    Open it before using it, close it after using it.
    Otherwise, sometime in a big program, if another function want to access
    the ***.data, and foutmaster has not been closed yet, maybe problem or trouble.
    oh okay. thanks :]
    I am using Visual C++ 2005 edition.
    I'm a high school student taking college courses to learn to program.
    I am planning on taking computer Science as a major, but I'll need a lot of effort and help.

    Thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center