C程序逐字读入字符串
基本上,我所做的是在一个随机的文本文件中阅读,删除标点符号并将单词排列到一个链接列表中 用这个一切都很好C程序逐字读入字符串,c,arrays,pointers,C,Arrays,Pointers,基本上,我所做的是在一个随机的文本文件中阅读,删除标点符号并将单词排列到一个链接列表中 用这个一切都很好 printf("%c", (*(buf+x))); 例如,文本文件具有以下内容 “无论我们去哪里,他们都是他们的朋友。” 正确的输出已打印出来 theyre their where ever we may go go 我的问题是,如何将它们转换为字符串并存储在数组中?我假设您希望重用buf,因此您希望将这些单词复制到单独分配的存储中。如果在复制buf中的每个单词之前都以空字节结尾,即'\
printf("%c", (*(buf+x)));
例如,文本文件具有以下内容
“无论我们去哪里,他们都是他们的朋友。”
正确的输出已打印出来
theyre
their
where
ever
we
may
go
go
我的问题是,如何将它们转换为字符串并存储在数组中?我假设您希望重用
buf
,因此您希望将这些单词复制到单独分配的存储中。如果在复制buf
中的每个单词之前都以空字节结尾,即'\0'
,则可以使用strdup
进行复制。以后可以使用free
释放空间。添加以下内容(第二个是免费的):
我假设您希望将文件中的单词存储在一个数组中并打印它们。 您可以尝试以下操作:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
char *buf[40];
for (int n = 0; n < 40; n++)
{
buf[n] = (char*)malloc(20 * sizeof(char));
}
FILE *file;
fopen_s(&file, "file.txt", "r");
int c, i = 0, j = 0;
bool init = 0;
while ((c = getc(file)) != EOF)
{
if (c != ' ')
{
buf[i][j] = c;
j++;
init = 1;
}
else
{
if (init)
{
buf[i][j] = '\0';
i++;
j = 0, init = 0;
}
}
}
buf[i][j] = '\0';
for (int x = 0; x <= i; x++)
{
printf("word[%d] : %s\n", x, buf[x]);
}
for (int x = 0; x < 40; x++)
{
free(buf[x]);
}
fclose(file);
return 0;
}
#包括
#包括
#包括
内部主(空)
{
char*buf[40];
对于(int n=0;n<40;n++)
{
buf[n]=(char*)malloc(20*sizeof(char));
}
文件*文件;
fopen_s(&file,“file.txt”,“r”);
int c,i=0,j=0;
bool init=0;
而((c=getc(文件))!=EOF)
{
如果(c!='')
{
buf[i][j]=c;
j++;
init=1;
}
其他的
{
if(init)
{
buf[i][j]='\0';
i++;
j=0,init=0;
}
}
}
buf[i][j]='\0';
对于(int x=0;x
- 尝试以下代码以更好地理解概念:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
char c;
int i = 0, count = 0,max = 0;
FILE *fp;
fp = fopen("file.txt", "r");
if(fp == 0)
{
printf("File is not opened\n");
return;
}
else
{
// Finding line having maximum characters from the file to allocate that much memory..
while ((c = getc(fp)) != EOF)
{
count++;
if(c == '\n')
{
if(count > max)
max = count;
count = 0;
}
else
{
count = 0;
}
}
}
rewind(fp);
// Allocating the memory up to max line from the file.
char *temp = (char*)malloc(max * sizeof(char));
while ((c = getc(fp)) != EOF)
{
if ((c >= 'a' && c<='z') || (c >= 'A' && c <= 'z'))
{
temp[i++] = c;
}
else if(c == ' ' || c == '\n')
{
temp[i] = '\0';
printf("%s\n",temp);
i = 0;
memset(temp,'\0',max);
}
}
free(temp);
fclose(fp);
return 0;
}
#包括
#包括
#包括
内部主(空)
{
字符c;
int i=0,count=0,max=0;
文件*fp;
fp=fopen(“file.txt”,“r”);
如果(fp==0)
{
printf(“文件未打开\n”);
返回;
}
其他的
{
//正在从文件中查找最大字符数的行以分配如此多的内存。。
而((c=getc(fp))!=EOF)
{
计数++;
如果(c=='\n')
{
如果(计数>最大值)
最大值=计数;
计数=0;
}
其他的
{
计数=0;
}
}
}
倒带(fp);
//将内存分配到文件中的最大行。
char*temp=(char*)malloc(max*sizeof(char));
而((c=getc(fp))!=EOF)
{
如果((c>='a'&&c='a'&&c请显示所有相关代码。如果它不是数组,那么什么是buf
?抱歉,buf是一个缓冲区,根据输入文件为其分配了动态内存,x是一个计数器,每次while循环完成时计数每个字符。我之所以没有显示所有代码,是因为它是一个a你的代码为什么会被剽窃,如果你不想被剽窃的话?说得好,但在这一点之前,它工作得很好,我不希望我的作品被标记为在网上的某个地方,然后陷入麻烦,被指控剽窃。这是一个奇怪的系统,他们在PLPACE中有这么一个词“那"printf语句在由于到达空格而退出while循环之前运行三次。然后我是否添加空终止符,然后将其传递给str?对,一旦所有字符都在buf
中,在末尾添加一个\0
字节,然后您可以调用strdup
。之后,您可以在下一个s中重用buf
tring.Ok,我重新措辞了它,还添加了一个示例。遇到了一个问题,它只打印整个缓冲区。您需要保存使用strdup创建的每个字符串。当您开始每个新词时,您需要从buf
开头开始(或者,当您将其传递到strdup
时,向buf
添加一个偏移量)。刚刚查看了它,它很有意义,现在我使用的是一个链表,我猜您在printf之后添加了一个指针语句,这对我的操作非常简单,谢谢您帮助我理解。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
char *buf[40];
for (int n = 0; n < 40; n++)
{
buf[n] = (char*)malloc(20 * sizeof(char));
}
FILE *file;
fopen_s(&file, "file.txt", "r");
int c, i = 0, j = 0;
bool init = 0;
while ((c = getc(file)) != EOF)
{
if (c != ' ')
{
buf[i][j] = c;
j++;
init = 1;
}
else
{
if (init)
{
buf[i][j] = '\0';
i++;
j = 0, init = 0;
}
}
}
buf[i][j] = '\0';
for (int x = 0; x <= i; x++)
{
printf("word[%d] : %s\n", x, buf[x]);
}
for (int x = 0; x < 40; x++)
{
free(buf[x]);
}
fclose(file);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
char c;
int i = 0, count = 0,max = 0;
FILE *fp;
fp = fopen("file.txt", "r");
if(fp == 0)
{
printf("File is not opened\n");
return;
}
else
{
// Finding line having maximum characters from the file to allocate that much memory..
while ((c = getc(fp)) != EOF)
{
count++;
if(c == '\n')
{
if(count > max)
max = count;
count = 0;
}
else
{
count = 0;
}
}
}
rewind(fp);
// Allocating the memory up to max line from the file.
char *temp = (char*)malloc(max * sizeof(char));
while ((c = getc(fp)) != EOF)
{
if ((c >= 'a' && c<='z') || (c >= 'A' && c <= 'z'))
{
temp[i++] = c;
}
else if(c == ' ' || c == '\n')
{
temp[i] = '\0';
printf("%s\n",temp);
i = 0;
memset(temp,'\0',max);
}
}
free(temp);
fclose(fp);
return 0;
}