在C语言中,从较长的字符串中提取子字符串的最佳方法是什么
我有一个格式化为字符串的文件,后跟一长串由空格分隔的浮点: 字符串(空格)浮点(空格)。。。浮动 字符串(空格)浮点(空格)。。。浮动 字符串(空格)浮点(空格)。。。浮动 每一行的字符串和浮点数都要放在一个结构中,目前我的做法是使用fgets将每一行存储为字符串,然后通过该字符串递增,检查空格之间的子字符串,然后将这些字符串转换为浮点数并存储在我的结构中在C语言中,从较长的字符串中提取子字符串的最佳方法是什么,c,string,file-io,C,String,File Io,我有一个格式化为字符串的文件,后跟一长串由空格分隔的浮点: 字符串(空格)浮点(空格)。。。浮动 字符串(空格)浮点(空格)。。。浮动 字符串(空格)浮点(空格)。。。浮动 每一行的字符串和浮点数都要放在一个结构中,目前我的做法是使用fgets将每一行存储为字符串,然后通过该字符串递增,检查空格之间的子字符串,然后将这些字符串转换为浮点数并存储在我的结构中 这将变得非常乏味和复杂。有更好的方法吗 根据要固定或可变的变量数量,有两种可能的方法。如果浮点数固定,可能的解决方案可能是: #inc
这将变得非常乏味和复杂。有更好的方法吗 根据要固定或可变的变量数量,有两种可能的方法。如果浮点数固定,可能的解决方案可能是:
#include <stdio.h>
int main(int argc, char ** argv)
{
FILE * file = fopen("test_data.txt", "r");
if (file == NULL)
{
printf("Cannot open file.\n");
return 1;
}
char string[32] = { 0 };
float f1, f2, f3, f4;
while (feof(file) == 0)
{
fscanf(file, "%s %f %f %f %f", string, & f1, & f2, & f3, & f4);
printf("For string: %s values are:\n\t%f %f %f %f\n", string, f1, f2, f3, f4);
}
fclose(file);
return 0;
}
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void remove_trailing_spaces(char ** begin)
{
while (isspace(** begin))
++(* begin);
return;
}
static void get_string(char ** begin, char * output)
{
remove_trailing_spaces(begin);
char * end = * begin;
while (isalnum(* end))
++end;
strncpy(output, * begin, (int)(end - * begin));
* begin = end;
return;
}
static void get_float(char ** begin, float * output)
{
remove_trailing_spaces(begin);
char * end;
* output = strtof(* begin, & end);
* begin = end;
return;
}
int main(int argc, char ** argv)
{
FILE * file = fopen("test_data.txt", "r");
if (file == NULL)
{
printf("Cannot open file\n");
return 1;
}
char buffer[1024] = { 0 };
char string[32] = { 0 };
while (feof(file) == 0)
{
if (fgets(buffer, 1024, file) != NULL)
{
char * begin = & buffer[0];
get_string(& begin, string);
printf("For string: %s values are:\n\t", string);
while ((feof(file) == 0) && (* begin != '\n'))
{
float f = 0.0;
get_float(& begin, & f);
printf("%f ", f);
}
printf("\n");
}
}
fclose(file);
return 0;
}
包含以下数据的“test_data.txt”文件:
test1 1.41 1.73 2.78 3.14
test2 2.41 2.73 3.78 4.14
test1 1.41 1.73 2.78 3.14
test2 2.41 2.73 3.78 4.14 5.15
用于读取数据的源文件可能是:
#include <stdio.h>
int main(int argc, char ** argv)
{
FILE * file = fopen("test_data.txt", "r");
if (file == NULL)
{
printf("Cannot open file.\n");
return 1;
}
char string[32] = { 0 };
float f1, f2, f3, f4;
while (feof(file) == 0)
{
fscanf(file, "%s %f %f %f %f", string, & f1, & f2, & f3, & f4);
printf("For string: %s values are:\n\t%f %f %f %f\n", string, f1, f2, f3, f4);
}
fclose(file);
return 0;
}
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void remove_trailing_spaces(char ** begin)
{
while (isspace(** begin))
++(* begin);
return;
}
static void get_string(char ** begin, char * output)
{
remove_trailing_spaces(begin);
char * end = * begin;
while (isalnum(* end))
++end;
strncpy(output, * begin, (int)(end - * begin));
* begin = end;
return;
}
static void get_float(char ** begin, float * output)
{
remove_trailing_spaces(begin);
char * end;
* output = strtof(* begin, & end);
* begin = end;
return;
}
int main(int argc, char ** argv)
{
FILE * file = fopen("test_data.txt", "r");
if (file == NULL)
{
printf("Cannot open file\n");
return 1;
}
char buffer[1024] = { 0 };
char string[32] = { 0 };
while (feof(file) == 0)
{
if (fgets(buffer, 1024, file) != NULL)
{
char * begin = & buffer[0];
get_string(& begin, string);
printf("For string: %s values are:\n\t", string);
while ((feof(file) == 0) && (* begin != '\n'))
{
float f = 0.0;
get_float(& begin, & f);
printf("%f ", f);
}
printf("\n");
}
}
fclose(file);
return 0;
}
用于读取数据的源文件可能是:
#include <stdio.h>
int main(int argc, char ** argv)
{
FILE * file = fopen("test_data.txt", "r");
if (file == NULL)
{
printf("Cannot open file.\n");
return 1;
}
char string[32] = { 0 };
float f1, f2, f3, f4;
while (feof(file) == 0)
{
fscanf(file, "%s %f %f %f %f", string, & f1, & f2, & f3, & f4);
printf("For string: %s values are:\n\t%f %f %f %f\n", string, f1, f2, f3, f4);
}
fclose(file);
return 0;
}
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void remove_trailing_spaces(char ** begin)
{
while (isspace(** begin))
++(* begin);
return;
}
static void get_string(char ** begin, char * output)
{
remove_trailing_spaces(begin);
char * end = * begin;
while (isalnum(* end))
++end;
strncpy(output, * begin, (int)(end - * begin));
* begin = end;
return;
}
static void get_float(char ** begin, float * output)
{
remove_trailing_spaces(begin);
char * end;
* output = strtof(* begin, & end);
* begin = end;
return;
}
int main(int argc, char ** argv)
{
FILE * file = fopen("test_data.txt", "r");
if (file == NULL)
{
printf("Cannot open file\n");
return 1;
}
char buffer[1024] = { 0 };
char string[32] = { 0 };
while (feof(file) == 0)
{
if (fgets(buffer, 1024, file) != NULL)
{
char * begin = & buffer[0];
get_string(& begin, string);
printf("For string: %s values are:\n\t", string);
while ((feof(file) == 0) && (* begin != '\n'))
{
float f = 0.0;
get_float(& begin, & f);
printf("%f ", f);
}
printf("\n");
}
}
fclose(file);
return 0;
}
#包括
#包括
#包括
#包括
静态无效删除尾随空格(字符**开始)
{
while(isspace(**开始))
++(*开始);
返回;
}
静态void get_字符串(字符**开始,字符*输出)
{
删除尾随空格(开始);
字符*结束=*开始;
while(isalnum(*结束))
++结束;
strncpy(输出,*begin,(int)(end-*begin));
*开始=结束;
返回;
}
静态void get_float(字符**开始,浮点*输出)
{
删除尾随空格(开始);
字符*结束;
*输出=strof(*开始和结束);
*开始=结束;
返回;
}
int main(int argc,字符**argv)
{
FILE*FILE=fopen(“test_data.txt”、“r”);
if(file==NULL)
{
printf(“无法打开文件\n”);
返回1;
}
字符缓冲区[1024]={0};
字符字符串[32]={0};
while(feof(file)==0)
{
如果(fgets(缓冲区,1024,文件)!=NULL)
{
char*begin=&缓冲区[0];
获取字符串(&开始,字符串);
printf(“对于字符串:%s,值为:\n\t”,字符串);
而((feof(file)==0)和(*begin!='\n'))
{
浮动f=0.0;
获取浮点值(&begin,&f);
printf(“%f”,f);
}
printf(“\n”);
}
}
fclose(文件);
返回0;
}
请记住,这可能不是最好的解决方案。它只表明在
test_data.txt
文件的每一行中改变数据数量来解析文本文件比在第一种情况下要花费更多的精力。每一行中的浮点数是固定的还是可变的?您当前的方法没有错,一点也不复杂。显示您的代码。数字是可变的。不幸的是,案例是第二个,但我感谢您的帮助<代码>while(feof(file)==0)-->