0

So i have task to output to one file certain things from another file... Lets say input file is in format (NAME#SURNAME AGE) with 100 lines and i have to output persons that are older then 15 in this format (NAME AGE SURNAME) and i have something like this in struct

struct person
{
    char name[10];
    char surname[10];
    int age;
};

Can I, and if I can how can I fread my input file to that struct (file is binary)

Dongilica
  • 73
  • 7
  • Can you post some example lines from the input? What have you tried? – KamilCuk Dec 30 '19 at 12:55
  • Peter#Schmit 16 – Dongilica Dec 30 '19 at 12:55
  • Jhon#Simons 9 and so on – Dongilica Dec 30 '19 at 12:56
  • i havent tried anything i dont have any idea how can i separate those two cause u dont have fixed number of chars in name and surname so u cant hard code it and pc sees it like one string – Dongilica Dec 30 '19 at 12:57
  • can you please give me short example how to do it – Dongilica Dec 30 '19 at 12:59
  • Ok. You read a whole line. Then you find the `#`. Them copy from the beginning of the line to the `#` character into `name`. Then you find the space in the line. You copy from after the `#` character to the space into `surname`. Then you find end of line. You convert from the next character after space to the end of line the character to a number (ex. with `atoi`). And you save the number into `age`. You can find [many examples of C online](https://www.google.com/search?q=fgets+example). – KamilCuk Dec 30 '19 at 12:59
  • "*(file is binary)*": What do you see if you open the input file using a text editor? – alk Dec 30 '19 at 13:07
  • well it looks crypted (wierd)... I opened it with notepad... and it is written in the task that input file is binary – Dongilica Dec 30 '19 at 13:13

1 Answers1

0

If you want more help about the code please do a first version and edit you question, then add a comment and the community will help you.

But if you are looking for the methodology you can go with something like that :

  1. Open the file you want to extract data in read mode (e.g. fopen(filename, "r"))
  2. Check if the file was correctly open
  3. Use a while and take N characters with fgets() or use getline() (Note that getline() is in ISO/IEC TR 24731-2:2010 extension (see n1248).).
    There is lots of threads about how to read line by line in a file (e.g. Going through a text file line by line in C)
  4. Now you are looking for a way to split your line with delimiters, your can use strtok(), strsep()/strpbrk() or you can do your own implementation of a simple string delimiter (Maybe look this question)
  5. Next the AGE part, you wan to only copy the line with an AGE superior to 15, so use strtol() to convert your string to long int then check the value.
    Don't use atoi() this function is deprecated).

    The atoi() function has been deprecated by strtol() and should not be used in new code.

    source Mac OS X Manual Page for atoi(3)

  6. Then you need to create and/or write to a file. You can use fopen(newFilename, "w"), check the file descriptor the function give you in return, if it's ok you can add line with fprintf() (e.g. fprintf(fd, "%s %s %s\r\n", name, age, surname);).

If this is one of your first project/exercise in c take your time to properly understand the function you use and always check the returned values before using them.

Ben Souchet
  • 1,450
  • 6
  • 20