Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9092819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:55:55+00:00 2026-06-16T22:55:55+00:00

I am trying to read a text file into a cstring array with struct/union

  • 0

I am trying to read a text file into a cstring array with struct/union implementations. First off, this is what my text file looks like:

F South Korea
Male Psy Park Jae Sang
31 - 12 - 1977
3 CSCI114 55 CSCI103 44 GangNam 100

S Female Super Junior
5 - 8 - 1978 
2 CSCI114 60 CSCI103 80

F People Republic Of China
Unknown James Bond
11 - 12 - 1976
4 CSCI114 54 CSCI124 66 CSCI007 99 CSCI123 28

Now, ignoring the obvious Asian popstar references my lecturer decided to fill this example with – I’d like to be able to read the first number before the CSCI, which is the number or courses and their respective grades and automate(loop) it for all the 3 (or possibly more) students in the text file.

I have not written any code for what I’m trying to describe here but here is what I have so far.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;

const int MAX = 80;

struct Birthday
{
    char day[MAX];
    char month[MAX];
    char year[MAX];
};

struct Local
{
    char name[MAX];
    char gender[MAX];
    Birthday bd;
    char subjects [MAX];
};

struct Foreigner
{
    char name[MAX];
    char nationality[MAX];
    char gender[MAX];
    Birthday bd;
    char subjects [MAX];
};

union Student
{
    Local     localStudent;
    Foreigner foreignStudent;
};

enum CountryType {S, F};

struct UowStudents
{
    CountryType ct;
    Student st;
};

int fileToArray (fstream& afile, char fileName [], UowStudents& x);

int main ()
{
    srand(time_t(NULL));

    fstream afile;
    UowStudents x [MAX];
    char fileName[MAX];
    cout << "Enter filename: ";
    cin >> fileName;
    int size = fileToArray (afile, fileName, *x);


}

int fileToArray (fstream& afile, char fileName [], UowStudents& x)
{
    afile.open(fileName, ios::in);

    if (!afile)
    {
        cout << fileName << "could not be opened for read" << endl;
        exit (-1);
    }
    //file to array.
    char locale;
    char dateJunk;
    afile >> locale;
    afile.getline(x.st.foreignStudent.nationality, MAX);
    afile >> x.st.foreignStudent.gender;
    afile.getline(x.st.foreignStudent.name, MAX);
    afile >> x.st.foreignStudent.bd.day;
    afile >> dateJunk;
    afile >> x.st.foreignStudent.bd.month;
    afile >> dateJunk;
    afile >> x.st.foreignStudent.bd.year;


    //Tests my cstring arrays to see everything is copied in correctly.
    cout << locale << " " << x.st.foreignStudent.nationality;
    cout << endl;
    cout << x.st.foreignStudent.gender;
    cout << x.st.foreignStudent.name;
    cout << endl;
    cout << x.st.foreignStudent.bd.day << " - ";
    cout << x.st.foreignStudent.bd.month << " - ";
    cout << x.st.foreignStudent.bd.year;
    return 0;
}

This is what my final text file with all the array information processed, would look like:

https://i.stack.imgur.com/4g4CM.jpg

Any help is appreciated, thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T22:55:56+00:00Added an answer on June 16, 2026 at 10:55 pm

    You can read in each course list this way:

    int numOfCourses;
    afile >> numOfCourses;
    for (int i = 0; i < numOfCourses; i++)
    {
        string course;
        afile >> course;
    
        int grades;
        afile >> grades;
    
        // Fill in structure from `course` and `grades`...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to read a text file that looks like this 0,-16,-4,12,10,4,-14,8,44,8,8,12,-4 1,-16,-4,12,10,4,-14,6,43,10,10,12,-4 2,-16,-6,10,11,2,-13,4,43,10,11,12,-4
i am trying to read jpg file and convert into byte array this is
I am trying to read a text file into a two dimensional character array.
I'm trying to read a text file containing 20 names into an array of
I'm trying to read values from a text file into a hashtable, I want
I am trying to read a simple text file into a String. Of course
trying to read text into memory from a text file the user selects MODEL:
Im trying to read a text file, x bytes at a time into a
I am trying to read lines stored in a separate text file into my
I am trying to read a .txt file into a multi-line text box with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.