简体   繁体   English

文件中的 fscanf 字符串

[英]fscanf string from file

I have i file "student_read.txt" that my code is supposed to read from.我有我的代码应该从中读取的文件“student_read.txt”。

the file contains this:该文件包含以下内容:

3872187 3872187

John Doe约翰·多伊

21 21

then its going to print the information as seen in the print_student function.然后它将打印在 print_student 函数中看到的信息。 but it seems like when it reads from the file with fscanf it detects the space between John and doe as enter which makes it so the output is.但似乎当它使用 fscanf 从文件中读取时,它会检测到 John 和 doe 之间的空格作为输入,这使得输出如此。

student id: 3872187学生证:3872187

full name: John全名:约翰

age: Doe年龄:母鹿

what can i do to make it print the output:我该怎么做才能让它打印输出:

student id: 3872187学生证:3872187

full name: john doe全名:约翰·多伊

age: 21年龄:21

#include <stdio.h>
#include <string.h>
#define STRING_LENGTH 100
//Struct with alias student_t that contains student information.
typedef struct student_t{
    char studentId[STRING_LENGTH];
    char studentName[STRING_LENGTH];
    char studentAge[STRING_LENGTH];
}student_t;

//function for printing the student information
void print_student(struct student_t student){
    printf("\nStudent id: %s\n", student.studentId);
    printf("Name: %s\n", student.studentName);
    printf("Age: %s\n", student.studentAge);

}

int main() {
    //Use the defined struct to crate an instance of Student
    struct student_t student;

    //Zero out all the memory of the struct instance
    memset(&student, 0, sizeof(student));

    //selecting option
    int option;
    printf("Choose an option");
    scanf("%i", &option);


    switch(option){
        case 1:{
            FILE* read = fopen("student_read.txt", "r");

            fscanf(read, "%s", &student.studentId);
            fscanf(read, "%s", &student.studentName);
            fscanf(read, "%s", &student.studentAge);




            print_student(student);


        }
        break;
        case 2:{
            //Asks for student_t id
            printf("\nStudent id: ");
            scanf("%s", &student.studentId);

            //getchar(); is used to prevent newline in input of fgets function.
            getchar();

            //Asks for full name (strcpy since datatype = string)
            char name[STRING_LENGTH] = {0};
            printf("\nFull name: ");
            fgets(name, STRING_LENGTH, stdin);
            name[strlen(name)- 1] = 0;
            strcpy(student.studentName, name);


            //Asks for age
            printf("\nAge: ");
            scanf("%s", &student.studentAge);

        }
        break;
        case 3:{
            printf("Program closing");
        }
        break;

        default:
            printf("Invalid Option... Try again");


    }
/*





    FILE* write = fopen("student_write.txt", "w");

    if (read==0){
        printf("failed to open file\n");
        return -1;
    }

    fclose(read);
    fclose(write);
*/

    return 0;
}

You can use fgets instead of fscanf .您可以使用fgets而不是fscanf This function will be reading characters until it finds a newline character or end-of-file, so it won't stop at the whitespace.此函数将读取字符,直到找到换行符或文件结尾,因此它不会在空格处停止。

Edit: if you need to use fscanf compulsory, you can check this response: R: Can fscanf() read whitespace?编辑:如果你需要强制使用fscanf ,你可以查看这个回复: R: Can fscanf() read whitespace?

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

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