Jump to content

adding int to string,creat a file named string..

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
emda321

emda321

    Newbie

  • Members
  • Pip
  • 3 posts
hi everyone..

i want to add an integer at the end of a given string,and then i want to creat and open a text file of the resulting string.example:name,1,the required path is ("d:\name1.txt).
i use this code but it never works,anybody know why??
char * output;
char * output2;
char  *add;
char * add1;    
int msgno;
msgno=1;
char name1[20];
stpcpy(name,"name");

output2=new char[2];
    
    ltoa((msgno+1),output2,10);
    output2[1]='\0';
    output=new char[strlen(name)+2];
    stpcpy(output,name);
    output=strcat(output,output2);
    output[strlen(name)+1]='\0';
    add=new char[(strlen(output))+5];
    add=strcat(output,".txt");
    add[(strlen(name))+5]='\0';
    add1=new char[(strlen(add))+4];
    add1=strcat("d:\\",add);
    add1[(strlen(name))+8]='\0';
    obj1.open(add1,ios::out);
    
    obj1.close();

    delete output;
    delete output2;
    delete add;
    delete add1;


#2
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
I see you're using a lot of C++ in your code, then why don't you use a C++way of doing this, instead of mixing C into it too. I don't know if you've to use C-strings, or just need the result in C-strings...

Anyways, try take a look at this:
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>

int main()
{
	int iInt = 100;
	std::string sStr = "This is the value: ";
	std::stringstream SS;
	std::ofstream hFile;
	
	SS << iInt;
	sStr += SS.str();
	
	hFile.open("your_file.ext");
	if(hFile.is_open())
	{
		hFile << sStr;
		hFile.close();
	}

	return 0;
}

If you need to have the result in a C-string, you can simply use the member of std::string, c_str:
	char *INeedACString = const_cast<char *>(sStr.c_str());
	std::cout << INeedACString << std::endl;


#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
why not use cout after each assignment to verify that each variable contains the value you expected?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
veda87

veda87

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
here is a C code to add an integer at the end of the string and generate an another string....

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


/* converts integer into string */
char* itoa(unsigned long num) {
        char* retstr = calloc(12, sizeof(char));
        if (sprintf(retstr, "%ld", num) > 0) {
                return retstr;
        } else {
                return NULL;
        }
}

int main ()
{
        unsigned long i;
        char *buffer;
        char *array = "abcd";
        int number;
        char *str;

        printf("Enter the number:");
        scanf("%d", &number);

        buffer = itoa(number);

        /* allocate the memory for the string
           of size length of buffer + array
           and +1 is for NULL termination */
        str = malloc((strlen(buffer) + strlen(array))+1);

        /* copy the string from array and buffer into the str */
        for(i=0; i < (strlen(buffer) + strlen(array)); i++) {
                if( i < strlen(array) ) {
                        str[i] = array[i];
                } else {
                        str[i] = buffer[i-(strlen(array))];
                }
        }

        /*terminate with NULL character */
        str[i] = '\0';
        free(buffer);
        printf("%s\n", str);

        return 0;
}


#5
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Wow... this one's old. :P
Wow I changed my sig!

#6
cheeta

cheeta

    Newbie

  • Members
  • Pip
  • 1 posts
try this code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<conio.h>


/* converts integer into string */
char* itoa(unsigned long num) {
        char* retstr;
        if (sprintf(retstr, "%ld", num) > 0) {
                return retstr;
        } else {
                return NULL;
        }
}

int main ()
{
        unsigned long i;
        char *buffer;
        char *array = "abcd";
        int number;


        printf("Enter the number:");
        scanf("%d", &number);

        buffer = itoa(number);
	strcat(array,buffer);
	printf("array is %s",array);
	printf("buffer is %s",buffer);
	getch();
        free(buffer);
        

        return 0;
}

Edited by WingedPanther, 10 September 2009 - 07:32 AM.
add code tags (the # button)