C and C++ Programming Resources

need help in adding a charecter to the file name

Complete discussion on C/C++ and Object Oriented Behavior of C++.

need help in adding a charecter to the file name

Postby swathin2 » Tue Sep 08, 2009 7:55 am

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
swathin2
 
Posts: 4
Joined: Tue Sep 08, 2009 7:50 am

Re: need help in adding a charecter to the file name

Postby swathin2 » Tue Sep 08, 2009 8:37 am

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
swathin2
 
Posts: 4
Joined: Tue Sep 08, 2009 7:50 am

Re: need help in adding a charecter to the file name

Postby dman » Wed Sep 09, 2009 11:57 pm

could just do this:

Code: Select all
/****************************************************************
* File Name : c:\programs\tempCG.cpp
* Date : September,9,2009
* Comments : new project
* Compiler/Assembler : Visual C++ 6.0
* Modifications :
*
*
*
*
*
* Program Shell Generated At: 6:23:04 p.m.
=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/


#include < stdio.h >
#include < string.h >
#include < stdlib.h >
//#include < math.h >
//#include < iomanip >
//#include < ctype.h >



//main function ******************************

int main ( )
{
   
    char str [ 20 ];
    char numberStr [ 10 ];
   
    strcpy ( str , "c:\\data\\file" );
   
    int stringLength = strlen ( str );
   
    int j = 0;
    while ( j < 10 )
    {
       
        itoa ( j , numberStr, 10 );
        strcpy ( str + stringLength , numberStr );
        printf ( "%s" , str  );
        printf ( "\n" );
      FILE * file = fopen ( str , "a" );
      //..................
      j ++;
    }
   return 0 ;
}


dman
 
Posts: 94
Joined: Mon Oct 06, 2008 6:47 pm

Re: need help in adding a charecter to the file name

Postby sganesh » Thu Feb 25, 2010 9:58 am

Try this code for you requirement It will solve you problem.
As you want it will check for abcd if it is there it will check for abcd1 file.

Code: Select all
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<limits.h>

char *reverse(char s[])
{
        int c,i,j;

        for(i=0,j=strlen(s)-1;i<j;i++,j--)
        {
                c=s[i];
                s[i]=s[j];
                s[j]=c;
        }
        return s;
}



char  *itoa(int n)
{
        char s[100];
        int i,sign,ind=0,mod,w=0;
        if((sign=n)<0){
                if(sign<=INT_MIN)
                {
                        mod=-(n%10);
                        n=n/10;
                        ind=1;
                }
                n=-n;
        }
        i=0;
        do
        {
                if(ind==1)
                {
                        s[0]=mod+'0';
                        i++;
                        ind=0;
                }
                s[i++]=n%10+'0';

        }while((n/=10)>0);
        s[i]='\0';
        return (reverse(s));
}

int exists(const char *filename)
{
        FILE *f = fopen(filename, "r");
        if (!f) return 0;
        fclose(f);
        return 1;
}

int main ( )
{
        char str [1024];
        strcpy(str,"abcd");
        int num = 0;
        int created = 0;
        while(created == 0)
        {
                num++;
                if(exists(str))
                {
                        char numb[100];
                        strcpy(str,"abcd");
                        strcpy(numb,"");
                        strcpy(numb,itoa(num));
                        strcat(str,numb);
                }
                else
                {
                        FILE *f=fopen(str,"w");
                        printf("%s file created\n",str);
                        created=1;
                        fclose(f);
                }
        }
        return 0 ;
}
sganesh
 
Posts: 2
Joined: Thu Feb 25, 2010 7:53 am


Return to C/C++ Programming Language

Who is online

Users browsing this forum: No registered users and 0 guests

cron