Home > Back-end >  I want to ask me why can't write the generated random number in the text? Online please! Thanks
I want to ask me why can't write the generated random number in the text? Online please! Thanks

Time:10-08

# include & lt; Stdio. H>
# include & lt; Cstdlib>
# include & lt; Time. H>
Int main ()
{
The FILE * fp.
Fp=fopen (" random number. TXT ", "w +");
If (fp==NULL)//determine if the File pointer is empty {printf (" the File always open! "); exit(0); } int n;
Int d [100];
int a;
srand(time(NULL)); For (n=0; N & lt; 100; N++)
[n] {d=1 + rand () % 100;
Printf (" random number between 1 and 100: % d \ n ", 1 + rand () % 100);
}
The fclose (fp);
Int I=1;
While (I=1) {printf (" please enter a number to query (exit) input - 1: \ n ");
scanf("%d",& A);
{the for (n=0; n<100; N++) {if (d==a) [n] {printf (" have the data of the random number \ n "); break; }} printf (" random number without this data \ n ");
}
} return 0;

}

CodePudding user response:

From the beginning to the end did not see the write file operations
Fprintf is written to the file

CodePudding user response:

reference 1st floor gouyanfen response:
from the beginning to the end did not see the write file operations
Fprintf is written to the file
who can help me to change it... The file I still don't understand this way, I want to save to a file, the random number to look for in the file

CodePudding user response:

 # include & lt; Stdio. H> 
//# include & lt; Cstdlib>
# include & lt; Stdlib. H>
# include & lt; Time. H>

Int main ()
{
Int n, a, d [100].
The FILE * fp.
Fp=fopen (" random number. TXT ", "w +");
If (fp==NULL)//determine if the file pointer to an empty
{
Printf (" the File always open! ");
exit(0);
}

srand(time(NULL));
For (n=0; N & lt; 100; N++)
{
D [n]=1 + rand () % 100;
//printf (" random number between 1 and 100: % d \ n ", 1 + rand () % 100);
Fprintf (fp, "random number between 1 and 100: % d \ n", 1 + rand () % 100);
}
The fclose (fp);
//int I=1;
//while (I=1) {
While (1) {
Printf (" please enter a number to query (exit) input - 1: \ n ");
scanf("%d",& A);
For (n=0; n<100; N++) {
If (d==a) [n] {
Printf (" have the data of the random number \ n ");
break;
}
}
If (n & gt; {
=100)Printf (" random number without this data \ n ");
break;
}
}
return 0;

}


For your reference ~

CodePudding user response:

reference 3 building self-confidence boy reply:
 # include & lt; Stdio. H> 
//# include & lt; Cstdlib>
# include & lt; Stdlib. H>
# include & lt; Time. H>

Int main ()
{
Int n, a, d [100].
The FILE * fp.
Fp=fopen (" random number. TXT ", "w +");
If (fp==NULL)//determine if the file pointer to an empty
{
Printf (" the File always open! ");
exit(0);
}

srand(time(NULL));
For (n=0; N & lt; 100; N++)
{
D [n]=1 + rand () % 100;
//printf (" random number between 1 and 100: % d \ n ", 1 + rand () % 100);
Fprintf (fp, "random number between 1 and 100: % d \ n", 1 + rand () % 100);
}
The fclose (fp);
//int I=1;
//while (I=1) {
While (1) {
Printf (" please enter a number to query (exit) input - 1: \ n ");
scanf("%d",& A);
For (n=0; n<100; N++) {
If (d==a) [n] {
Printf (" have the data of the random number \ n ");
break;
}
}
If (n & gt; {
=100)Printf (" random number without this data \ n ");
break;
}
}
return 0;

}


For reference ~
thank??
  • Related