简体   繁体   中英

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.
The second line contains string with a space followed by three integers and a float data type.
The last row contains a string followed by three integers and a float data type.
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. 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.
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..

In your fscanf format string, use %10[^\\t] instead of %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:

#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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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