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
    Posts
    1

    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
    thanks

    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;
    Text1_file.open(filename);
    cout<<" Enter the Title of the File.";
    Text1<<text;
    cout<<" Enter the Text of the File.";
    Text1<<text;
    Text1.close();
    return 0;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    It's pretty straightforward, just open an output file. If it doesn't exist then the program will attempt to create it.
    Code:
    #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 );
      writeFile.open ( fileName.c_str() );
    
      if ( writeFile.good () ) {
        cout<<"File opened\nEnter a string to place in the file: ";
        getline ( cin, fileContents );
        writeFile<<fileContents;
        writeFile.close();
        cout<<"Would you like to view the file? (y/n): ";
        if ( ( ch = cin.get() ) == 'y' ) {
          readFile.open ( fileName.c_str() );
          getline ( readFile, fileContents );
          cout<<fileContents<<"\n";
          readFile.close();
        }
        else if ( ch == 'n' )
          cout<<"Have a nice day!\n";
        else
          cout<<"Invalid input\n";
      }
      else
        cerr<<"File could not be opened\n";
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

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

    For those using Borland:

    #include <conio.h>

    and

    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, 11:43 AM
  2. Help creating multiple text files
    By Tom Bombadil in forum C Programming
    Replies: 19
    Last Post: 03-28-2009, 11:21 AM
  3. creating and using functions
    By moy18 in forum C Programming
    Replies: 2
    Last Post: 11-01-2007, 04:26 AM
  4. Creating Functions that use Format control Strings
    By tzuchan in forum C Programming
    Replies: 6
    Last Post: 03-29-2004, 11:50 AM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

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