Thread: Creating files

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    118

    Creating files

    Please can anyone tell me what is wrong with this function.I am trying to create a text file but its not working
    Code:
    void Grades ::createFile(){
      string s;
    string filename;
    cout<<"please enter the name you want to name your file";
    cin>>filename;
    
    cout<<"please enter the text you want to create";
        cin>>s;
        ofstream mfile;
      mfile.open(filename);
      mfile<<s;
      mfile.close();
    }
    Last edited by sigur47; 02-17-2012 at 03:08 PM.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You need to use filename.c_str() in your open call.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by oogabooga View Post
    You need to use filename.c_str() in your open call.
    I dont know what you mean by open call.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    This line
    Code:
     mfile.open(filename);
    should be this
    Code:
     mfile.open(filename.c_str());
    Also, it would be better to read the filename and the text to write to the file with getline, like this:
    Code:
    getline(cin, filename);

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    "call" is what you say or how you describe it when you use a function, in your code; be it your own or one in a library.

    Code:
    #include <iostream>
    
    using namespace std;
    
    void MyFunction() //declare it - this is the simplest example.
    {
    
    	cout << "I called MyFunction()...!!!\n";
    }
    
    int main()
    {
    	cout << "I am about to call MyFunction, .... lock up your daughters!\n";
    	MyFunction(); 	//call it
    	
    	return 0;
    }
    Equally when your code has " myString.c_str() " you called a member function (member of the class string) that returns a null terminated c-style string.
    Last edited by rogster001; 02-17-2012 at 04:03 PM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Could also turn on C++0x support - if your compiler has support, using the switch will compile your code as is. Look:
    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string s;
        string filename;
        cout<<"please enter the name you want to name your file";
        cin>>filename;
    
        cout<<"please enter the text you want to create";
        cin>>s;
        ofstream mfile;
        mfile.open(filename);
        mfile<<s;
        mfile.close();
    }
    
    /* 
    mingw32-g++.exe -std=c++0x -Wall  -g     -c "C:\Documents and Settings\User\My Documents\sandbox\sandbox.cpp" -o obj\Debug\sandbox.o
    Process terminated with status 0 (0 minutes, 0 seconds)
    0 errors, 0 warnings
    */
    Neat right?

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by whiteflags View Post
    Could also turn on C++0x support - if your compiler has support, using the switch will compile your code as is. Look:
    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string s;
        string filename;
        cout<<"please enter the name you want to name your file";
        cin>>filename;
    
        cout<<"please enter the text you want to create";
        cin>>s;
        ofstream mfile;
        mfile.open(filename);
        mfile<<s;
        mfile.close();
    }
    
    /* 
    mingw32-g++.exe -std=c++0x -Wall  -g     -c "C:\Documents and Settings\User\My Documents\sandbox\sandbox.cpp" -o obj\Debug\sandbox.o
    Process terminated with status 0 (0 minutes, 0 seconds)
    0 errors, 0 warnings
    */
    Neat right?
    interesting would try it out and find out how it goes

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    These two can also be merged:

    ofstream mfile;
    mfile.open(filename);

    ofstream mfile(filename);

    And this

    mfile.close();

    is just plain unnecessary. The destructor will automatically call close.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating files
    By ginom71 in forum C Programming
    Replies: 7
    Last Post: 07-11-2009, 08:45 PM
  2. Creating Files
    By Halo2Master in forum C++ Programming
    Replies: 3
    Last Post: 07-20-2005, 02:49 PM
  3. Creating Files
    By Seb in forum C++ Programming
    Replies: 5
    Last Post: 06-15-2003, 08:11 PM
  4. creating .DAT files
    By breed in forum C Programming
    Replies: 3
    Last Post: 11-30-2001, 09:09 AM
  5. creating + using files
    By dave in forum C++ Programming
    Replies: 1
    Last Post: 10-26-2001, 09:49 PM