Thread: Newbie: casting variables?

  1. #1
    Sanglier
    Guest

    Question Newbie: casting variables?

    Firstly I've just started trying to learn C, so if this is a dumb question appologies in advance!
    I have a loop with an int variable incrementing for each iteration, I want to write a file in the loop with the variable value as the file name. I obviously can't cast the int to char[] as this will output the ASCII character..... is there a way to do this? (I'm using a C++ compiler so it doesn't have to be C specific)

    Thankyou

    And a big thankyou to everyone who's written the tutorials!

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    If you want the integer value 65 to become the string value "65", then use itoa().
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Sanglier
    Guest
    Thankyou

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >If you want the integer value 65 to become the string value "65", then use itoa().
    itoa() isn't a standard function, it would be better/easier/less foogly(Yes, I made that up) to use sprintf. Another option is string streams, but that is C++ and off-topic for this board.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Sanglier
    Guest
    Originally posted by Prelude
    itoa() isn't a standard function, it would be better/easier/less foogly(Yes, I made that up) to use sprintf.
    -Prelude
    foogly I like
    I hadn't seen your post before I went home, so I've used itoa (and it works ) so a general question - should you avoid all non-standard functions? for itoa you have to include stdlib.h, isn't this included in all compilers? should you try to only use ANSI functions? are POSIX ok? Is the only problem portability, or are standard functions more efficient as well? At the moment I'm more interested in getting something that works on my PC, but I'd like to avoid bad habits!

    Some other questions, I'm using a for loop with the int value, in the loop I need to open a file, copy the contents to another file with filename the string value of the integer value with a file extension (*.in), append two lines of text to the file including string values for the int value of the iteration. Because I need to create the file with string value of int value of the iteration (with extension) and later use the string value without the file extension, I've used itoa to generate the string value (nfin), defined a new string variable (runnumber), set it equal to nfin, used strcat to append the extension to nfin, then used runnumber to append the string value in the file.... it works but is there a better way of doing it?:
    Code:
    int iruns;
    char nfin[100];
    
    for (iruns = 0; iruns < nruns; iruns++)
    	{
    		itoa(iruns+1, nfin, 10); \*iruns int to string*\
                                    char runnumber[100];
                                    runnumber = nfin;
    		strcat(nfin, ".in");
    
    		ftemp = fopen("input.temp","r");
    		fin = fopen(nfin, "a");
    
    		while ((character = fgetc(ftemp)) != EOF)
    		{
    		   fputc(character, fin);
    		}
    
    		fputs("database = D:\\", fin);
    		fputs(runnumber, fin);
    		fputs(".txt\n", fin);
    		fputs("path-filename = D:\\", fin);
    		fputs(runnumber, fin);
    		fputs(".res\n", fin);
      		fclose(fin);
    do more stuff.........
    later in the loop I need to run a console application with a path to the this *.in file that I've created. I've used spawnl(P_WAIT, "prog.exe", " nfin.in"); the problem here is I need to insert a space before the file name, so I can't just use nfin, so I've defined *another* string (runfile) as a space, and appended nfin using strcat:
    Code:
            	char runfile[100] = " ";
    		strcat(runfile, nfin);
    		spawnl(P_WAIT, "Chess.exe", runfile);
    Again it works, but I'm sure there's a better way of doing it.... Any advice will be much appreciated

    Apologies for the long post and thankyou!

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>>so a general question - should you avoid all non-standard functions?
    If you're coding for personal use, on a single platform, then I see nothing wrong in using whatever function suits your needs, but at least be aware of what's what. However, if you are coding for a wider audience, you should really use standard functions where ever possible, that way you guaranteed your code will work. When posting answers to questions on this board, always try to use standard functions when they are available. If you do post non-standard code,, make sure you highlight this fact, and at least give a short reason why you've done so. (imho!).

    >>for itoa you have to include stdlib.h, isn't this included in all compilers?
    itoa() is included in stdlib.h, as you said, but only in some compilers. As it isn't a standard function, it isn't going to be in everyones stdlib.h file.

    >>Is the only problem portability, or are standard functions more efficient as well?
    Non-standard functions can be more versatile, and in some case, I'm sure they are more efficient too, as they are often designed to interact with a specific OS.

    >>At the moment I'm more interested in getting something that works on my PC, but I'd like to avoid bad habits!
    A wise plan

    Some other questions, I'm using a for loop with the int value, in the loop I need to open a file, copy the contents to another file with filename the string value of the integer value with a file extension (*.in), append two lines of text to the file including string values for the int value of the iteration. Because I need to create the file with string value of int value of the iteration (with extension) and later use the string value without the file extension, I've used itoa to generate the string value (nfin), defined a new string variable (runnumber), set it equal to nfin, used strcat to append the extension to nfin, then used runnumber to append the string value in the file.... it works but is there a better way of doing it?:
    Wow, that was a mouthful! Without deciphering what you want, I going to suggest you review sprintf() and fprintf(). If these don't suit your needs, can you narrow down your question a little (my head hurts after reading that last one!).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    5
    Thanks for taking the time to reply Hammer, I only started learning last week, I've done no programming before & I started with the impression that C is C is C (is a bit of C++).... I'm finding out otherwise

    Originally posted by Hammer
    Wow, that was a mouthful! Without deciphering what you want, I going to suggest you review sprintf() and fprintf(). If these don't suit your needs, can you narrow down your question a little (my head hurts after reading that last one!).
    Sorry about that I think I tried to do too much at once, I've broken down the code into several functions which are a bit easier to keep a handle on. The functions all work up to a point now.... I think my remaining problem is due to still using itoa() in a loop - it works for n upto about 500 then crashes (I need n=10^4), don't understand why but I'll try sprintf later..... I'd like to figure this out myself if I can - I might learn something
    Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to avoid using global variables
    By Canadian0469 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2008, 12:02 PM
  2. Father and Son Variables
    By khdani in forum Linux Programming
    Replies: 3
    Last Post: 11-28-2008, 06:42 PM
  3. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  4. Headers and Global Variables
    By ajoo in forum C++ Programming
    Replies: 1
    Last Post: 05-15-2004, 04:49 AM
  5. Replies: 6
    Last Post: 01-02-2004, 01:01 PM