Jump to content

need help in adding a charecter to the file name

- - - - -

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

#1
swathin2

swathin2

    Newbie

  • Members
  • PipPip
  • 18 posts
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

#2
swathin2

swathin2

    Newbie

  • Members
  • PipPip
  • 18 posts
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

#3
veda87

veda87

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
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..
FILE *filePtr;

filePtr = fopen("FILE NAME", "r");


if(filePtr == NULL) {

        printf("FILE DOESNOT EXISTS \n");

        exit(1);

}

fclose(filePtr);



#4
swathin2

swathin2

    Newbie

  • Members
  • PipPip
  • 18 posts
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

:
:
:

#5
veda87

veda87

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
Here is the C program to add an integer at the end of the string and create 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;
}

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
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
veda, remember, you need to free your malloc'd resources and the char array returned by itoa. :)
Wow I changed my sig!

#7
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts
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
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
Fixed:

dargueta said:

If itoa() is defined in stdlib.h, you don't need to write your own version.
:p

#9
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts
I need to read fine print better...
FAIL.
sudo rm -rf /