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

  • SEARCH
  • Home
  • 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 9073683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:28:21+00:00 2026-06-16T18:28:21+00:00

I am trying to write a program that reads information from a text file

  • 0

I am trying to write a program that reads information from a text file and copies it into an array using structs and unions and cstrings. My textfile looks something like this.

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

My query is, I’ve written a simple switch case with a for loop that reads in the information based on its criteria. For instance, the first letter before each paragraph denotes whether the student is a Foreigner, F, or Singaporean, S. Based to their nationality, I’m choosing which struct/union to copy their information into. To give you a better idea, this is what my final text output file will look like after I take in the information from the original text file and process it.

https://i.stack.imgur.com/Bv2YS.jpg

Below is my section of code I am having trouble with. It seems whether or not the char variable reads “S” or “F”, my switch statement is only reading it in as “F”.

        //file to array.


        char dateJunk;
        int numOfCourses;

        int k = 0;
        while (!afile.eof())
        {
            for (int k = 0; k < 3; k++)
            {
            afile >> locale;
                switch (locale)
                {
                    case 'F':
                        afile.getline(x[k].st.foreignStudent.nationality, MAX);
                        afile >> x[k].st.foreignStudent.gender;
                        afile.getline(x[k].st.foreignStudent.name, MAX);
                        afile >> x[k].st.foreignStudent.bd.day;
                        afile >> dateJunk;
                        afile >> x[k].st.foreignStudent.bd.month;
                        afile >> dateJunk;
                        afile >> x[k].st.foreignStudent.bd.year;
                        afile >> x[k].st.foreignStudent.numOfCourses;

                        for (int i = 0; i < x[k].st.foreignStudent.numOfCourses; i++)
                        {
                            afile >> x[i].st.foreignStudent.subjects[k];

                            afile >> x[i].st.foreignStudent.grades[k];
                        }
                        break;

                    case 'S':
                        afile >> x[k].st.localStudent.gender;
                        afile.getline(x[k].st.localStudent.name, MAX);
                        afile >> x[k].st.localStudent.bd.day;
                        afile >> dateJunk;
                        afile >> x[k].st.localStudent.bd.month;
                        afile >> dateJunk;
                        afile >> x[k].st.localStudent.bd.year;
                        afile >> x[k].st.localStudent.numOfCourses;;
                        for (int i = 0; i < x[k].st.localStudent.numOfCourses; i++)
                        {
                            afile >> x[i].st.localStudent.subjects[k];

                            afile >> x[i].st.localStudent.grades[k];
                        }
                }
                }
        }

        //Tests my cstring arrays to see everything is copied in correctly.
        for (int k = 0; k < 3; k++)
        {
        cout << locale << " " << x[k].st.foreignStudent.nationality;
        cout << endl;
        cout << x[k].st.foreignStudent.gender;
        cout << x[k].st.foreignStudent.name;
        cout << endl;
        cout << x[k].st.foreignStudent.bd.day << " - ";
        cout << x[k].st.foreignStudent.bd.month << " - ";
        cout << x[k].st.foreignStudent.bd.year;
        cout << endl;
        cout << x[k].st.foreignStudent.numOfCourses;
        cout << endl;
        for(int i = 0; i < x[k].st.foreignStudent.numOfCourses; i++)
        {
        cout << x[i].st.foreignStudent.subjects[k] << " ";
        cout << x[i].st.foreignStudent.grades[k] << " ";

        }
            cout << endl;
        }

        return 0;
    }

