我试图创建一个可以读取文本文件并将文件中的所有单词放入数组的程序,然后按字母顺序组织数组,删除所有不是字母的符号,然后制作所有字母小写。
到目前为止,我只能读取文本文件,然后吐出一列中的所有数据。当我试图将打印部分移动到它自己的功能中时,它会中断。
我也试图将一个单词插入一个数组,但我也没有太成功。
这是我到目前为止所获得的代码:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
void main(){
FILE *fp;
FILE *fp2;
int wordcount = 1;
int i,x;
char filename[100];
Start: printf("Insert Filename: \n");
gets(&filename);
fp = fopen(filename, "rw+");
char ch;
if (fp == NULL){
printf("Error, file is NULL.\n");
goto Start; //Reprompts you
}
else
while (ch != EOF)
{
if (ch==' '||ch=='\n')
{
wordcount += 1;
}
ch = fgetc(fp);
}
char *Table[wordcount];
fp2 = fopen(filename, "r"); //For some reason I had to reopen the file for the next two to work.
for (i = 0; i < wordcount; ++i) {
Table[i] = malloc(128);
fscanf(fp2, "%127s", Table[i]);
}
for (i = 0; i < wordcount; ++i){ //This is what prints the array
printf("%d: %s\n", i, Table[i]);}
/*Functions I'm trying to add*/
void print(char *Table[], int wordcount){
}
int insert(char *word, char *Table[], int wordcount){
}
fclose(fp2);
fclose(fp);
}
那我该怎么办呢?我非常迷失,如果这是蟒蛇我现在已经完成了,但C对我来说非常困惑。非常感谢任何帮助。
答案 0 :(得分:0)
您可以使用fseek()重新读取文件,而不是使用第二个fopen()。如果有足够的空间将所有单词存储为128字节字符串,则可能有足够的空间将整个文件读入内存。您可以使用fseek(),ftell()来获取文件大小,fseek()或倒带()到&#34;倒带&#34;文件,分配那么多内存,然后将整个文件读入内存。然后,您可以扫描文件图像一次以获得字数,然后再次扫描以设置指针数组以指向每个单词的开头。我建议你用零替换结束空格或换行符来制作单词字符串。
char *pdata;
FILE *fp;
long filesize;
/* read entire file into memory */
fp=fopen(..., "r");
fseek(fp, 0, SEEK_END);
filesize = ftell(fp);
rewind(fp);
pdata = malloc(filesize);
fread(pdata, 1, filesize, fp);
// scan to get word count and set word terminators to zero
// scan for words setting pointers in Table[];
// ...
free(pdata); // free memory when done
答案 1 :(得分:0)
这是一个已完成目标1的清理部分版本,以及一些清理[已在评论中提出]:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
/* Functions I'm trying to add */
void
print(char **Table,int wordcount)
{
int i;
for (i = 0; i < wordcount; ++i) {
printf("%d: %s\n", i, Table[i]);
}
}
int
main(int argc,char **argv)
{
FILE *fp;
//FILE *fp2;
int wordcount = 0;
int i;
char *cp;
//int x;
char ch;
char filename[100];
while (1) {
printf("Insert Filename: ");
fflush(stdout);
cp = fgets(filename,sizeof(filename),stdin);
if (cp == NULL)
continue;
cp = strchr(filename,'\n');
if (cp != NULL)
*cp = 0;
fp = fopen(filename, "r");
if (fp != NULL)
break;
printf("Error, file is NULL.\n");
}
while (1) {
ch = fgetc(fp);
if (ch == EOF)
break;
switch (ch) {
case ' ':
case '\n':
wordcount += 1;
break;
}
}
// For some reason I had to reopen the file for the next two to work.
#if 0
fp2 = fopen(filename, "r");
#else
rewind(fp);
#endif
char *Table[wordcount];
for (i = 0; i < wordcount; ++i) {
Table[i] = malloc(128);
fscanf(fp, "%127s", Table[i]);
}
fclose(fp);
// This is what prints the array
#if 0
for (i = 0; i < wordcount; ++i) {
printf("%d: %s\n", i, Table[i]);
}
#else
print(Table,wordcount);
#endif
#if 0
int insert(char *word, char *Table[], int wordcount) {
}
#endif
return 0;
}
你&#34;插入&#34;的确切定义是什么?我会编码。