用C语言从文本文件中读取数据
我有一个文本文件,其中包含用空格分隔的单词。我想从文件中取出每个单词并存储起来。因此,我打开了文件,但不确定如何将单词分配给字符用C语言从文本文件中读取数据,c,file,C,File,我有一个文本文件,其中包含用空格分隔的单词。我想从文件中取出每个单词并存储起来。因此,我打开了文件,但不确定如何将单词分配给字符 文件*fp fp=fopen(“file.txt”,“r”) //那么我要 char one=文件中的第一个单词 char two=文件中的第二个单词不能在char变量中保存单词。它必须是可以放大的字符串或字符指针 试试这个 charp[10]//假设一个单词最多可以包含10个字符 文件*fp fp=fopen(“file.txt”,“r”) fscanf(fp,“%
文件*fp
fp=fopen(“file.txt”,“r”)
//那么我要
char one=文件中的第一个单词
char two=文件中的第二个单词
不能在char变量中保存单词。它必须是可以放大的字符串或字符指针
试试这个
charp[10]//假设一个单词最多可以包含10个字符
文件*fp
fp=fopen(“file.txt”,“r”)
fscanf(fp,“%s”,p) 不能将单词分配给字符。您可以将一个字符指定给一个字符-因此命名。您可以将一个字分配给一个字符数组,例如s[128] 例如:
char word[128];
fscanf(fp, "%s", word);
注意,在生产代码中,您不能仅使用静态大小的缓冲区,这将导致缓冲区溢出可利用的代码。如果您希望更灵活一些,例如:通过选择标识单词的字符,您可以查看以下内容:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// adjust size for your needs
#define MAX_WORD_LEN 1000
static char *parseable_characters_str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy0123456789-";
static char parseable_characters_tbl[256] = {0}; // lookup index table, stores yes/no -> allowed/not allowed
/*
* builds the lookup table
*/
void build_lookup_index(char *table, const char *str)
{
int i;
// init table to zero
memset(table,0,256);
// set value to 1 at ASCII-code offset of the array if the character is allowed to be
// part of the word
for (i=0; str[i]; i++)
table[(unsigned char)str[i]] = 1;
}
/*
* returns unparsed bytes (kind of offset for next reading operation)
*/
int parse_buffer(char *buf, int size, const char *lookup_table)
{
int i,l,s;
char word[MAX_WORD_LEN+1];
i = 0;
l = 0;
s = 0;
while (i<size) {
// character not in lookup table -> delimiter
if (!lookup_table[buf[i]] || !buf[i]) {
if (l >= MAX_WORD_LEN) {
fprintf(stderr,"word exceeds bounds\n");
}
else if (l > 0) { // if word has at least 1 character...
// append string-terminator
word[l] = '\0';
printf("word found (%d): '%s'\n",l,word);
}
// reset word length
l = 0;
// save last word offset
s = i+1;
}
else {
// prevent buffer overflows
if (l < MAX_WORD_LEN)
word[l] = buf[i];
l++;
}
if (!buf[i])
break;
i++;
}
if (s > 0 && size-s > 0) {
// move rest of the buffer to the start for next iteration step
memmove(buf,buf+s,size-s);
return size-s;
}
return 0;
}
int main(int argc, char *argv[])
{
FILE *fh;
char buf[1000]; // read buffer
// "rb" because of Windows - we want all characters to be read
fh = fopen("out.txt","rb");
// initialize word index
build_lookup_index(parseable_characters_tbl,parseable_characters_str);
if (fh) {
int r,off = 0;
while (!feof(fh)) {
r = fread(buf+off,1,sizeof(buf)-off,fh);
off = parse_buffer(buf,r,parseable_characters_tbl);
}
fclose(fh);
}
return 0;
}
#包括
#包括
#包括
//根据需要调整尺寸
#定义最大单词长度1000
静态字符*parseable_characters_str=“abcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvxy0123456789-”;
静态字符可解析字符\u tbl[256]={0};//查找索引表,存储是/否->允许/不允许
/*
*构建查找表
*/
void build\u lookup\u索引(char*table,const char*str)
{
int i;
//将表初始化为零
memset(表0256);
//如果允许输入字符,则在数组的ASCII码偏移处将值设置为1
//词义
对于(i=0;str[i];i++)
表[(无符号字符)str[i]]=1;
}
/*
*返回未解析的字节(下一次读取操作的偏移量)
*/
int parse_缓冲区(char*buf、int size、const char*lookup_表)
{
int i,l,s;
字符字[MAX_word_LEN+1];
i=0;
l=0;
s=0;
while(i)分隔符
if(!lookup_table[buf[i]]| |!buf[i]){
如果(l>=最大字长度){
fprintf(stderr,“字超出界限\n”);
}
如果(l>0){//如果单词至少有1个字符。。。
//附加字符串终止符
字[l]='\0';
printf(“找到的字(%d):'%s'\n',l,字);
}
//重置字长
l=0;
//保存最后一个字偏移量
s=i+1;
}
否则{
//防止缓冲区溢出
if(l0&&size-s>0){
//将缓冲区的其余部分移动到下一迭代步骤的开始处
memmove(buf,buf+s,size-s);
返回大小-s;
}
返回0;
}
int main(int argc,char*argv[])
{
文件*fh;
char buf[1000];//读取缓冲区
//“rb”是因为Windows-我们希望读取所有字符
fh=fopen(“out.txt”、“rb”);
//初始化单词索引
建立查找索引(可解析字符、可解析字符、字符串);
如果(fh){
int r,off=0;
而(!feof(fh)){
r=fread(buf+off,1,sizeof(buf)-off,fh);
off=解析缓冲区(buf、r、可解析字符);
}
fclose(fh);
}
返回0;
}
您可以使用fscanf(fp,“%127s”,word);
在这种情况下避免缓冲区溢出。