none
Creating and Opening a Sequential Access File in C++

    Question

  • I am a twenty four year old college student taking a class in basic c++ programming. I am using the Visual C++ .NET Express software and I need some help.

    A while ago I had an assignment to complete involving the ifstream and ofstream classes from the "fstream.h" library.

    I was supposed to create an output file object that I could write information to and then create an input file object that could send that information back into the program to be used.

    I have used *&!@ near every example my textbook has to offer and just don't know where to turn for this. This assignment isn't going to get done.

    I just want to know from someone who is better at this than me, how do you create a sequential access file, write and read to it.

    This is what I have so far

    H:\Chapter

    #include <iostream>

    #include <fstream>

    using std::fstream;

    using std::cout;

    using std::cin;

    using std::endl;

    int main ()

    {

    ofstream outFile;

    outFile.open("H:\Chapter 13\Chapter 13 Data", ios::out);
     

    return 0;

    }

    Will someone please tell me what went wrong?



    Thursday, December 01, 2005 7:41 PM

Answers

  • The following sample should work:

    #include <iostream>
    #include <fstream>
    using namespace std;
    int main ()
    {
     ofstream outFile;
     outFile.open("c:\\bug\\forum.log", ios::out);
     outFile.write ("Anything",sizeof("Anything"));
     outFile.close();
     return 0;
    }


    Note the changes in RED

    For more details, please take a look at http://msdn2.microsoft.com/en-us/library/59zy697d.aspx

    Note: I would really advise you to dig into the help at http://msdn2.microsoft.com first since this will help you understand the language better.

    Thanks,
     Ayman Shoukry
     VC++ Team

    Thursday, December 01, 2005 10:07 PM
    Moderator

All replies

  • The following sample should work:

    #include <iostream>
    #include <fstream>
    using namespace std;
    int main ()
    {
     ofstream outFile;
     outFile.open("c:\\bug\\forum.log", ios::out);
     outFile.write ("Anything",sizeof("Anything"));
     outFile.close();
     return 0;
    }


    Note the changes in RED

    For more details, please take a look at http://msdn2.microsoft.com/en-us/library/59zy697d.aspx

    Note: I would really advise you to dig into the help at http://msdn2.microsoft.com first since this will help you understand the language better.

    Thanks,
     Ayman Shoukry
     VC++ Team

    Thursday, December 01, 2005 10:07 PM
    Moderator
  • I can help you

    what you entered wrong is the file location

    ("H:\Chapter 13\Chapter 13 Data", ios:Surpriseut);
    you bust write   \\   instead of   \   like this 

    ("H:\\Chapter 13\\Chapter 13 Data.txt", ios:Surpriseut);

    don't forget the file extention

    ex:

        

    ofstream outFile;

    outFile.open("c:\\folder1\\folder2\\folder3\\textfile.txt", ios:Surpriseut);

    Friday, April 06, 2007 11:21 AM