#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    New Zealand
    Posts
    2
    Rep Power
    0

    Help! How can I turn an integer into a string?


    Hi :) As part of a comp sci assignment I am doing, I need to read an integer value from a plain text file, and turn it into a string. This string will be used a filename. I have used getc to obtain a count of the number of chars in the integer text file, and then I have used getc again to copy the integers one by one into an array.
    After that, I'm using strcat to append an extension (.dat) to the array. The problem is that the output I get is invariably:
    "10000
    .dat"
    Obviously for use as a file, I need "100000.dat"
    I don't know where the newline is coming from, or how to get rid of it? If my function is reading only integer chars, how is a newline value getting in there?

    Given that the text file contains 123456, when I run the code I get:

    breaking, g is less than 0 char
    number of chars in integer: 6
    q = 1234(.dat

    I hope this all makes sense! I am banging my against the wall here, I cannot understand what is going on. I think I can use sprintf maybe as an alternative?

    Here's the code I have:

    --Edit: I tried to get this to display formatting, but it didn't work. Sorry. Hope it's not too hard to read--

    #include <stdio.h>
    #include <string.h>

    int main() {
    int w; /*FOR counter*/
    int g; /*for getch return*/
    int h; /*for the length of the file*/
    char* q;
    FILE *fp;
    fp = fopen("album_count.txt", "r+");
    /*This function counts the number of integers in the file*/
    for (h = 0; ; h++) {
    g = fgetc(fp);
    /*Test for integer char values below 48 (0)*/
    if ((g - 48) < 0) { printf("breaking, g is less than 0 char\n");
    break;
    }
    /*Test for integer char values above 9 (57)*/
    if ((g - 57) > 0) { printf("breaking, g is greater than 9 char\n");
    break;
    }
    }
    char str[h+4]; /*h = number of characters in file, +4 for the file extension*/
    rewind(fp);
    printf("number of chars in integer: %d\n", h);
    for (w = 0; w < h; w++) {
    g = fgetc(fp);
    if ((g - 48) < 0) { printf("breaking, g is less than 0 char\n");
    break;
    }
    if ((g - 57) > 0) { printf("breaking, g is greater than 9 char\n");
    break;
    }
    str[w] = g;
    }
    q = strcat(str, ".dat");
    printf("q = %s\n", q);
    }


    Cheers,
    Craig
    Last edited by Vosper; September 26th, 2003 at 02:44 AM.
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    New Zealand
    Posts
    2
    Rep Power
    0

    Solved


    sprintf seems to work fine, so I think I'm sweet.
    Cheers,
    Craig
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Romania
    Posts
    3
    Rep Power
    0
    try _itoa();
    check MSDN for details.

IMN logo majestic logo threadwatch logo seochat tools logo