I can't output this, no matter how hard I try

This is a discussion on I can't output this, no matter how hard I try within the C++ Programming forums, part of the General Programming Boards category; I try to output a specific character into a file, but everytime I do, it just stands "unterminated string or ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    367

    Unhappy I can't output this, no matter how hard I try

    I try to output a specific character into a file, but everytime I do, it just stands "unterminated string or character constant" and "possible real start of unterminated constant".

    It's not a: \, a: ' or a: " which I'm trying to output, it's another character.

    I've also tried to put a: \, before the character, but it doesn't help.

    Although I know that it's possible to output this character because I have another program that does it.


    I haven't got a clue!

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    What's the mystery character?

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    367

    Unhappy It would be an , but I'm not sure if it can "survive" through HTML

    Else, it would look like this: ^Z, even though it's not the same character as the one I'm talking about.


    *Still: *

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    If you are trying to write ^Z, I think you might have problems because I think that it might be the EOF representation. If not, ignore me but I just thought that I read that before.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Well, you'll need to be writing in binary mode (if using ofstream, use ios::binary).

    Then, make an unsigned character which contains the ASCII code (26) you want to write, then output that, without using the stream operator <<, which sometimes screws up format characters (esp. CR/LF) .

    Something like this:

    unsigned char c = 26;
    fileOut.write(&c,1);

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    367

    Sorry, but I'm not very good at this

    I've tried a number of variants of your suggestions but nothing works, so I just hand the code over to you:


    #include <fstream>

    int main()
    {
    std::cout << "Creating file..." << std::endl;
    std::ofstream thefile("thefile.dat");

    std::cout << "Writing to file..." << std::endl; thefile << "other text";

    return 0;
    }


    What should I replace the  with, and how can I get it's binary value? And should I also convert the other text to binary format?

    Please fix this code once and for all.
    Last edited by Zewu; 01-25-2002 at 02:19 PM.

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    I think you need to #include <iostream> for cout, etc; maybe fstream will cover it, but haven't seen it used that way.
    Also, why do you have a semi-colon after std::endl?
    You might try cout'ingthe Ctrl-Z character by itself. But why do you want it? Just close the file. Ctrl-Z is usually entered by the user to signify EOF when interacting with the program. Maybe you want to check for EOF some other way. There are ways to do it.
    If I'm misreading what you're trying to do, sorry.

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    412

    Re: Sorry, but I'm not very good at this

    Originally posted by Zewu
    I've tried a number of variants of your suggestions but nothing works, so I just hand the code over to you:


    #include <fstream>

    int main()
    {
    std::cout << "Creating file..." << std::endl;
    std:fstream thefile("thefile.dat");

    std::cout << "Writing to file..." << std::endl; thefile << "other text";

    return 0;
    }


    What should I replace the  with, and how can I get it's binary value? And should I also convert the other text to binary format?

    Please fix this code once and for all.
    Have you tried opening the stream like this:

    ofstream thefile("thefile.dat",ios::binary);

  9. #9
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Fstream is derived from iostream, ifstream, and ofstream. Therefore, when including it, you do not need to include iostream. If you do, it might even give you an error.

  10. #10
    Señor Member
    Join Date
    Jan 2002
    Posts
    561
    According to what my compsci teacher says it won't give you an error ever. It's simple, fstream uses iostream; fstream.h incudes iostream.h.

  11. #11
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    That's cool, I never knew that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 02:29 PM
  2. Output to csv file -is this hard?
    By pastitprogram in forum C++ Programming
    Replies: 3
    Last Post: 06-26-2008, 04:59 AM
  3. Replies: 4
    Last Post: 11-30-2005, 03:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 04:27 AM

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