Thread: How to write to file with structure array and read it

  1. #1
    Registered User
    Join Date
    Dec 2020

    How to write to file with structure array and read it

    Hello. I’m a beginner at programming with C. I’m here because I need help from professional programmers. So, I have to do a simple program called “Lessons”. In this program, I need to use structure, an array of structures and use files to save all records.

    For now, I have a struct

    typed struct lessons{
    char LessonName [30],
    char TeacherName [20],
    char TeacherLastName[20],
    int numberOfStudent
    } lessons
    And array

    lessons info[10]
    As far I can understand I have an array called “info” which can handle 10 lessons information. Right?

    And now I face up with my biggest problem. How should I “play” with all records?

    Should I create a txt file and fill it up with some information or I should add the lesson’s information with code help?

    Can anybody explain, give some examples of how should I put the new record to a static array when the user enters all information about the lesson? Maybe with fscanf or fget.

    And also, how to scan all records from (txt or bin) file and show it on console?

    To make work easier I can give examples of records:

    Physical education Harry Pleter 32
    History Emily Shelton 12

    Thank you for your time.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Initializing an array of structs is similar to a multi-dimensional array:
    lessons info[10] = {
        { "Physical education", "Harry", "Pleter", 32 },
        { "History", "Emily", "Shelton", 12 },
        etc, etc
    Reading it from a file though, that a whole different beast. You need to think about the format, for starters. For example, if you use spaces for the different fields, then the lesson name can only be a single word. We usually go with a symbol that won't be used for anything else, or we "escape" symbols that might conflict with formatting.

    For simple things though, you could just use fscanf() for its built-in pattern matching, although that would make it quite complicated... Take a look at this example that parses 4 comma-separated strings:
    if (fscanf(file, "%[^,\n],%[^,\n],%[^,\n],%[^,\n]", str1, str2, str3, str4) != 4 || fgetc(file) != '\n') {
        fprintf(stderr, "Error: Invalid format at line #%d\n", line);
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 08-21-2014, 01:45 PM
  2. write to file .. pointer to structure array
    By mad_muppet in forum C Programming
    Replies: 11
    Last Post: 05-07-2011, 05:33 AM
  3. help with read/write file/array of chars
    By JustJonny in forum C Programming
    Replies: 8
    Last Post: 02-08-2011, 02:24 PM
  4. Replies: 5
    Last Post: 03-18-2006, 11:25 AM
  5. read/write Cstring array to file
    By bonkey in forum Windows Programming
    Replies: 1
    Last Post: 08-18-2002, 09:15 AM

Tags for this Thread