简体   繁体   中英

write file to structs

I want to write and read some statics from file to structs. write from structs to file is work of but write data from file isn't ok. I will be grateful to help me what is the problem.

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

const char* MemberFormatIn="(%[^,], %[^,], %[^,], %d)";
const char* MemberFormatOut="(%s, %s, %s, %d)\n";

typedef struct member {
    char name[20],
        lastName[20],
        address[20];
    int age;
}p1,p2;

void inputMemberList(struct member p[],int count) {
    
    printf("name:");
    scanf("%s",p[count].name);

    printf("lastName:");
    scanf("%s",p[count].lastName);

    printf("address:");
    scanf("%s",p[count].address);

    printf("age:");
    scanf("%d",&p[count].age);
    
}

void printMemberList(struct member p[],int count) {
    system("cls");

    for(int i=0; i<count; i++) {
        printf("\aMember %d:\n", i+1);
        printf("Name is: %s", p[i].name);
        printf("\nLast name is: %s", p[i].lastName);
        printf("\nAddress is: %s", p[i].address);
        printf("\nAge is: %d", p[i].age);
        printf("\n\n");
    }
}
void saveMember(struct member p[],int count) {
    FILE* fp = fopen("member.txt","a+");

    if (fp == NULL) {
        printf("File can't open.");
        exit(1);
    }
    
    fprintf(fp, MemberFormatOut, p[count].name, p[count].lastName, p[count].address, p[count].age);

    fclose(fp);
}

void fileToStructure(struct member p[],int count) {
    FILE* fp = fopen("member.txt","a+");

    if (fp == NULL) {
        printf("File can't open.");
        exit(1);
    }

    fseek(fp, 0, SEEK_SET);
    for (int i=0; i<count+1; i++) {
        fscanf(fp, MemberFormatIn, p[i].name, p[i].lastName, p[i].address, &p[i].age);
    }
    
    

    fclose(fp);
}
int numberOfMember() {
    int count = 0;
    char c;
    FILE* fp = fopen("member.txt","r");
    if(fp == NULL) {
        printf("File can't open.");
        exit(1);
    }

    do {
        c = fgetc(fp);
        if(c == '\n')
            count++;
    }while(c != EOF);

    fclose(fp);

    return count;
}
int main() {
    int len = 100;
    p1 p[len];
    int count = numberOfMember();
    inputMemberList(p,count);
    saveMember(p,count);
    fileToStructure(p,count);
    printMemberList(p,count);
    return 0;
}

at result just statics of member true and shown show but the others doesn't true.

File data(example):

(ahmad, dad, tir, 12)
(hossein, dad, tiran, 12)
(ali, dad, tir, 15)
(mohammadi, mmad, tiron, 16)
(helma, dad, tiran, 5)
(mohammad, amin, dadkhah, 5)

output(example):

Member 1:
Name is: ahmad
Last name is: dad
Address is: tir
Age is: 12

Member 2:
Name is: ├wöΩ\`
Last name is:
Address is: ↑
Age is: 0

Member 3:
Name is: Ä
Last name is: t
Address is: e
Age is: 7471221

Member 4:
Name is: r
Last name is: l
Address is: o
Age is: 6881396

Member 5:
Name is: n
Last name is: s
Address is:
Age is: 0

Member 6:
Name is:
Last name is:
Address is:
Age is: 0

In fileToStructure() , the first iteration of the loop calls fscanf() and if all is OK, the next character to be read from the file will be the newline character at the end of the first line. Unfortunately for the next iteration, the next call to fscanf() is expecting to read the ( character, but instead it reads the newline character. This will cause this call to fscanf() and all the subsequent calls to fail to match anything and return EOF .

The code needs to eat the newline character. One way to do that is to change the MemberFormatIn format string to one of the following:

  • const char* MemberFormatIn=" (%[^,], %[^,], %[^,], %d)"; - will discard any whitespace characters before the ( character; or
  • const char* MemberFormatIn="(%[^,], %[^,], %[^,], %d) "; - will discard any whitespace characters after the ) character.

The first one (discarding whitespace before the ( character) would be preferable for interactive input, but it doesn't matter too much when reading from a file.

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