C language reads Txt__c language with special format

Source: Internet
Author: User

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); }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.