Home › Forums › C Programming › need help in adding a charecter to the file name
- This topic has 3 replies, 3 voices, and was last updated 14 years, 9 months ago by LenoraRamsey.
- AuthorPosts
- September 8, 2009 at 6:55 am #2228Archie7891Participant
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
- September 8, 2009 at 7:37 am #3608Archie7891Participant
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
- September 9, 2009 at 10:57 pm #3609GWILouisaxwzklaParticipant
could just do this:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354<br />/****************************************************************<br />* File Name : c:programstempCG.cpp<br />* Date : September,9,2009<br />* Comments : new project<br />* Compiler/Assembler : Visual C++ 6.0<br />* Modifications :<br />*<br />*<br />*<br />*<br />*<br />* Program Shell Generated At: 6:23:04 p.m.<br />=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /><br /><br />#include < stdio.h ><br />#include < string.h ><br />#include < stdlib.h ><br />//#include < math.h ><br />//#include < iomanip ><br />//#include < ctype.h ><br /><br /><br /><br />//main function ******************************<br /><br />int main ( )<br />{<br /><br />char str [ 20 ];<br />char numberStr [ 10 ];<br /><br />strcpy ( str , "c:\data\file" );<br /><br />int stringLength = strlen ( str );<br /><br />int j = 0;<br />while ( j < 10 )<br />{<br /><br />itoa ( j , numberStr, 10 );<br />strcpy ( str + stringLength , numberStr );<br />printf ( "%s" , str );<br />printf ( "n" );<br />FILE * file = fopen ( str , "a" );<br />//..................<br />j ++;<br />}<br />return 0 ;<br />}<br /><br /><br /> - February 25, 2010 at 8:58 am #3610LenoraRamseyParticipant
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.1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586<br />#include<stdio.h><br />#include<string.h><br />#include<stdlib.h><br />#include<limits.h><br /><br />char *reverse(char s[])<br />{<br />int c,i,j;<br /><br />for(i=0,j=strlen(s)-1;i<j;i++,j--)<br />{<br />c=s<i>;<br />s</i><i>=s[j];<br />s[j]=c;<br />}<br />return s;<br />}<br /><br /><br /><br />char *itoa(int n)<br />{<br />char s[100];<br />int i,sign,ind=0,mod,w=0;<br />if((sign=n)<0){<br />if(sign<=INT_MIN)<br />{<br />mod=-(n%10);<br />n=n/10;<br />ind=1;<br />}<br />n=-n;<br />}<br />i=0;<br />do<br />{<br />if(ind==1)<br />{<br />s[0]=mod+'0';<br />i++;<br />ind=0;<br />}<br />s[i++]=n%10+'0';<br /><br />}while((n/=10)>0);<br />s</i><i>='';<br />return (reverse(s));<br />}<br /><br />int exists(const char *filename)<br />{<br />FILE *f = fopen(filename, "r");<br />if (!f) return 0;<br />fclose(f);<br />return 1;<br />}<br /><br />int main ( )<br />{<br />char str [1024];<br />strcpy(str,"abcd");<br />int num = 0;<br />int created = 0;<br />while(created == 0)<br />{<br />num++;<br />if(exists(str))<br />{<br />char numb[100];<br />strcpy(str,"abcd");<br />strcpy(numb,"");<br />strcpy(numb,itoa(num));<br />strcat(str,numb);<br />}<br />else<br />{<br />FILE *f=fopen(str,"w");<br />printf("%s file createdn",str);<br />created=1;<br />fclose(f);<br />}<br />}<br />return 0 ;<br />}<br /></i>
- AuthorPosts
- The forum ‘C Programming’ is closed to new topics and replies.