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 8316389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:21:09+00:00 2026-06-08T21:21:09+00:00

I have posted the following code where I am reading from an input file

  • 0

I have posted the following code where I am reading from an input file — storing information in a structure — and then writing to an output file. I know that the eof function is not safe and hence one must use the getline function to check whether the end of file has been detected or not; however, in this particular code, I have not been able to use the getline function and hence has finally relied on the eof function. Hence, can you please suggest an alternative to the eof function or let me know how I can use the getline function when I am trying to initialize an array of structures . I have used two asterisk symbols to indicate where I want to use the getline function.

#include <iostream>
#include <fstream>

using namespace std;
//student structure
struct student
{
  char name[30];
  char course[15];
  int age;
  float GPA;
};

ifstream inFile;
ofstream outFile;

student getData();
void writeData(student writeStudent);
void openFile();

int main (void)
{
  const int noOfStudents = 3; // Total no of students
  openFile(); // opening input and output files

  student students[noOfStudents]; // array of students

  // Reading the data from the file and populating the array
  for(int i = 0; i < noOfStudents; i++)
  {
        if (!inFile.eof()) // ** This where I am trying to use a getline function.
            students[i] = getData();
        else
            break ;
  }


  for(int i = 0; i < noOfStudents; i++)
    writeData(students[i]);

  // Closing the input and output files
  inFile.close ( ) ;
  outFile.close ( ) ;

}

void openFile()
{
  inFile.open("input.txt", ios::in);
  inFile.seekg(0L, ios::beg);
  outFile.open("output.txt", ios::out | ios::app);
  outFile.seekp(0L, ios::end);

  if(!inFile || !outFile)
  {
    cout << "Error in opening the file" << endl;
    exit(1);
  }

}

student getData()
 {
  student tempStudent;
  // temp variables for reading the data from file

  char tempAge[2];
  char tempGPA[5];

  // Reading a line from the file and assigning to the variables
  inFile.getline(tempStudent.name, '\n');
  inFile.getline(tempStudent.course, '\n');
  inFile.getline(tempAge, '\n');

  tempStudent.age = atoi(tempAge);

  inFile.getline(tempGPA, '\n');
  tempStudent.GPA = atof(tempGPA);
  // Returning the tempStudent structure
  return tempStudent;
 }

void writeData(student writeStudent)
 {
  outFile << writeStudent.name << endl;
  outFile << writeStudent.course << endl;
  outFile << writeStudent.age << endl;
  outFile << writeStudent.GPA << endl;
 }
  • 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-08T21:21:12+00:00Added an answer on June 8, 2026 at 9:21 pm

    You want to write an operator>> for your student type. Something like:

    std::istream& operator>>(std::istream& in, student& s) {
      in >> s.age; // etc.
      return in;
    }
    

    Which then allows you to write:

    int studentNo = 0;
    students[maxStudents];
    while (studentNo < maxStudents && (in >> students[studentNo]))
      ++studentNo;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Posted in spring forum with no response. I have the following code snippet (from
Following on from a question I posted yesterday about GUIs, I have another problem
I have the following code in a layout: Posted <%=time_ago_in_words post.created_at %> ago <%
I have written the following code to perform some simultaneous HTTP posting and file
I have the following code: <form action= method=get onsubmit=doRequest($('word').value); $('word').value=''; return false;> <input type=text
I have the following code: $.ajax({ url: some-page.aspx, type: POST, data: {fileName: file, param02:
I have a js.erb file with the following code but I can't seem to
I have the following code to escape posted data: if (isset($_POST['submitted'])) { $trimmed_post =
I have the following code: <video width=640 height=360 controls id=video-player poster=/movies/poster.png> <source src=/movies/640x360.m4v type='video/mp4;
So I just have posted a question about this code (which was answered): $(document).ready(Main);

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.