简体   繁体   English

C 语言 - 从文本文件中读取特定数据

[英]C language - Read specific data from text file

I'm studying file I/O.我正在研究文件 I/O。 I have a trouble in reading specific data.我在读取特定数据时遇到问题。

text file :文本文件 :

index (x,y)

 1     2,3   1,5   8,2

 2     4,4

 3     0,1   9,4

 4

The number of (x,y) can be changed. (x,y)可以改变。

I read only numbers with following code:我只阅读了以下代码的数字:

while (1){而 (1){

    getNum = fscanf(fp, "%d", &num);

    if (getNum == EOF)

        break;

    else if (getNum < 1)

        fscanf(fp, "%*[^0-9]");

    else

        printf("%d\t", num);

    }

How can I split index, x, y?如何拆分索引,x,y?

Follow these steps:按着这些次序:

  • Read the file line by line逐行读取文件
  • Split a single line with space " " as delimiter into fields将带有空格“”作为分隔符的单行拆分为字段
  • starting at the second field (if present): split each field using comma "," as delimiter从第二个字段开始(如果存在):使用逗号“,”作为分隔符分割每个字段
  • convert each subfield from string to integer将每个子字段从字符串转换为整数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM