Thread: int to string question

  1. #1
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373

    int to string question

    I wish to make files for enemies in a gme, but they're so many enemies, that i do not wish to do a if else ife to open all of them for battle. I want to randomize an int, and open a file with that number.

    is there a way that i can do this? I have tried making int a = <num>;
    char b = a;
    ifstream fin(b);

    It wouldn't get errors, but it would not open, and i assume that is because it is looking for the ascii symbol.

    Thanks ahead of time.
    This war, like the next war, is a war to end war.

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Use sprintf() or one of the string stream classes.

    int a;
    char FileName[20];
    a = 23;
    sprintf(Filename, "File%n.dat", a); // for example
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    Ill give that a quick try. thanks
    This war, like the next war, is a war to end war.

  4. #4
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <fstream>
    #include <conio.c>
    using namespace std;
    int main()
    {
    int a;
    char Filename[20];
    a = 23;
    sprintf(Filename, "%n.txt", a);
    ifstream fin(Filename);
    char ch;
    while(fin.get(ch))
    {
    cout << ch;
    }
    getche();
    }
    The program perfroms an illegle operation and crashes.
    This war, like the next war, is a war to end war.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    sprintf is that way, you probably want
    sprintf(filename,"%d.txt",a);
    %n writes the number of characters written so far to the int pointer in the corsiponding position. So your program wrote 0 to memory location 23, this is rarely a good thing. Be careful of the character after the dot being interpreted as precision as well, if your extention was .5xt you would get "23 xt" (i think) thus you may really want sprintf(filename,"%d\.txt",a);

    All the cool kids use std::ostringstream these days.

    std::ostringstream oss;
    oss << a << ".txt";
    ifstream fin(oss.str().c_str());

    boost::lexical_cast also has it goin on.

    std::string filename = boost::lexical_cast<std::string>(a) + ".txt";
    ifstream fin(filename.c_str());

  6. #6
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    Oh yea? well all the cool kids can kiss my uncool butt! the %d works swell, though i must point out one error in your code
    leave out the '\', or you will get "Unknown esacape sequence"

    Thanks man!

    Now i just need to figure out to print a txt file in dos console with the HP 3820 Deskjet printer, but i cant find a tutorial... (HINT HINT)
    Last edited by Blizzarddog; 11-10-2003 at 10:06 AM.
    This war, like the next war, is a war to end war.

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    hmmm, "\." should be a legit excape sequence, in fact \q where q is not part of a legit escape sequence should be just q. It's not important. Printing causes endless grief there is a dos "print" command that might work if windows wants it to. I just redirect to a file and open in wordpad if I want a hard copy of something.
    foo > file.txt
    wordpad file.txt
    then click print

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > in fact \q where q is not part of a legit escape sequence should be just q.
    Nope - illegal escape sequences require a diagnostic from the compiler.

    All the unused lower case letters are reserved, so you really wouldn't want your code doing something new and interesting just because you used a previously undefined escape sequence.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    thanks. You can get the project, which you can now add your own enemies, at blizzarddog.tk
    get the music pack as well, and put the music files into the directory of the exe.
    This war, like the next war, is a war to end war.

  10. #10
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Originally posted by Salem

    Nope - illegal escape sequences require a diagnostic from the compiler.

    All the unused lower case letters are reserved, so you really wouldn't want your code doing something new and interesting just because you used a previously undefined escape sequence.
    You have no sense of adventure, also after actually looking up conversion specifications '.' only appears after the % and before conversion type. oops. gcc accepts \. as an escape for '.' don't know why now, might be a bug.

  11. #11
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    odd.. im using Dev CPP with GCC.
    Oh heck, \a doesn't do anything either..
    This war, like the next war, is a war to end war.

  12. #12
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    oh and uhm, the game is uploaded.
    This war, like the next war, is a war to end war.

  13. #13
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Strange, I usually build with -Wall --ansi --pedantic and had that in my Dev-Cpp but some how this project did not "inherit" those settings. I get the warning when I build from the command line but my defaults have been screwed up. Sometimes IDE's apparently save you work by lying to you I guess it does make sence to warn people about slashdot

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > You have no sense of adventure
    If you define "adventure" as having to relearn the language everytime you change compiler vendor / version then knock yourself out.

    I found that game became very old very quick a long time ago.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Replies: 26
    Last Post: 11-30-2007, 03:51 AM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM