Results 1 to 3 of 3

Thread: Ofstream help!!!

  1. #1
    Junior Member
    Join Date
    Jul 2004
    Posts
    19

    Ofstream help!!!

    Hey,
    I am writing a C++ program that uses the "ofstream" function. The output is a C++ file and in the output file I need to use char function.
    But only one problem is that i can't add the qutation marks. Here is the code:

    #include <fstream.h>
    #include <iostream.h>
    #include <stdio.h>
    #include <stdlib.h>

    int main( )
    {
    char *nme = new char[50];
    char exten = ".cpp";

    cout<<"Name of the file:\n";
    cin>>nme;

    int fname = strcat(nme, exten);

    ofstream a_file(fname);
    a_file<<"#include <iostream.h>\n";
    a_file<<"#include <stdlib.h>\n";
    a_file<<"\n";
    a_file<<"int main( )\n";
    a_file<<"{\n";
    a_file<<" char a = "

    and that is how far i can get because it will not let me add quotation marks to the outputed file. If you can help me please do.Thanks

  2. #2
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    You simply need to tell the compiler that what you want is the ascii character ' rather than that you are specifying a character:

    Code:
    a_file << "char a = \\'a\\';";
    The following small c program will demonstrate it to you:

    Code:
    int main()
    {
            char string[20] = "char a = \\'a\\';";
    
            printf("%s\n", string);
    
            return(0);
    }
    Code:
    [ac@ezranet c]$ gcc test.c -o test
    [ac@ezranet c]$ ./test
    char a = 'a';
    [ac@ezranet c]$
    Hope that helped.

    ac

  3. #3
    Junior Member
    Join Date
    Jul 2004
    Posts
    19
    how do i print an ascii value in C++?

Posting Permissions

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