zoukankan      html  css  js  c++  java
  • 第一次用C语言把数据写入文件中

    在文件中创建一个txt文件后,就可以使用程序了

    本程序从正文文件text.txt中读入一篇英文短文,统计该短文中不同单词及出现次数,并按词典编辑顺序将单词及出现次数输出到正文文件word.txt中。

    程序用一棵有序二叉树存储这些单词及其出现的次数,边读入边建立,然后中序遍历该二叉树,将遍历经过的二叉树上的结点的内容输出。

    #include "stdafx.h"
    # include <stdio.h>
    # include <malloc.h>
    # include <ctype.h>
    # include <string.h>
    # define   INF        "text.txt"
    # define   OUTF     "word.txt"
    typedef struct treenode {
           char *word;
           int    count;
           struct treenode *left, *right;
    } BNODE;
    int getword(FILE *fpt, char *word)
    {     char c;
           c=fgetc(fpt);
           if ( c == EOF)
                  return 0;
           while(!(tolower(c) >= 'a' && tolower(c) <= 'z'))
           {     c=fgetc(fpt);
                  if ( c == EOF)
                         return 0;
           }  /* 跳过单词间的所有非字母字符 */
           while(tolower(c) >= 'a' && tolower(c) <= 'z')
           {     *word++ = c;
                  c = fgetc(fpt);
           }
           *word = '';
           return 1;
    }
    void binary_tree(BNODE **t, char *word)
    {     BNODE   *ptr, *p; int compres;
           p = NULL;
        ptr=*t;
           while (ptr)  /* 寻找插入位置 */
        {     compres=strcmp(word,ptr->word); /* 保存当前比较结果 */
                  if (!compres)
         {      ptr->count++ ;
         return; }
                  else
                  {       p=ptr ;
                         ptr = compres>0 ? ptr->right : ptr->left;
                  }
           }
           ptr = (BNODE *)malloc(sizeof(BNODE));
           ptr->left = ptr->right = NULL;
           ptr->word = (char *)malloc(strlen(word)+1);
           strcpy(ptr->word, word);
           ptr->count = 1;
           if (p == NULL)
                *t=ptr ;
           else if (compres > 0)
                  p->right = ptr;
           else
                  p->left = ptr;
    }
    void midorder(FILE *fpt, BNODE *t)
    {     if ( t == NULL )
                  return;
           midorder(fpt, t->left);
           fprintf(fpt, "%s %d ", t->word, t->count);
           midorder(fpt, t->right);
    }
    void main()
    {     FILE       *fpt;  char word[40];
           BNODE   *root=NULL;
           if ((fpt=fopen(INF, "r")) == NULL)
           {     printf("Can't open file %s ", INF);
                  return;
           }
           while(getword(fpt, word) == 1)
                  binary_tree( &root, word );
           fclose(fpt);
           fpt = fopen(OUTF, "w");
           if (fpt == NULL)
           {     printf("Can't open file %s ", OUTF);
                  return;
           }
           midorder(fpt, root);
           fclose(fpt);
    }

  • 相关阅读:
    idea的使用和安装破解 2019.2
    get请求和post请求的区别
    MySQL-事务
    MySQL-mysql的查询练习
    MySQL-mysql的多表查询
    CodeForces
    2018宁夏邀请赛网赛 I. Reversion Count(java练习题)
    HDU
    Codeforces Round #479 (Div. 3)解题报告
    nyoj 1274信道安全 第九届河南省赛(SPFA)
  • 原文地址:https://www.cnblogs.com/wk2008520/p/3236177.html
Copyright © 2011-2022 走看看