50027. File Sorter

I'm a slow walker, but I never walk backwards.

Problem Description

You are given a name of a binary file consisting of structures. Your program should read the structures from this file, remove those structures that contain errors, and sort the remaining valid structures, and write them to another file. The structure is like the following.

fs.h

1
2
3
4
5
6
7
8
9
10
#ifndef _FS_H
#define _FS_h
 
typedef struct {
    int balance;
    char name[128];
    int gender;
} Account;
 
#endif

A structure is invalid if any of the following is true.

  • The balance is negative
  • The name (as a string) contains anything other than letters and space
  • The gender is not 0 or 1

We should sort the structure according to their names. It is guaranteed that no two names will be the same.

Subtasks

  • 15pt. The file has only one structure, and it is valid.
  • 15pt. The file has only one structure, and it may be invalid.
  • 40pt. The file may have more than one structure ($\leq 1024$), and they are all valid.
  • 30pt. The file may have more than one structure ($\leq 1024$), and they could be invalid.

Sample Input (stdin)

p50027-in.dat p50027-out.dat

Sample Input (p50027-in.dat)

$N$ valid or invalid structures in binary format. Read this file until EOF. ( $1 \leq n \leq 1024$ )

Sample Output (p50027-out.dat)

$M$ valid structures in binary format. ( $M \leq N$ )

p10065-in.dat

download p50027-in.dat, p50027-out.dat

1
2
3
4
5
6
7
8
9
10
Account A = {
    .balance = 50,
    .name = {'p', 'a', 'n', 'g', '\0'},
    .gender = 1
};
Account B = {
    .balance = 100,
    .name = {'f', 'e', 'n', 'g', '\0'},
    .gender = 0
};
morris821028@morris821028-PC MINGW64 ~/Desktop/demo
$ xxd p50027-in.dat
00000000: 3200 0000 7061 6e67 0000 0000 0000 0000  2...pang........
00000010: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000020: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000030: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000040: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000050: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000060: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000070: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000080: 0000 0000 0100 0000 6400 0000 6665 6e67  ........d...feng
00000090: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000000a0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000000b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000000c0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000000d0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000000e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000000f0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000100: 0000 0000 0000 0000 0000 0000 0000 0000  ................
 
morris821028@morris821028-PC MINGW64 ~/Desktop/demo
$ ./main
p50027-in.dat p50027-out.dat
 
morris821028@morris821028-PC MINGW64 ~/Desktop/demo
$ xxd p50027-out.dat
00000000: 6400 0000 6665 6e67 0000 0000 0000 0000  d...feng........
00000010: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000020: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000030: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000040: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000050: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000060: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000070: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000080: 0000 0000 0000 0000 3200 0000 7061 6e67  ........2...pang
00000090: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000000a0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000000b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000000c0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000000d0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000000e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000000f0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000100: 0000 0000 0000 0000 0000 0000 0100 0000  ................

Discussion