c - 读取C语言文本文件

标签 c file text

大家好,我有这个文件结构:

0
2 4
0: 1(ab) 5(b)
1: 2(b) 6(a)
2: 0(a) 2(b)
3: 2(a) 6(b)
4: 5(ab)
5: 2(a) 6(b)
6: 4(b) 6(ab)

每一行都会向一个结构体提供其数据(数字+字母)。
读取该行并获取我想要的字符串的最佳方法是什么?

示例:

0
2 4
0,1,ab,5,b
1,2,b,5,a
...

线条的大小可能会有所不同,因为我们可以有 1, 2, 3, .... 数字。

我已经做到了:

//struct 
#define MAX_ 20

struct otherstats{ //struct otherStats
  int conectstat[MAX_];//conection with others stats
  int transitions[MAX_];//Symbols betwen conection ASCI
}tableStats[MAX_];

struct sAutomate{
 int stat_initial; //initial
 int stats_finals[MAX_]; //final orfinals
 struct otherstats tableStats[MAX_]; //otherStats 0 1 2 3 4 5 6
};

/* eXample that what i want ..using the example 
sAutomate.stat_initial=0
sAutomate.stats_finals[0]=2
sAutomate.stats_finals[1]=4

Others Stats table
//0
sAutomate.tableStats[0].conectstat[0]=1;
sAutomate.tableStats[0].conectstat[1]=5;
sAutomate.tableStats[0].transitions[0]=ab;
sAutomate.tableStats[0].transitions[1]=b; 
//1
sAutomate.tableStats[1].conectstat[0]=2;
sAutomate.tableStats[1].conectstat[1]=6;
sAutomate.tableStats[1].transitions[0]=b;
sAutomate.tableStats[1].transitions[1]=a;
///etc
 */



void scanfile(){ //function to read the file

struct sAutomate st; //initialize st struct

char filename[] = "txe.txt";
FILE *file = fopen ( filename, "r" );
char buf[81];       
char parts[5][11];  

fscanf(file,"%d", &st.stat_initial);//read first line
printf("initial state : %d \n", st.stat_initial);
fscanf(file,"%d",&st.stats_finals);
fscanf(file,"%d",&st.stats_finals);


while (fgets(buf, sizeof(buf), stdin) != NULL)
{
if (sscanf(buf, "%10[^:]: (%10[^(], %10[^)]), (%10[^(], %10[^)])",
           parts[0], parts[1], parts[2], parts[3], parts[4]) == 5)
{
  printf("parts: %s, %s, %s, %s, %s\n",
         parts[0], parts[1], parts[2], parts[3], parts[4]);
}
else
{
  printf("Invalid input: %s", buf);
}
}
//fclose

最佳答案

我看到的第一个问题是您正在覆盖 stats_finals:

fscanf(file,"%d",&st.stats_finals);
fscanf(file,"%d",&st.stats_finals);

您想要在这里做的是:

fscanf(file,"%d",&st.stats_finals[0]);
fscanf(file,"%d",&st.stats_finals[1]);

从文本文件中保存“2”和“4”。

第二个主要问题是您正在从 stdin 读取内容:

while (fgets(buf, sizeof(buf), stdin) != NULL)

它不会读取您的文本文件,而是从键盘读取输入......所以您希望它是:

while (fgets(buf, sizeof(buf), file) != NULL)

第三个(小)问题是 fscanf() 不会读取换行符,而 fgets() 会。这意味着当您从读取第二个 stats_finals 到 while 循环中的第一次读取时,您的第一个输入将只是剩下的换行符。这不是什么大问题,因为您会检查“无效输入”,但值得注意。

最后,你的 sscanf 对我来说看起来不对:

sscanf(buf, "%10[^:]: (%10[^(], %10[^)]), (%10[^(], %10[^)])",
               ^                        ^
              That's a width of 10,     Why are you checking for commas? You didn't
              I don't think that's      have any in your text file
              what you wanted...

我认为这更符合您的需求:

sscanf(buf, "%[0-9]: %[0-9](%[^)]) %[0-9](%[^)])",
                ^
             takes a digit (0 to 9)
<小时/>

编辑 错过了你原来的观点。如果您不知道正在读取的字符串有多长,则无法使用 sscanf()。就是这么简单。 :)

scanf 系列假设您知道要解析多少个对象,并且格式字符串会接收这些对象。然而还有其他选择。

像您正在做的那样使用 fgets 读取一行,然后您可以对其进行标记。使用 C 函数 strtok 或您自己使用 for 循环。

但是有一点需要注意:

由于您不知道它有多长,因此: char parts[5][11]; 不是您的最佳选择。这将您限制为 2 个条目...可能最好动态执行此操作(读取该行,然后分配正确的大小来存储您的 token 。)

关于c - 读取C语言文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13973068/

相关文章:

c - 将 C 变量链接到 Lua

php - $_FILES ["file"] ["type"] 和 end(explode (".", $_FILES ["file"] ["name"])) 有什么区别

javascript - 在突出显示的文本附近显示 div(模态)

Git Merge - 不完整、丢失的文件和文件夹

javascript - 从输入类型文件中获取值

mysql - 当 TEXT 不足以用于记录条目时?

c - C 是否具有函数参数名称的 __func__ 功能?

c - 如何使用C语言将一系列位存储在变量中

c - 在二维数组中显示最大值和最小值时出现问题

c - 如何将文本文件传递到c中的数组中?