简体   繁体   中英

fscanf string from file

I have i file "student_read.txt" that my code is supposed to read from.

the file contains this:

3872187

John Doe

21

then its going to print the information as seen in the print_student function. 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.

student id: 3872187

full name: John

age: Doe

what can i do to make it print the output:

student id: 3872187

full name: john doe

age: 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 . 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?

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