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
:-)

Never claim your question is urgent. Many people here will completely ignore any post that has that word in it. No question is ever urgent posted here. We'll answer it when we get to it. You're just lucky this time...

As for your problem, see this
Don't blindly output your '-' after your loop. Count the characters as you read them. When you read the 5th character, output your '-' and reset the count.

walt is right: everything is "urgent" ... youre lucky today is a slow day.

and don't use feof in a while loop to break on the end of a file. use instead fgets() to read one full line at a time, and parse what you need out of that line before moving to the next line, if any.

to write 50 random lines, one option is to make the counter run to 800 (because that's 50 lines * 16 characters per line) and after every 16th character, insert a "$\n"

int numberOfLines = ??;  //how to fill this value: hard code?  function argument?

	for (counter = 1; counter <= (numberOfLines * 16); counter++)
	{
		// ... do your random stuff here

		if (counter%16 == 0)  // finds every multiple of 16
			fprintf(fp1,"$\n");		
	}

.

ya i wont say its urgent any time thanks alot for the idea :-) will try it.

hey its generating 50 numbers in first file(file1).but in file1 am getting ine character with "$" and also in file 2 its reading the same first line again in file2.

hey its generating 50 numbers in first file(file1).but in file 2 its reading the same first line again in file2.

Hey, that's too bad. My psychic powers are on the fritz today so I can't see the changes you made with my mind's eye. You'll just have to use another method of getting the current code to us...

hey thats ok am able to generate about 50 numbers and write them onto a file (file1).But suggest me how can i write to file2 with hyphen after every 4 characters.am only getting first alphanumeric character generated in file1.

ok here is the changed code.

#include <stdio.h>
#include <stdlib.h>
#include<unistd.h>
main ()
{
    char randChar =' ';
    int counter = 0;
    int randNum = 0;
    char ch2;
    char buf[100];
    int n;
    int numberoflines;
    int i,d;
    int j;
    FILE *fp1,*fp2;
    fp1=fopen("file1.txt","w");
    printf("how many random alphanumeric character u want to generate\n");
    scanf("%d",&n);
    numberoflines=n;
    srand (time(NULL));
    for (counter = 0; counter<(numberoflines*16); 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==32))
	       {
		 randNum = 26 * (rand () / (RAND_MAX +  1.0));
		 randNum=randNum+48;
	       }
	        randChar = (char) randNum;
                        fprintf (fp1,"%c", randChar);
	        if(counter%16 == 15) //  finds every multiple of 16
	        fprintf(fp1,"$\n");
                 }
          fclose(fp1);
  fp1=fopen("file1.txt","r");
  fp2=fopen("file2.txt","a+");
  j=0;
  while(!feof(fp1))	
       {
           for(i=0;i<=3;i++)
	{     
	        fscanf(fp1,"%c",&ch2);
	        if(ch2=='$'||ch2=='\n')
                          {
	                break;	
                           }
	        else
	            {
		buf[i]=ch2;
		fprintf(fp2,"%c",buf[i]);
	            }
                         j++;
	}
	if(j>=16)
                {
	    break;
                 }
	 else			
                     {  	
	       fprintf(fp2,"-");
                      }
          }	
fclose(fp1);
fclose(fp2);
}

is it ok now?

you should always use code tags:

and make sure your tabs/indentations come out right. go fix it now. you've got 15 mins left to edit.

once you format it so that it's readable, then i'll take a look.[code=c]

and make sure your tabs/indentations come out right. go fix it now. you've got 15 mins left to edit.

once you format it so that it's readable, then i'll take a look.

ok here is the changed code.

#include <stdio.h>
#include <stdlib.h>
#include<unistd.h>
main ()
{
	char randChar =' ';
	int counter = 0;
	int randNum = 0;
	char ch2;
	char buf[100];
	int n;
	int numberoflines;
	int i,d;
	int j;
	FILE *fp1,*fp2;
	fp1=fopen("file1.txt","w");
	printf("how many random alphanumeric character u want to generate\n");
	scanf("%d",&n);
	numberoflines=n;
	srand (time(NULL));
	for (counter = 0; counter<(numberoflines*16); 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==32))
			{
				randNum = 26 * (rand () / (RAND_MAX + 1.0));
				randNum=randNum+48;
			}
			randChar = (char) randNum;
			fprintf (fp1,"%c", randChar);
			if(counter%16 == 15)
                                                {
			fprintf(fp1,"$\n");
                                                }
		}
	 fclose(fp1);

	 fp1=fopen("file1.txt","r");
	 fp2=fopen("file2.txt","a+");
	 j=0;
	while(!feof(fp1)) 
	 {
		for(i=0;i<=3;i++)
		{ 
			fscanf(fp1,"%c",&ch2);
			if(ch2=='$'||ch2=='\n')
			{
				break; 
			}
			else
			{
				buf[i]=ch2;
				fprintf(fp2,"%c",buf[i]);
			}
			j++;
		}
		if(j>=16)
		{
			break;
		}
		else 
		{ 
			fprintf(fp2,"-");
		}
	} 
	fclose(fp1);
	fclose(fp2);
}

