从文本文件创建数组并用C进行组织
我正在尝试制作一个程序,可以读取一个文本文件,并将文件中的所有单词放入一个数组,然后可以按字母顺序组织数组,删除所有非字母的符号,并将所有字母设置为小写 到目前为止,我只能读取文本文件,然后吐出列中的所有数据。当我尝试将打印部分移动到它自己的功能中时,它会断开 我也在尝试将一个单词插入数组,但我也没有太成功 以下是迄今为止我得到的代码:从文本文件创建数组并用C进行组织,c,arrays,C,Arrays,我正在尝试制作一个程序,可以读取一个文本文件,并将文件中的所有单词放入一个数组,然后可以按字母顺序组织数组,删除所有非字母的符号,并将所有字母设置为小写 到目前为止,我只能读取文本文件,然后吐出列中的所有数据。当我尝试将打印部分移动到它自己的功能中时,它会断开 我也在尝试将一个单词插入数组,但我也没有太成功 以下是迄今为止我得到的代码: #include <stdio.h> #include <ctype.h> #include <string.h> #inc
#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);
}
#包括
#包括
#包括
#包括
void main(){
文件*fp;
文件*fp2;
int-wordcount=1;
int i,x;
字符文件名[100];
开始:printf(“插入文件名:\n”);
获取(&filename);
fp=fopen(文件名为“rw+”);
char ch;
如果(fp==NULL){
printf(“错误,文件为空。\n”);
转到开始;//重新开始
}
其他的
while(ch!=EOF)
{
如果(ch=''| | ch='\n')
{
字数+=1;
}
ch=fgetc(fp);
}
字符*表[字数];
fp2=fopen(filename,“r”);//出于某种原因,我不得不重新打开该文件,以便在接下来的两周内继续工作。
对于(i=0;i
那么我应该如何从这里开始呢?我非常迷茫,如果这是python,我现在已经完成了,但是C对我来说非常混乱。非常感谢您的帮助。您可以使用fseek()重新读取文件,而不是使用第二个fopen()。如果有足够的空间将所有单词存储为128字节字符串,则可能有足够的空间将整个文件读入内存。您可以使用fseek()、ftell()来获取文件大小,使用fseek()或rewind()来“倒带”文件,分配足够的内存,然后将整个文件读入内存。然后可以扫描文件图像一次以获得字数,然后再次扫描以设置指针数组以指向每个字的开头。我建议您将结尾空格或换行符替换为零,以生成字符串
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的已清理部分版本,以及一些清理[已在评论中建议]:
#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;
}
#包括
#包括
#包括
#包括
/*我正在尝试添加的函数*/
无效的
打印(字符**表格,整数字数)
{
int i;
对于(i=0;i
您对“插入”的确切定义是什么?我会把它编码的。一些东西。不要使用goto。您必须重新打开文件的原因是,您已经在文件末尾了,因为您阅读了整个文件以获得字数。到底什么东西断了?这是一个课堂项目还是你自己在做的事情?C++从Python中得到了一点友好(但不是很多)。这不能解释问题,但是<代码> FGCTC返回代码> int <代码>,因此应该是<代码> int CH;<代码>即使是
ch
在第一次使用之前也没有初始化。@ChaseHenslee类项目,否则我会使用python,因为从我在过去两天的努力中了解到,C不是文件处理的最佳工具。在我之前的迂腐观点中,while(ch!=EOF)
正在测试未初始化的变量。这应该是while((ch=fgetc(fp))!=EOF)
并删除该循环体中的fgetc
行。我现在要做的就是将其插入数组,从那里我会尝试找到一种方法,以按字母顺序正确排列数组。顺便说一下,非常感谢你的代码@CraigEstey评论还建议您正确使用int ch
@CraigEstey您的代码有内存韭葱。堆的总使用量:13个allocs,1个frees,分配2088字节==4100==1536字节,在12个块中的丢失记录1中肯定丢失了,就像WeatherVane sad一样,您应该使用int ch not char ch。@Michi我的时间很短,但我没有尝试完全重写。只是在清理最糟糕的东西。我错过了int-ch,但是,因为问题出现在文本文件中,所以即使使用char,代码也可以正常工作。没有任何内存“韭葱”[sp.leaks]——没有输入文件,您的数字就毫无意义。插入函数尚未编写。并且,自由循环可以添加到末尾——但是如果您只是终止