Functions and Creating Files

This is a discussion on Functions and Creating Files within the C++ Programming forums, part of the General Programming Boards category; Im working on a project that is supposed to let the user enter a title and some text and the ...

  1. #1
    Registered User
    Join Date
    May 2002

    Functions and Creating Files

    Im working on a project that is supposed to let the user enter a title and some text and the program is supposed to create the file and then give the user the option to view it, i am very lost at the moment and any help would be great

    void CreateText(void);

    int main()
    const int Max=80;
    char filename[Max]="c:\Text1.dat";
    char text[Max]="c:\Text1.dat";
    void CreateText(void);


    void CreateText(char filename[],char text[])
    char Text1;
    ofstream Text1_file;;
    cout<<" Enter the Title of the File.";
    cout<<" Enter the Text of the File.";
    return 0;

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    It's pretty straightforward, just open an output file. If it doesn't exist then the program will attempt to create it.
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    int main() 
      char ch;
      string fileName, fileContents;
      ifstream readFile;
      ofstream writeFile;
      cout<<"Enter a file to create (ex. somefile.txt): ";
      getline ( cin, fileName ); ( fileName.c_str() );
      if ( writeFile.good () ) {
        cout<<"File opened\nEnter a string to place in the file: ";
        getline ( cin, fileContents );
        cout<<"Would you like to view the file? (y/n): ";
        if ( ( ch = cin.get() ) == 'y' ) {
 ( fileName.c_str() );
          getline ( readFile, fileContents );
        else if ( ch == 'n' )
          cout<<"Have a nice day!\n";
          cout<<"Invalid input\n";
        cerr<<"File could not be opened\n";
      return 0;
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Prelude's code is excellent.

    For those using Borland:

    #include <conio.h>


    add a getch(); statement preceding return 0; at the end of main().

    (Just in case you want to play with it...)

    Some may not be aware...

    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating File Handling Functions
    By td4nos in forum C Programming
    Replies: 6
    Last Post: 06-26-2009, 12:43 PM
  2. Help creating multiple text files
    By Tom Bombadil in forum C Programming
    Replies: 19
    Last Post: 03-28-2009, 12:21 PM
  3. creating and using functions
    By moy18 in forum C Programming
    Replies: 2
    Last Post: 11-01-2007, 05:26 AM
  4. Creating Functions that use Format control Strings
    By tzuchan in forum C Programming
    Replies: 6
    Last Post: 03-29-2004, 12:50 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21