Hello,
if the account number is "abcd" then i need to have to check whether "abcd1" or "abcd2" or ...... is existing or not.
if it is not existing then i need to create it.
so how can i do this
any suggestions
need help in adding a charecter to the file name
Started by swathin2, Sep 07 2009 10:46 PM
8 replies to this topic
#1
Posted 07 September 2009 - 10:46 PM
|
|
|
#2
Posted 07 September 2009 - 11:36 PM
hello see i have an account name abcd already exixting in DB
and i need to get that account number from the DB and then check in a particular location that, that particular file is present or not like abcd1 or2 or 3...
and then suppose if abcd2 is present then it should create a new file by the name abcd3
like this it should go on
now i hope you can help me
and i need to get that account number from the DB and then check in a particular location that, that particular file is present or not like abcd1 or2 or 3...
and then suppose if abcd2 is present then it should create a new file by the name abcd3
like this it should go on
now i hope you can help me
#3
Posted 07 September 2009 - 11:41 PM
Are you using C or C++...
Anyway for adding an integer at the end of the string, do check out this linkhttp://forum.codecal...med-string.html
For check whether the file is present or not, just try opening the file..
Anyway for adding an integer at the end of the string, do check out this linkhttp://forum.codecal...med-string.html
For check whether the file is present or not, just try opening the file..
FILE *filePtr;
filePtr = fopen("FILE NAME", "r");
if(filePtr == NULL) {
printf("FILE DOESNOT EXISTS \n");
exit(1);
}
fclose(filePtr);
#4
Posted 07 September 2009 - 11:49 PM
i am using c itself
in the above program it will help us in creating a file if that doesnt exist but how to add a integer value to the end of the file name
like abcd1
abcd2
abcd3
:
:
:
in the above program it will help us in creating a file if that doesnt exist but how to add a integer value to the end of the file name
like abcd1
abcd2
abcd3
:
:
:
#5
Posted 08 September 2009 - 02:32 AM
Here is the C program to add an integer at the end of the string and create an another string...
Use this code to generate abcd1, abcd2,.....
#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;
}
Use this code to generate abcd1, abcd2,.....
Edited by veda87, 08 September 2009 - 02:52 AM.
Previously Didn't free the memory allocated for buffer, but now its freed.
#6
Posted 08 September 2009 - 02:42 AM
veda, remember, you need to free your malloc'd resources and the char array returned by itoa. :)
Wow I changed my sig!
#7
Posted 08 September 2009 - 04:53 PM
itoa() is defined in stdlib.h, you don't need to write your own version. In fact, you can't--standard C does not support function overloading.
sudo rm -rf /
#8
Posted 08 September 2009 - 05:39 PM
Fixed::p
dargueta said:
If itoa() is defined in stdlib.h, you don't need to write your own version.
#9
Posted 08 September 2009 - 05:46 PM


Sign In
Create Account


Back to top









