Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C语言中将文本文件中的一行拆分为多个变量_C - Fatal编程技术网

如何在C语言中将文本文件中的一行拆分为多个变量

如何在C语言中将文本文件中的一行拆分为多个变量,c,C,我有一个结构数组,其索引量与文本文件中的行数相同,其中包含以下变量: char[] model, float version, int price, char[] color; 我正在从一个文件中读取数据,该文件有行,每行填充一个结构。以下是文件中几行的示例: F150 5.4 28000白色 RAM1500 5.7 32000橙色 因此,如果我正在阅读第一行,我想将F150保存在char[]model中,5.7保存在float版本中,28000保存在int price中,白色保存在char

我有一个结构数组,其索引量与文本文件中的行数相同,其中包含以下变量:

char[] model, float version, int price, char[] color;
我正在从一个文件中读取数据,该文件有行,每行填充一个结构。以下是文件中几行的示例:

F150 5.4 28000白色
RAM1500 5.7 32000橙色
因此,如果我正在阅读第一行,我想将F150保存在
char[]model
中,5.7保存在float版本中,28000保存在
int price
中,白色保存在
char[]color
中,所有这些都存储在数组的第一个位置的结构中

我不确定如何遍历文本文件并将不同的变量分配给数组中的每个结构,如有任何帮助,将不胜感激

我当前访问和读取此文件的代码如下:

void accessFile(char *fn) {
struct vehicle *array;
int count = 0;
char line[100];
FILE *fp = fopen(fn, "r");
while(fgets(line, sizeof(line), fp) != 0) {
    count++;
}

array = (struct vehicle *) malloc(count * sizeof(struct vehicle));

rewind(fp);
while(fgets(line, sizeof(line), fp) != 0) {
    count++;
    }
}
我第一次浏览了文件,找到了文件中的行,以便知道创建数组的大小。我的计划是在每一个结构中为每一行分配相应的变量,但我正在努力学习如何区分每一行的各个部分的语法。

您可以使用sscanf()来拆分该行

因为我很无聊,我就这样做了:

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct
{
  char* model;
  float version;
  int   price;
  char* color;
}
vehicle;

bool vehicle_create( vehicle* v, const char* model, float version, int price, const char* color )
{
  v->model   = strdup( model );
  if (!(v->model)) return false;

  v->version = version;
  v->price   = price;
  v->color   = strdup( color );  

  if (v->color) return true;

  free( v->model );
  return false;
}

void vehicle_destroy( vehicle* v )
{
  if (!v) return;
  if (v->model) free( v->model ); v->model = NULL;
  if (v->color) free( v->color ); v->color = NULL;
}

bool read_vehicle( FILE* f, vehicle* v )
{
  char  model[ 50 ];
  char  color[ 50 ];
  char  line[ 200 ];
  float version;
  int   price;

  if (!fgets( line, sizeof(line), f )) 
    return false;

  if (4 != sscanf( line, "%50s %f %d %50s", model, &version, &price, color ))
    return false;

  return vehicle_create( v, model, version, price, color );
}

size_t read_vehicle_inventory( FILE* f, vehicle* vs, size_t n )
{
  size_t count = 0;
  while (count < n 
      && read_vehicle( f, vs + count ))
    count += 1;
  return count;
}

int main()
{
  const size_t MAX_INVENTORY_SIZE = 100;
  vehicle inventory[ MAX_INVENTORY_SIZE ];
  size_t inventory_size = 0;

  // Load inventory from file
  inventory_size = read_vehicle_inventory( stdin, inventory, MAX_INVENTORY_SIZE );

  printf( "inventory size = %d\n", (int)inventory_size );

  // Display the inventory
  for (size_t n = 0; n < inventory_size; n++)
    printf( "%s %s %f is $%d\n", 
      inventory[ n ].color,
      inventory[ n ].model,
      inventory[ n ].version,
      inventory[ n ].price );

  // Free inventory
  for (size_t n = 0; n < inventory_size; n++)
    vehicle_destroy( inventory + n );

  return 0;
}
#包括
#包括
#包括
#包括
类型定义结构
{
char*模型;
浮动版本;
国际价格;
字符*颜色;
}
交通工具
bool vehicle_create(vehicle*v,const char*model,float version,int price,const char*color)
{
v->model=strdup(model);
如果(!(v->model))返回false;
v->version=版本;
v->价格=价格;
v->color=strdup(颜色);
如果(v->color)返回true;
自由(v->model);
返回false;
}
无效车辆销毁(车辆*v)
{
如果(!v)返回;
如果(v->model)自由(v->model);v->model=NULL;
如果(v->color)自由(v->color);v->color=NULL;
}
bool read_车辆(文件*f,车辆*v)
{
char模型[50];
炭色[50];
字符行[200];
浮动版本;
国际价格;
如果(!fgets(线,尺寸f(线),f))
返回false;
如果(4!=sscanf(行“%50s%f%d%50s”,型号、版本、价格、颜色))
返回false;
返回车辆(v、型号、版本、价格、颜色);
}
尺寸读取车辆库存(文件*f,车辆*vs,尺寸n)
{
大小\u t计数=0;
而(计数

享受。

到目前为止,您还没有编写任何代码,您是否尝试过?因此,这不是一种编码服务,不要指望我们为您提供代码。你必须证明你是最少尝试的。根据这个例子,你正在从文件中读取字符串。因此,这里有一个提示:您需要解析字符串,并在解析过程中找出要解析的元素。然后需要将相应的元素转换为相应的数据类型,即将ascii“5.4”转换为float。祝你好运。@Pablo我添加了代码,但这对回答我的问题真的没有多大帮助,因为这不是因为有些东西不起作用,而是我不知道如何构造语法。我从来没有要求别人写代码,正如你在上面看到的,我说“任何帮助都将不胜感激”,所以我正在寻找一个正确的方向,就像Serge所做的那样。@DrewPesall不会把这些评论当成是个人的,但我们不能读心,我不知道你是否尝试过一些东西,但失败了,或者只是懒惰,想从我们这里得到代码。现在您已经发布了代码,我们可以对其进行评论,指出您的错误,等等。昨天我对一个与您的问题几乎相同的问题给出了答案,请参见