Hello, Basically I am having trouble reading in a file. I have a class that has
ID: 3639208 • Letter: H
Question
Hello,
Basically I am having trouble reading in a file. I have a class that has a few functions that retreive, remove and destroy a list. I am trying to read in the file to the orderedList class use these functions. here is the coding for the main app...
What Im trying to read in is the key.. The key is combined of Flight #+Date+Time (see function buildKey).
I need to make a function to read in the file so I can use my orderedList to retreive and remove. If im not making myself clear please let me know. I know it sounds kinda confusing. Thanks in advance!
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
using namespace std;
struct flightType
{
char airport [4];
char flightNo[4];
char date[9];
char time[6];
};
void readString (fstream &, char *, int);
void buildKey (const flightType &, char *);
int main()
{
// declarations
ordered <flightType> orderedList;
fstream flightFile;
flightType flightData;
char key[17];
int numFlights = 0;
// executable program
flightFile.open("../flights.txt", ios::in);
if (flightFile.fail())
{
cerr << " *** file does not exist";
return 0;
}
flightData.airport[3] = '';
flightData.flightNo[3] = '';
flightData.date[8] = '';
flightData.time[5] ='';
readString(flightFile, flightData.airport,3);
while (!flightFile.eof())
{
readString(flightFile, flightData.flightNo,3);
readString(flightFile, flightData.date,8);
readString(flightFile, flightData.time,5);
buildKey (flightData, key);
cout << "flight " << flightData.airport << ' '
<< flightData.flightNo << ' '
<< flightData.date << ' '
<< flightData.time << ' '
<< "Key = " << key << endl;
++numFlights;
readString(flightFile, flightData.airport,3);
}
cout << " Read " << numFlights << " records. ";
flightFile.close();
return 0;
}
void readString (fstream & inFile, char * string, int length)
{
inFile.get (string, length + 1);
inFile.get(); // removes space or newline
}
void buildKey (const flightType& a, char * key)
{
strcpy (key, a.flightNo); // copy flightNo
strncat (&key[3], a.date, 8); // concatenate date
strncat (&key[11], a.time, 5); // concatenate time
key[16] = ''; // all string end in NULL
}
Explanation / Answer
Hi here im giving to read student information from file and store them in an ordered doubly linked list. the list should be ordered according to the student ID. The information in the file aren't ordered. this may be helpfull.. void list::insert() { string name1 ,name2 ,name3 ; int id , year ,grade; node *temp; ifstream inData; inData.open("inputfile.txt"); if (!inData) { cerrname1 >>name2 >>name3 >>year >>grade; temp->set(id ,name1,name2,name3,year,grade); orderedList(temp); } } inData.close(); } void list::orderedList(node *temp) { node *curr ,*prev; bool found=false; if (first==NULL) { first = temp ; last = temp; counter++; } else { found=false; curr= first; while (!found && curr!=NULL) { if (curr->getID() >= temp->getID()) found = true; else { prev =curr; curr=curr->next; } } if (curr==first) { first->back= temp; temp->next = curr; first=temp; counter++; } else { if (curr!=NULL) { prev->next = temp; temp->back=prev; temp->next=curr; curr->back=temp; } else { prev->next=temp; temp->back=prev; last=temp; } counter++;} }}Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.