从C语言中的输入文件扫描到某个字符(#)?
简单问题: 如果在输入文件中有一行如下所示:从C语言中的输入文件扫描到某个字符(#)?,c,arrays,loops,input,terminate,C,Arrays,Loops,Input,Terminate,简单问题: 如果在输入文件中有一行如下所示: Hello#Great#Day#Today 如何将每个单词作为自己的数组单独扫描,换句话说,告诉C在到达#字符时停止扫描,然后在循环的下一次迭代中将下一个单词作为单独的数组扫描?这是假设您正在通过stdin读取。当然也要看看@Whoz kick start方法(与此非常类似) 您要做的是创建一个动态数组,并用通过stdin读取的每个字节填充它。然后,您需要创建一个字符指针数组,该数组将指向每个“单词”中的第一个字符,在该数组中,您将单词定义为“#
Hello#Great#Day#Today
如何将每个单词作为自己的数组单独扫描,换句话说,告诉C在到达#字符时停止扫描,然后在循环的下一次迭代中将下一个单词作为单独的数组扫描?这是假设您正在通过
stdin
读取。当然也要看看@Whoz kick start方法(与此非常类似)
您要做的是创建一个动态数组,并用通过
stdin
读取的每个字节填充它。然后,您需要创建一个字符指针数组,该数组将指向每个“单词”中的第一个字符,在该数组中,您将单词定义为“#”字符(分隔符)前面的每个字符。然后,您将遍历该字符数组,并使用每个单词中第一个字符的内存地址填充字符指针数组。这是假设您正在通过stdin
进行读取。当然也要看看@Whoz kick start方法(与此非常类似)
您要做的是创建一个动态数组,并用通过stdin
读取的每个字节填充它。然后,您需要创建一个字符指针数组,该数组将指向每个“单词”中的第一个字符,在该数组中,您将单词定义为“#”字符(分隔符)前面的每个字符。然后,您将遍历该字符数组,并用每个单词中第一个字符的内存地址填充字符指针数组。使用strtok()按指定字符标记输入
使用strtok()按指定字符标记输入
在两个阶段中,我使用了如下内容:
#include <ansi_c.h>
//tokenizing a string
int GetCount(char *in, char *delim, int *m);
int GetStrings(char *in, char *delim, int count, char **out);
void main(void)
{
int count, maxlen, i;
char inpString[]={"Hello#Greatest#Day#Today"};
char *resultBuf[10];
//get a count of strings to store
count = GetCount(inpString, "#", &maxlen);
for(i=0;i<10;i++)
{
resultBuf[i] = calloc(maxlen+1, sizeof(char));
}
//Store strings in arrays
GetStrings(inpString, "#", count, resultBuf);
for(i=0;i<count;i++)
{
printf("%s\n", resultBuf[i]);
free(resultBuf[i];
}
}
//Gets count of tokens (delimited strings)
int GetCount(char *in, char *delim, int *m)
{
char *buf=0;
char temp1[10]={0};
char *inStr;
int count = 0;
int max = 0, keepMax = 0;
if(in)
{
inStr = calloc(strlen(in)+1, sizeof(char));
strcpy(inStr, in);
if(strlen(inStr) > 1)
{
count = 0;
buf = strtok(inStr, delim);
while(buf)
{
strcpy(temp1, buf);
max = strlen(temp1);
(max > keepMax)?(keepMax = max):(keepMax == keepMax);
count++;
buf = strtok(NULL, delim);
}
*m = keepMax;
}
free(inStr);
}
return count;
}
//Gets array of strings
int GetStrings(char *in, char *delim, int count, char **out)
{
char *buf=0;
char *inStr;
int i = 0;
if(in)
{
inStr = calloc(strlen(in)+1, sizeof(char));
strcpy(inStr, in);
if(strlen(inStr) > 1)
{
buf = strtok(inStr, delim);
while(buf)
{
strcpy(out[i], buf);
buf = strtok(NULL, delim);
i++;
}
}
free(inStr);
}
return 0;
}
#包括
//标记字符串
int GetCount(char*in,char*delim,int*m);
int GetStrings(char*in,char*delim,int count,char**out);
真空总管(真空)
{
整数计数,maxlen,i;
char inpString[]={“你好#最伟大的#今天”};
char*resultBuf[10];
//获取要存储的字符串数
count=GetCount(inpString,#,&maxlen);
对于(i=0;i keepMax)?(keepMax=max):(keepMax==keepMax);
计数++;
buf=strtok(NULL,delim);
}
*m=keepMax;
}
免费(inStr);
}
返回计数;
}
//获取字符串数组
int GetStrings(char*in,char*delim,int count,char**out)
{
char*buf=0;
字符*仪器;
int i=0;
如果(在)
{
inStr=calloc(strlen(in)+1,sizeof(char));
strcpy(inStr,in);
如果(斯特伦(仪表)>1)
{
buf=strtok(仪表、仪表);
while(buf)
{
strcpy(out[i],buf);
buf=strtok(NULL,delim);
i++;
}
}
免费(inStr);
}
返回0;
}
在两个阶段中,我使用了如下内容:
#include <ansi_c.h>
//tokenizing a string
int GetCount(char *in, char *delim, int *m);
int GetStrings(char *in, char *delim, int count, char **out);
void main(void)
{
int count, maxlen, i;
char inpString[]={"Hello#Greatest#Day#Today"};
char *resultBuf[10];
//get a count of strings to store
count = GetCount(inpString, "#", &maxlen);
for(i=0;i<10;i++)
{
resultBuf[i] = calloc(maxlen+1, sizeof(char));
}
//Store strings in arrays
GetStrings(inpString, "#", count, resultBuf);
for(i=0;i<count;i++)
{
printf("%s\n", resultBuf[i]);
free(resultBuf[i];
}
}
//Gets count of tokens (delimited strings)
int GetCount(char *in, char *delim, int *m)
{
char *buf=0;
char temp1[10]={0};
char *inStr;
int count = 0;
int max = 0, keepMax = 0;
if(in)
{
inStr = calloc(strlen(in)+1, sizeof(char));
strcpy(inStr, in);
if(strlen(inStr) > 1)
{
count = 0;
buf = strtok(inStr, delim);
while(buf)
{
strcpy(temp1, buf);
max = strlen(temp1);
(max > keepMax)?(keepMax = max):(keepMax == keepMax);
count++;
buf = strtok(NULL, delim);
}
*m = keepMax;
}
free(inStr);
}
return count;
}
//Gets array of strings
int GetStrings(char *in, char *delim, int count, char **out)
{
char *buf=0;
char *inStr;
int i = 0;
if(in)
{
inStr = calloc(strlen(in)+1, sizeof(char));
strcpy(inStr, in);
if(strlen(inStr) > 1)
{
buf = strtok(inStr, delim);
while(buf)
{
strcpy(out[i], buf);
buf = strtok(NULL, delim);
i++;
}
}
free(inStr);
}
return 0;
}
#包括
//标记字符串
int GetCount(char*in,char*delim,int*m);
int GetStrings(char*in,char*delim,int count,char**out);
真空总管(真空)
{
整数计数,maxlen,i;
char inpString[]={“你好#最伟大的#今天”};
char*resultBuf[10];
//获取要存储的字符串数
count=GetCount(inpString,#,&maxlen);
对于(i=0;i keepMax)?(keepMax=max):(keepMax==keepMax);
计数++;
buf=strtok(NULL,delim);
}
*m=keepMax;
}
免费(inStr);
}
返回计数;
}
//获取字符串数组
int GetStrings(char*in,char*delim,int count,char**out)
{
char*buf=0;
字符*仪器;
int i=0;
如果(在)
{
inStr=calloc(strlen(in)+1,sizeof(char));
strcpy(inStr,in);
如果(斯特伦(仪表)>1)
{
buf=strtok(仪表、仪表);
while(buf)
{
strcpy(out[i],buf);
buf=strtok(NULL,delim);
i++;
}
}
免费(inStr);
}
返回0;
}
我先写一个算法(检查),然后写一些代码。如果这不起作用,把它带到这里,我们会看看我们能做些什么来帮助。对不起,我只是觉得让一个比我更有经验的人给我展示一种实现方法会很简单,但我想解决方案比我想象的要复杂。好吧,这是一个kickstart。1.查找文件的大小(请参见fseek()
和ftell()
)。2.为null termiator(请参见malloc()
)为整个文件+1分配一个足够大的缓冲区。3.将整个文件读入缓冲区(请参见freed()
)。4.在缓冲区的末尾设置一个0
终止符。最后,使用“#”
作为分隔符,使用strtok()
循环缓冲区。完成后,不要忘记只释放上面(2)中分配的原始内存块。查找刚才提到的所有API并制定您的计划。祝你好运。应该是fread()
。我总是这么做。很抱歉我先写一个算法(检查),然后写一些代码。如果这不起作用,把它带到这里,我们会看看我们能做些什么来帮助。对不起,我只是觉得让一个比我更有经验的人给我展示一种实现方法会很简单,但我想解决方案比我想象的要复杂。好吧,这是一个kickstart。1.查找文件的大小(请参见fseek()
和ftell()
)。2.为null termiator(请参见malloc()
)为整个文件+1分配一个足够大的缓冲区。3.将整个文件读入缓冲区(请参见freed()
)。4.在缓冲区的末尾设置一个0
终止符。最后,使用“#”
作为分隔符,使用strtok()
循环缓冲区。完成后,别忘了只释放原稿