Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- int main(void) {
- //char line[] = "Foo Bar 42";
- char line[20];
- char name[20][20];
- char name_r[10];
- char surname_r[10];
- FILE *inp; /* pointer to input file */
- inp = fopen("in_file.txt", "r");
- {
- if (inp) {
- while (fgets(line, sizeof line, inp)) {
- int age;
- char *space = strrchr(line, ' ');
- if (!space) /* error, bad line */;
- if (space - line >= 30) /* error, name too long */;
- sprintf(name, "%.*s", space - line, line);
- age = strtol(space, NULL, 10); // needs error checking
- printf("[%s]; %d\n", name, age);
- // ...}
- }
- }
- }
- return 0;
- }
- Input:
- Patrice Evra 95
- Rio Ferdi 96
- Marcus Rash 78
- Wayne Roon 69
- Alex Ferg 100
- Nemanja Vida 78
- Output:
- [Patrice Evra]; 95
- [Rio Ferdi]; 96
- [Marcus Rash]; 78
- [Wayne Roon]; 69
- [Alex Ferg]; 100
- [Nemanja Vida]; 78
Advertisement
Add Comment
Please, Sign In to add comment