typedef struct neirong
{
Char day [4];//date
Int top [3].//1. Homework 2. Cheat 3. Note
Char wen [1000].//neirong
}; Struct neirong nr, rq;
//including the structure of the
Void the find ()
{
The FILE * op, * mp;
Fwen char fday [30], [30], ch.
Int I=0, s=1, j=0;
Op=fopen (" D: \ \ day. TXT ", "r");
Mp=fopen (" D: \ \ netrng TXT ", "r +");
if (! (op=fopen (" D: \ \ day. TXT ", "r")))
{printf (" wrong! \n"); }
Printf (" to find the date: ");
The scanf (" % s ", fday);
While (1)
{the while (STRCMP (fday, rq. Day!=0)
{I=I + 1; S=I;
The fscanf (op, "% s \ n", & amp; Rq. Day); }
The fscanf (op, "% s \ n", & amp; Nr);
Printf (" found! \n"); break;
For (j=0; J & lt; S - 1; J++)
{
The rewind (mp); Fscanf (mp, "[^ \ n] % % * * c"); }
The fgets (nr. Wen, 30, mp);
Printf (" % s \ n ", nr. Wen); break;
}; The fclose (mp);
Printf (" 11 ");
}
Void main ()
{
The FILE * fp, * lp.
Int j=0, n=1;
if (! (fp=fopen (" D: \ \ day. TXT ", "a +")))
{
Printf (" error! \n");
}
Lp=fopen (" D: \ \ netrng TXT ", "a +");
While (1)
{
J=j + 1;
Today's date: printf (" ");
The scanf (" % s ", rq. Day);
Fprintf (fp, "% s \ n", rq, day);
/* for (n=1; N & lt; 30; N++)
{if (STRCMP (nr [I] day, nr [I + n]. Day==0)
{if (! The feof (fp))
{printf (" already have this date!" ); exit(0);
}
}
} */
Printf (" start recording: \ n ");
The scanf (" % s ", & amp; Nr. Wen);//input the content of the
The fprintf (lp, "% s \ n", nr. Wen);//input into TXT
If (getchar ()=='\ n') break;
}//when the sign bit, the last of XXX break
The fclose (fp);
The fclose (lp);//must close the file or lead to cache disappear
The find ();
}
CodePudding user response:
Please eldest brother of the two days going to cryCodePudding user response:
You want to open a few timesA file, only leave a fopen
CodePudding user response:
Code is too messy, format is not clear, I don't copy to adjust the format I couldn't see the compiler, the error is very much also, you want the nr and rq should be one to write one to read it, you should write the date on which the rq, nr content, in so doing, there is no necessary to make structure, the output is similar; The fscanf (op, "% s \ n", & amp; Nr); This statement to read string to a structure is this? Find () function in the while (1) loop inside put two break is what circumstance? Executed only once, do not have to use the while, while to break with generally want to combine the if statement, meet certain conditions to jump out, you put directly under the while loop of two break, saw the first jump out, behind a piece of code that is never executed, is to remove the first just to perform again break, is equal to the orderCodePudding user response:
Facing the day in the main file only write the date, other are written into the content of theFprintf (fp, "% s \ n", rq, day);
The fprintf (lp, "% s \ n", nr. Wen);
Find inside, date from the date of the file and read, read again, this is obviously not matching
The fscanf (op, "% s \ n", & amp; Rq. Day); }
The fscanf (op, "% s \ n", & amp; Nr);
CodePudding user response:
# include# include
Struct neirong
{
Char day [4];//date
Int top [3].//1. Homework 2. Cheat 3. Note
Char wen [1000].//neirong
}; Struct neirong nr, rq;
//including the structure of the
Void the find ()
{
The FILE * op, * mp;
Char fday [30].
Int I=0, s=1, j=0;
if (! (op=fopen (" D: \ \ day. TXT ", "r")))
{
Printf (" error! \n");
return;
}
if (! (mp=fopen (" D: \ \ netrng TXT ", "r +")))
{
Printf (" error! \n");
return;
}
Printf (" to find the date: ");
The scanf (" % s ", fday);
While (STRCMP (fday, nr. Day!=0)
{
I=I + 1; S=I;
Fscanf (op, "% s \ n", nr. Day);
The fscanf (mp, "% s \ n", nr. Wen);
}
//fscanf (op, "% s \ n", nr. Day);
Printf (" found! \n");
//for (j=0; J & lt; S - 1; J++)
//{
//rewind (mp);
//fscanf (mp, "[^ \ n] % % * * c");
//}
//the fgets (nr. Wen, 30, mp);
Printf (" % s \ n ", nr. Wen);
The fclose (mp);
Printf (" 11 ");
}
Void main ()
{
The FILE * fp, * lp.
Int j=0, n=1;
if (! (fp=fopen (" D: \ \ day. TXT ", "a +")))
{
Printf (" error! \n");
return;
}
if (! (lp=fopen (" D: \ \ netrng TXT ", "a +")))
{
Printf (" error! \n");
return;
}
While (1)
{
J=j + 1;
Today's date: printf (" ");
The scanf (" % s ", rq. Day);
Fprintf (fp, "% s \ n", rq, day);
/* for (n=1; N & lt; 30; N++)
{if (STRCMP (nr [I] day, nr [I + n]. Day==0)
{if (! The feof (fp))
{printf (" already have this date!" ); exit(0);
}
}
} */
Printf (" start recording: \ n ");
The scanf (" % s ", rq. Wen);//input the content of the
Fprintf (lp, "% s \ n", rq. Wen);//input into TXT
If (getchar ()=='\ n') break;
}//when the sign bit, the last of XXX break
The fclose (fp);
The fclose (lp);//must close the file or lead to cache disappear
The find ();
}