Below is the code in its entirety.

    #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 nationality[MAX];
        char gender[MAX];
        Birthday bd;
        char subjects [MAX][MAX];
        char grades [MAX][MAX];
        int numOfCourses;

    };

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

    union Student
    {
        Local     localStudent;
        Foreigner foreignStudent;
    };

    enum CountryType {S, F};

    struct UowStudents
    {
        CountryType ct;
        Student st;
    };

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

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

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


    }

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

        if (!afile)
        {
            cout << fileName << "could not be opened for read" << endl;
            exit (-1);
        }
        //file to array.


        char dateJunk;
        int numOfCourses;

        int k = 0;
        while (!afile.eof())
        {
            for (int k = 0; k < 3; k++)
            {
            afile >> locale;
                switch (locale)
                {
                    case 'F':
                        afile.getline(x[k].st.foreignStudent.nationality, MAX);
                        afile >> x[k].st.foreignStudent.gender;
                        afile.getline(x[k].st.foreignStudent.name, MAX);
                        afile >> x[k].st.foreignStudent.bd.day;
                        afile >> dateJunk;
                        afile >> x[k].st.foreignStudent.bd.month;
                        afile >> dateJunk;
                        afile >> x[k].st.foreignStudent.bd.year;
                        afile >> x[k].st.foreignStudent.numOfCourses;

                        for (int i = 0; i < x[k].st.foreignStudent.numOfCourses; i++)
                        {
                            afile >> x[i].st.foreignStudent.subjects[k];

                            afile >> x[i].st.foreignStudent.grades[k];
                        }
                        break;

                    case 'S':
                        afile >> x[k].st.localStudent.gender;
                        afile.getline(x[k].st.localStudent.name, MAX);
                        afile >> x[k].st.localStudent.bd.day;
                        afile >> dateJunk;
                        afile >> x[k].st.localStudent.bd.month;
                        afile >> dateJunk;
                        afile >> x[k].st.localStudent.bd.year;
                        afile >> x[k].st.localStudent.numOfCourses;;
                        for (int i = 0; i < x[k].st.localStudent.numOfCourses; i++)
                        {
                            afile >> x[i].st.localStudent.subjects[k];

                            afile >> x[i].st.localStudent.grades[k];
                        }
                }
                }
        }

        //Tests my cstring arrays to see everything is copied in correctly.
        //The print for foreign student cstrings also has information for the
        //one Singaporean student "S" in the middle. Singaporean must go into
        //the local cstrings.
        for (int k = 0; k < 3; k++)
        {
        cout << locale << " " << x[k].st.foreignStudent.nationality;
        cout << endl;
        cout << x[k].st.foreignStudent.gender;
        cout << x[k].st.foreignStudent.name;
        cout << endl;
        cout << x[k].st.foreignStudent.bd.day << " - ";
        cout << x[k].st.foreignStudent.bd.month << " - ";
        cout << x[k].st.foreignStudent.bd.year;
        cout << endl;
        cout << x[k].st.foreignStudent.numOfCourses;
        cout << endl;
        for(int i = 0; i < x[k].st.foreignStudent.numOfCourses; i++)
        {
        cout << x[i].st.foreignStudent.subjects[k] << " ";
        cout << x[i].st.foreignStudent.grades[k] << " ";

        }
            cout << endl;
        }

//as you can see, everything gets copied into localStudent struct as well.
        for (int k = 0; k < 3; k++)
        {

            cout << x[k].st.localStudent.gender;
            cout << x[k].st.localStudent.name;
            cout << endl;
            cout << x[k].st.localStudent.bd.day << " - ";
            cout << x[k].st.localStudent.bd.month << " - ";
            cout << x[k].st.localStudent.bd.year;
            cout << endl;
            cout << x[k].st.localStudent.numOfCourses;
            cout << endl;
            for(int i = 0; i < x[k].st.localStudent.numOfCourses; i++)
            {
                cout << x[i].st.localStudent.subjects[k] << " ";
                cout << x[i].st.localStudent.grades[k] << " ";

            }
            cout << endl;
        }




        return 0;
    }
  • 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-16T18:28:23+00:00Added an answer on June 16, 2026 at 6:28 pm

    You are seeing the effects of using a union in C++.

    The main characteristic of a union that all members share the same block of memory. And because your member structures Local and Foreigner have exactly the same layout, you can use either to print the contents of the union.

    If you change one of the structures (for example, remove nationality from Local), you will see that some of the printouts get garbled (notably, those entries where you print a foreign student as a local one of vice versa). This is because the memory block used for the students get interpreted differently for local and foreign students.

    Note: Technically, this results in undefined behaviour, but in this case the results are unlikely to be catastrophic. Officially, you can only read the same member of a union that you last wrote to, or (if the members are structures) the initial members of a structure that are the same as in the structure last written to the union.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying to write a program that reads from a text file into a
I'm trying to write a program that reads in entries from a file into
I'm trying to write a program that reads text from external file (string string
I am trying to write a Java program that reads an input file consisting
So, I'm trying to write a c program that reads input piped into the
I am trying to write a program that sends basic text files from one
I'm trying to write a program that reads 2 numbers from the user and
I'm trying to write a program that will read from a flat file of
I'm trying to write a program that reads 2 numbers from the user and
I'm trying to write a java program that reads data from an image and

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.