用不同的数据类型为结构数组的文本文件 [英] Text file with different data types into structure array

查看:121
本文介绍了用不同的数据类型为结构数组的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要解析3种不同的数据类型为文本文件。我希望它具有三个成员被保存在一个结构数组。我的文本文件是这样的:

  A B 45.78965
A C 35.46731
B C 46.78695

这是我与阅读它的程序是下面的,它不工作。
我在做什么错了?

 的#include<&stdio.h中GT;结构GRA {
    从CHAR;
    CHAR到;
    双瓦;
};诠释的main()
{
    FILE *计划生育=的fopen(graph.txt,R);
    INT I = 0;
    而(!的feof(FP)){
        的fscanf(FP,%[^ \\ t],&安培;图[I]。从,和放大器;图[I]。要,&安培;图[I] .W);
        我++;
    }
    FCLOSE(FP);
 }


解决方案

你的一个问题是,你正在使用阅读%[^ \\ t] ,其内容为字符串,并把结果存储到不是字符数组变量(两个字符和一个双)。

尽管这不是从你的问题清楚,看来你输入的线路包含两个字符和一个制表符分隔的一个实数。如果是这样,你应该使用下面的的fscanf 来阅读:

 的fscanf(FP,%C \\ t%C \\ T%LF \\ n,&安培;图[I]。从,和放大器;图[I]。为,与放;图[I] .W);

如果你不知道究竟分隔字段,并要允许在开头和也额外空白和行结束之间的任意数量的空格,然后使用:

 的fscanf(FP,%C%C%LF \\ n,&安培;图[I]。从,和放大器;图[I]。要,&安培;图[我] .W);

这是在格式中使用一个额外的空间,每个%C之前明确地跳过空白。

您code也有一对夫妇的其他问题:


  1. 您正在使用的feof 来检查文件的末尾。如果你不按字符读取文件字符这通常不能很好地工作。相反,你应该检查你的的fscanf 返回3,也就是说,如果它成功地读取,你想它读三件事情。


  2. 您缺少数组定义的


我添加了完整的code,我会做解析写的:

 的#includestdio.h中
#定义MAX 100结构{
  从,为char;
  双瓦;
}图[MAX];诠释的main()
{
  FILE *计划生育=的fopen(graph.txt,RT);
  的for(int i = 0; I< MAX;我++)
    如果(的fscanf(FP,%C%C%LF \\ n,&放大器;图[I]。从,&放大器;图[I]。要,与放大器;图[I] .W)3;)
      打破;
  FCLOSE(FP);
  返回0;
}

I have to parse a text file with 3 different data types. I want it to be saved in a structure array with three members. My text file look like this:

A B 45.78965
A C 35.46731
B C 46.78695

The program that I'm reading it with is the following and it does not work. What am I doing wrong?

#include <stdio.h>

struct gra {
    char from;
    char to;
    double w;
};

int main ()
{
    FILE *fp = fopen("graph.txt", "r"); 
    int i = 0;
    while (!feof(fp)) {
        fscanf(fp, "%[^\t]", &graph[i].from, &graph[i].to, &graph[i].w);
        i++;
    }
    fclose(fp);
 }

解决方案

One of your problems is that you're reading using %[^\t], which reads strings, and store the result to variables that are not character arrays (two characters and a double).

Although it's not clear from your question, it seems that the lines of your input contain two characters and one real number separated by one tab character. If that is so, you should use the following fscanf to read them:

fscanf(fp, "%c\t%c\t%lf\n", &graph[i].from, &graph[i].to, &graph[i].w);

If you are not sure what exactly separates your fields and you want to allow any amount of white space in between and also extra white space in the beginning and end of the line, then use:

fscanf(fp, " %c %c%lf\n", &graph[i].from, &graph[i].to, &graph[i].w);

that is, use an extra space in the format before each "%c" to explicitly skip white space.

Your code has also a couple of other problems:

  1. You are using feof to check for end of file. This will usually not work well if you're not reading the file character by character. Instead, you should check if your fscanf returned 3, that is, if it successfully read the three things that you wanted it to read.

  2. You are missing a definition of array graph.

I'm adding the complete code that I'd write for doing the parsing:

#include"stdio.h"
#define MAX 100

struct {
  char from, to;
  double w;
} graph[MAX];

int main ()
{
  FILE *fp = fopen("graph.txt", "rt");  
  for (int i=0; i<MAX; i++)
    if (fscanf(fp, " %c %c%lf\n", &graph[i].from, &graph[i].to, &graph[i].w) < 3)
      break;
  fclose(fp);
  return 0;
}

这篇关于用不同的数据类型为结构数组的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