简体   繁体   English

如何从c中的文件中读取包含空格,整数和浮点值的字符串?

[英]How to read string with spaces, integers and float values from a file in c?

Hi first of all let me be precise, I have a data file in following format: 嗨,首先,我要精确地说,我有一个以下格式的数据文件:

样本文件

The first line contains a string followed by three integers and a float data type. 第一行包含一个字符串,后跟三个整数和一个float数据类型。
The second line contains string with a space followed by three integers and a float data type. 第二行包含带空格的字符串,后跟三个整数和一个float数据类型。
The last row contains a string followed by three integers and a float data type. 最后一行包含一个字符串,后跟三个整数和一个float数据类型。
My aim is to read these data and assign to an array of structure, a structure in an array contains one row with string, 3 integers and one float number. 我的目的是读取这些数据并分配给结构数组,数组中的结构包含一行包含字符串,3个整数和一个浮点数的行。 I tied using the following code and got succeeded to read a line where string has no space, but cannot read string with spaces: 我使用以下代码进行绑定,并成功读取了其中字符串没有空格但无法读取带有空格的字符串的行:

void readFromDatabase(struct student temp[], int *no) {
    FILE *filepointer;
    int i = 0;
    if ((filepointer = fopen("database", "r")) == NULL) {
        printf("Read error");
        return;
    }
    while (fscanf(filepointer, "%10s\t%d%d%d%f\n", temp[i].name,
            &temp[i].birthday.date, &temp[i].birthday.month,
            &temp[i].birthday.year, &temp[i].gpa) != EOF && i < MAX_CLASS_SIZE) {
        ++i;
    }
    *no = i;
    fclose(filepointer);
}

I got the follwing output which was unexpected: 我得到了以下意外的输出:

意外的输出


I was trying to loop through the structure array and display the data in above format. 我试图遍历结构数组并以上述格式显示数据。
But instead of getting 3 rows of output i got 4 rows. 但是我没有得到3行输出,而是得到了4行。
I really need some help on this topic. 我真的需要一些有关此主题的帮助。
Thanks in advance... 提前致谢...

And i am using gcc under ubuntu 16.04 for compiling and executing the program.. 我正在ubuntu 16.04下使用gcc来编译和执行程序。

In your fscanf format string, use %10[^\\t] instead of %10s . fscanf格式字符串中,使用%10[^\\t]而不是%10s This matches each character of the name until the tabulator separator. 这与名称的每个字符匹配,直到制表符分隔符为止。

Unfortunately, you did not give a full example, so this is my small program to test on Ubuntu 16.04: 不幸的是,您没有给出完整的示例,所以这是我要在Ubuntu 16.04上测试的小程序:

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

#define MAX_CLASS_SIZE 4

struct student {
        char name[11];
        struct {
                int date;
                int month;
                int year;
        } birthday;
        float gpa;
};

int main(void) {
        FILE *filepointer;
        int i = 0;
        int no;
        struct student temp[MAX_CLASS_SIZE];
        if ((filepointer = fopen("data.txt", "r")) == NULL) {
                printf("Read error");
                return EXIT_FAILURE;
        }
        while (fscanf(filepointer, "%10[^\t]\t%d%d%d%f\n", temp[i].name,
                                &temp[i].birthday.date, &temp[i].birthday.month,
                                &temp[i].birthday.year, &temp[i].gpa) != EOF && i < MAX_CLASS_SIZE) {
                ++i;
        }
        no = i;
        fclose(filepointer);

        for(i = 0; i < no; i++) {
                printf("%s: %d-%d-%d %f\n",
                        temp[i].name,
                        temp[i].birthday.date, temp[i].birthday.month, temp[i].birthday.year,
                        temp[i].gpa
                );
        }
        return EXIT_SUCCESS;
}

Based on your format string, I am assuming that all values in your database are separated by tabulators. 根据您的格式字符串,我假设数据库中的所有值都由制表符分隔。

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

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