is it ok now?

man ! what is this am indenting it properly once i save its showing without indentation.

you need to use opening AND closing tags

this includes an opening tag (the code=c , like you did)

but also needs a CLOSING TAG

at the end.

you understand the concept of markup tags? its like HTML tags but with square brackets rather than angle brackets.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

#include <stdio.h>
#include <stdlib.h>
#include<unistd.h>
main ()
{
char randChar =' ';
int counter = 0;
int randNum = 0;
char ch2;
char buf[100];
int n;
int numberoflines;
int i,d;
int j;
FILE *fp1,*fp2;
fp1=fopen("file1.txt","w");
printf("how many random alphanumeric character u want to generate\n");
scanf("%d",&n);
numberoflines=n;
srand (time(NULL));
for (counter = 0; counter<(numberoflines*16); 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==32))
{
randNum = 26 * (rand () / (RAND_MAX + 1.0));
randNum=randNum+48;
}
randChar = (char) randNum;
fprintf (fp1,"%c", randChar);
if(counter%16 == 15) // finds every multiple of 16
fprintf(fp1,"$\n");
}
fclose(fp1);
fp1=fopen("file1.txt","r");
fp2=fopen("file2.txt","a+");
j=0;
while(!feof(fp1)) 
{
for(i=0;i<=3;i++)
{ 
fscanf(fp1,"%c",&ch2);
if(ch2=='$'||ch2=='\n')
{
break; 
}
else
{
buf[i]=ch2;
fprintf(fp2,"%c",buf[i]);
}
j++;
}
if(j>=16)
{
break;
}
else 
{ 
fprintf(fp2,"-");
}
} 
fclose(fp1);
fclose(fp2);
}

yeah, now you need to put your indentations back in there.

if you put the code tags in the first time around, your original indentations that you've cut and pasted will persist.

forgetting to code tag it, then editing later, will cause your original indentations to disappear.

you can edit the post, cut out all that code, then repaste your original code with the indentations back in, betwen the code tags.

.

#include <stdlib.h>
#include<unistd.h>
main ()
{
     char randChar =' ';
     int counter = 0;
     int randNum = 0;
     char ch2;
     char buf[100];
     int n;
     int numberoflines;
     int i,d;
     int j;
     FILE *fp1,*fp2;
     fp1=fopen("file1.txt","w");
     printf("how many random alphanumeric character u want to generate\n");
     scanf("%d",&n);
     numberoflines=n;
     srand (time(NULL));
     for (counter = 0; counter<(numberoflines*16); 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==32))
              {
                   randNum = 26 * (rand () / (RAND_MAX + 1.0));
                   randNum=randNum+48;
              }
             randChar = (char) randNum;
             fprintf (fp1,"%c", randChar);
             if(counter%16 == 15) // finds every multiple of 16
             fprintf(fp1,"$\n");
        }
        fclose(fp1);
        fp1=fopen("file1.txt","r");
        fp2=fopen("file2.txt","a+");
        j=0;
        while(!feof(fp1)) 
          {
               for(i=0;i<=3;i++)
                  { 
                       fscanf(fp1,"%c",&ch2);
                             if(ch2=='$'||ch2=='\n')
                                 {
                                     break; 
                                  }
                                  else
                                        {
                                              buf[i]=ch2;
                                              fprintf(fp2,"%c",buf[i]);
                                         }
                              j++;
                   }
            if(j>=16)
              {
                   break;
              }
            else 
               { 
                     fprintf(fp2,"-");
                }
        } 
    fclose(fp1);
    fclose(fp2);
}

am able to generate as many random alpha numeric characters as i want but in file 2 its reading only first number generated in file1

Contents of file1(i generated only 10 numbers now):
7A96EF6447B863A8$
A3CCEDB3FF6C6877$
A28F9H9D6A5GBDD3$
GDEHFF3GHB8C8EH3$
D73ED27475862F9H$
5C8928HDBEC83AF6$
H6G96F3298B523DC$
EA7899GA65CB8HE4$
EB555BF24D589BAE$
496DF6D6A68G2A9D$

but in file2.txt only first number i.e:-
7A96-EF64-47B8-63A8

is being read am not able to design how to read all numbers from file1 and place it in file2 in this format only
7A96-EF64-47B8-63A8

the problem is this:

fscanf(fp1,"%c",&ch2);
            if(ch2=='$'||ch2=='\n')
            {
                break;
            }

as soon as you find either a $ or a \n, you break out of the while() loop, ending your entire search.

fix that and you'll be done.

im not going to critique your code for "best practices"

ya i knw that i have a problem ther i tried fseek and all that but it didnt work. can u suggest me some other methods?

okay, actually the problem is not due to that 'break' statement. the root of your problem is that you want to use fgets() not feof to control your read loop. but then that changes how you handle copying the string read from file1 to file2

then theres a number of ways you can deal with copying it. an intermediary buffer, plus something like strncpy() and strcat(), or just using pointers, which i prefer.

ok, it's late, and you've been a good sport. so here's what i would do. be advised, it has essentially no fault-tolerance. but that fits with the rest of the code, so at least it won't stick out in that regard.

just make sure you take time to really understand whats going on.

while(fgets(line,20,fp1))
    {
        char *buf_p = buf;  // pointer to string 'buf'
        char *line_p = line;  // pointer to string 'line' read from file1
        int index = 0;  // index counts position for '-' inserts

        while(++index <= 19 && *line_p != '$' && *line_p != '\n')
        {
            if (index % 5 == 0) // '-' inserted at pos. 5, 10, 15
                *buf_p++ = '-';

            else
                *buf_p++ = *line_p++;  // otherwise copy each char
        }

        *buf_p++ = '\n';  // when done, append newline and NULL
        *buf_p = '\0';

        fprintf(fp2,buf);  // then print the entire buffer to file2

    }

.

hey thanks a lot its working .i try to optimize my code and post it here.Once again thaks a lot.

in general, the best way to read data from text files is to use "fgets()" which reads one entire line at a time, or the number of characters specified if a new line is not found within that number of characters.

then you can do whatever you need to with each line. fgets will return a NULL pointer when there is nothing left to read (the end of file), so its perfect for use in a while loop .

become familiar with how the function works, you will likely use it a lot

http://www.cplusplus.com/reference/clibrary/cstdio/fgets/

Hey when i executed this program am getting an error
"10 [main] a 4756 _cygtls::handle_exceptions: Error while dumping
state(probably corrupted stack)
Segmentation fault (core dumped)"
Am not getting what to do?
Please help.....

#include <stdio.h>
#include <stdlib.h>
#include<unistd.h>
main ()
{
	char randChar =' ';  
	int counter = 0;
	int randNum = 0;  
	char ch2;  
	char buf[100]; 
	int n;   
	int numberoflines;  
	int i,d; 
	int j;    
	FILE *fp1,*fp2;    
	
	fp1=fopen("file1.txt","w");  
	printf("how many random alphanumeric character u want to generate\n");    
	scanf("%d",&n);    
	numberoflines=n;   
	srand (time(NULL));   
	for (counter = 0; counter<(numberoflines*16); 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==32))	      
			{		 
			randNum = 26 * (rand () / (RAND_MAX +  1.0));	
			randNum=randNum+48;	   
			}	
			randChar = (char) randNum;         
			fprintf (fp1,"%c", randChar);	   
			if(counter%16 == 15) //  finds every multiple of 16	   
			fprintf(fp1,"$\n");       
			}    
			fclose(fp1); 
			fp1=fopen("file1.txt","r"); 
			fp2=fopen("file2.txt","a+"); 
 char *line;
 while(fgets(line,20,fp1))    
	 {
        char *buf_p = buf;  // pointer to string 'buf'
        char *line_p = line;  // pointer to string 'line' read from file1
        int index = 0;  // index counts position for '-' inserts

        while(++index <= 19 && *line_p != '$' && *line_p != '\n')
        {
            if (index % 5 == 0) // '-' inserted at pos. 5, 10, 15
                *buf_p++ = '-';

            else
                *buf_p++ = *line_p++;  // otherwise copy each char
        }

        *buf_p++ = '\n';  // when done, append newline and NULL
        *buf_p = '\0';

        fprintf(fp2,buf);  // then print the entire buffer to file2

    }

}

here's your problem

char *line;
while(fgets(line,20,fp1))

you cant write data to an uninitialized pointer.

This article has been dead for over six months. Start a new discussion instead.