如何从文本文件中读取句子并将这些句子存储到数组中。 [英] How to read sentences from a text file and store these sentences into array.

查看:81
本文介绍了如何从文本文件中读取句子并将这些句子存储到数组中。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过,但第一句只读或该句不会存入数组。所以请帮助我。



我尝试了什么:



I tried but the first sentence only read or that sentence would not stored into array. So please kindly help me.

What I have tried:

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char c[100];
int i;
FILE *fp;
clrscr();
if((fp=fopen("hai.txt","r"))==NULL)
{
printf("Error! opening file");
getch();
}
else
{
fscanf(fp," %[^?]",c);
printf("%s\n",c);
}
getch();
fclose(fp);
}

推荐答案

抛弃这样一个古老的编译器并使用 C ++ 如果可以的话。

如果你不能使用 C ++ ,那就比如

Throw away such an ancient compiler and use C++ if you can.
If you cannot use C++, than something like
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define ITEMS 200
#define CHARS 100


int main()
{
  char *  ar[ITEMS];
  char tmp[CHARS];
  int i;
  FILE *fp;
  if((fp=fopen("hai.txt","r"))==NULL)
  {
    printf("Error! opening file\n");
    return -1;
  }

  i = 0;
  while ( i < ITEMS &&  fscanf(fp, " %[^?]", tmp) == 1)
  {
    fgetc(fp);
    ar[i] = strdup(tmp);
    ++i;
  }

  fclose(fp);

  int k = 0;
  while ( k < i)
  {
    printf("ar[%d] = %s\n", k, ar[k]);
    ++k;
  }
  while ( k-- )
    free(ar[k]);

  getchar();

  return 0;
}



可能只是一个起点(你的代码必须健壮)。


could be just a starting point (your code has to be robust).


要重复一些事情,你必须使用循环结构:

C循环 [ ^ ]

for循环C [ ^ ]

For,while and Do While循环在C - Cprogramming.com [ ^ ]



学会正确缩进代码,显示其结构,有助于阅读和理解。它还有助于发现结构错误。

To repeat something, you must use a looping structure:
C Loops[^]
for loop in C[^]
For, While and Do While Loops in C - Cprogramming.com[^]

Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
  char c[100];
  int i;
  FILE *fp;
  clrscr();
  if((fp=fopen("hai.txt","r"))==NULL)
  {
    printf("Error! opening file");
    getch();
  }
  else
  {
    fscanf(fp," %[^?]",c);
    printf("%s\n",c);
  }
  getch();
  fclose(fp);
}



专业程序员的编辑器具有此功能,其他功能包括括号匹配和语法高亮。

Notepad++主页 [ ^ ]

ultraedit [ ^ ]


嗯...要阅读多个句子,你需要一个循环某种形式。



我这样做的方法是将整个文件读入内存(使用文件长度告诉你要分配给读缓冲区的内存量)然后扫描寻找句子。可能你必须做两次这样的过程:第一次找出有多少句子 - 所以你可以分配一个合适的char *指针数组 - 第二次把句子放到数组中。你还需要为每个句子分配足够的空间(使用malloc)并复制句子,添加一个空终止符。



所以先写一个函数来读取文件并将char *返回给第一个字符。

然后编写一个函数来检查句末并返回true或false。请记住,句子可以以'。','!'或'?'结尾(或者是文本的结尾)。

然后编写一个计算句子的函数。

最后编写一个函数,将其分解并返回char *指针数组。
Well... to read more than one sentence, you would need a loop of some form.

The way I would do this is to read the whole file into memory (use the file length to tell you how much memory to allocate to the read buffer) and then scan through that looking for "sentences". Probably, you will have to do this process twice: first time to find out how many sentences there are - so you can allocate a suitable array of char* pointers - and the second time to put the sentences into the array. You will need to also allocate enough space for each sentence (using malloc) and copy the sentence across, adding a null terminator.

So start by writing a function to read the file and return a char* to the first character.
Then write a function which checks for the "end of sentence" and return true or false. Remember that a sentence can end with '.', '!', or '?' (or be the end of the text).
Then write a function that counts the sentences.
Finally write a function which breaks it up and returns the array of char* pointers.


这篇关于如何从文本文件中读取句子并将这些句子存储到数组中。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