rrz0

Reading strings and integers from file

Dec 11th, 2018
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.17 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. /*
  8.  * File:   main.c
  9.  * Author: ryanr
  10.  *
  11.  * Created on 11 December 2018, 14:43
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17.  
  18.  
  19. /*
  20.  *
  21.  */
  22.  
  23. typedef struct person {
  24.     char name[10][10];
  25.     char surname[10][10];
  26.     int age [10];
  27. } person_t;
  28.  
  29. int main(int argc, char** argv) {
  30.  
  31.     FILE *inp; /* pointer to input file */
  32.     FILE *outp; /* pointer to ouput file */
  33.     char c;
  34.     int word_count = 0;
  35.     int char_count = 0;
  36.     int s_word_count = 0;
  37.     int s_char_count = 0;
  38.     int input_status = 0;
  39.     int i = 0;
  40.  
  41.     /* Prepare files for input or output */
  42.     inp = fopen("in_file.txt", "r");
  43.    
  44.     person_t my_person;
  45.  
  46.     if (inp) {
  47.         while ((c = fgetc(inp)) != EOF) {
  48.             if (c == ' ' || c == '\r') {
  49.                 printf("\n");
  50.  
  51.                 my_person.name[word_count][char_count] = '\0'; //Terminate the string
  52.                 char_count = 0; //Reset the counter.
  53.                 word_count++;
  54.             }
  55.             else {
  56.                
  57.                 if (c >= '0' && c <= '9') {
  58.                     x = c - '0';
  59.                     my_person.age[i] = x;
  60.                     printf("Age: %d", my_person.age[i]);
  61.                     i++;
  62.                 }
  63.                 else {
  64.                     my_person.name[word_count][char_count] = c;
  65.                     printf("%c",my_person.name[word_count][char_count]);
  66.  
  67.                     if (char_count < 19) {
  68.                         char_count++;
  69.                     }
  70.                     else {
  71.                         char_count = 0;
  72.                     }
  73.                 }
  74.             }  
  75.         }
  76.     }
  77.     else {
  78.         printf("No can do\n");
  79.     }
  80.    
  81.     return (EXIT_SUCCESS);
  82. }
  83.  
  84.  
  85. //File Input
  86. Patrice 95
  87. Rio 96
  88. Marcus 78
  89. Wayne 69
  90. Alex 67
  91. Chris 100
  92. Nemanja 78
  93.  
  94. //Current Output
  95.     Patrice
  96.     95
  97.    
  98.     Rio
  99.     96
  100.    
  101.     Marcus
  102.     78
  103.    
  104.     Wayne
  105.     69
  106.    
  107.     Alex
  108.     67
  109.    
  110.     Chris
Advertisement
Add Comment
Please, Sign In to add comment