Hi,
Am anuj joined community today itsel.I have written a code which generates alpha numeric charaters and stores it in(file1).Then read the generated stuff 4 characters in file(by reading from file1)and appending "-" after 4 characters until "$" is encountered.I am getting an extra "-" in the file please tell me how to remove this.and also i want to generate 100 numbers at a time and append them to previously generated.Please help me.
Here is the code :-
#include <stdio.h>
#include <stdlib.h>
//#define MAX_LEN 16
main ()
{
char randChar =' ';
int counter = 0;
int randNum = 0;
int ch;
char ch2;
char buf[100];
int i;
char *c;
int stop;
FILE *fp1,*fp2;
fp1=fopen("file1.txt","w+");
srand (time(NULL));
for (counter = 0; counter <=15; counter++)
{
randNum = 26 * (rand () / (RAND_MAX + 1.0));
randNum=randNum+48;
while((randNum==48)||(randNum==49)||(randNum==79)||(randNum==73)||(randNum==58)||(randNum==59)||(randNum==60)||(randNum==61)||(randNum==62)||(randNum==63)||(randNum==64))
{
randNum = 26 * (rand () / (RAND_MAX + 1.0));
randNum=randNum+48;
}
randChar = (char) randNum;
fprintf (fp1,"%c", randChar);
}
fprintf(fp1,"$");
fclose(fp1);
fp1=fopen("file1.txt","r");
fp2=fopen("file2.txt","w");
printf("hi");
while(!feof(fp1))
{
//printf("hi1\n");
for(i=0;i<4;i++)
{
fscanf(fp1,"%c",&ch2);
// printf("%c\n",ch2);
if(ch2=='$'||ch2=='\n')
exit(0);
else
buf[i]=ch2;
fprintf(fp2,"%c",buf[i]);
}
fprintf(fp2,"-");
}
fclose(fp1);
fclose(fp2);
}
and here are the contents of file1:-
GH3HF722555FA9E8$
and here are the contents of file2:-
GH3H-F722-555F-A9E8-
Thanks
anuj
:-)