When my friend asked me to read TXT file in C language, my first reaction was that it was too easy. Even see the special form of txt I also feel easy. However, 3 when I was completely finished, I realized that one of my basic work is not good, the second is that things are small but the test is very much.
The TXT format is as follows:
Requirements are as follows:
<< >> <<->> <<->> << 468 >> <<->>
Program Requirements:
Enter any number, statistics the number of occurrences in txt, no is 0.
At first really feel very easy, think of their freshman curriculum design are all from txt read data, but I was using C + +, inside the CIN package is very good, and there are IStream functions can be used. But this is the C language, and the numbers are wrapped in parentheses, but also each row has an indeterminate number of numbers, can be 5, can be 6. In a word, learning step by step, and finally solve the problem, very guilty unexpectedly spent 3 hours.
#include <stdio.h> main () {int i=0,j=0,count=0; int a[100],searchnum=0; char ch; FILE *FP; File* in; File* out; in = fopen ("New_demo.txt", "RB"); out = fopen ("Temp.txt", "WB"); Filter the symbol < and symbol > in the original data and save it as a data temp.txt. while ((ch = getc (in))!= EOF) {if (ch!= ' < ' && ch!= ' > ') PUTC (ch, out);} fclose (in); Fclose (out); Input requires a number of numbers, such as 225 printf ("Please input the number search:"); scanf ("%d", &searchnum); if ((Fp=fopen ("Temp.txt", "RT")) ==null) {printf ("error!/n"); Getch (); exit (1); while (!feof (FP)) {fscanf (FP, "%d") &a[i]); i++; for (j=0;j<i;j++) {if (a[j] = = searchnum) count++;//printf ("%d/n", a[j)); Fclose (FP); printf ("The Count is:%d", count); scanf ("%d", &i); }