How can this be more efficient?

I am just looking through my code to see how to be more efficient/elegant. I feel like the writeFile function is not very efficient but i really can't think of how to improve it. i included the readFile function so you can see how the data is read from file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*****************************************************************************
 * reads file and puts data into linked list
 *****************************************************************************/
bool Trip::readTripFromFile()
{
   string startCity, destCity, transMode;
   int miles;
   UI ui;
   
   ifstream fin(filename.c_str());
   if (fin.fail())
   {
      ui.eraseLine();
      cout << "Error reading file\n";
      return false;
   }
   int month, day, year, hr, min;
   int month2, day2, year2, hr2, min2;
   
   getline(fin, startCity);
   
   while (!fin.eof())
   {
      getline(fin, destCity);
      fin >> transMode;
      fin >> miles;
      
      fin >> month >> day >> year >> hr >> min;
      Date startDate(month, day, year, hr, min);
      
      fin >> month2 >> day2 >> year2 >> hr2 >> min2;
      Date arrivalDate(month2, day2, year2, hr2, min2);
      
      fin.ignore(80,'\n');
      Leg leg(startCity, destCity, transMode, miles, startDate, arrivalDate);
      
      if (head == NULL)
      {
         insert(head, leg);
         endLeg = head;
      }
      else
      {
         insert(endLeg, leg);
         endLeg = endLeg->getLink();
      }
      
      getline(fin, startCity);
   }
   
   return true;
}

/*****************************************************************************
 * writes trip info to file in format that is compatible with the readFile function
 *****************************************************************************/
bool Trip::writeTripToFile()
{
   ofstream fout(filename.c_str());
   if (fout.fail())
   {
      cout << "Error opening file\n";
      return false;
   }
   Node<Leg>* currentNode = head;
   while (currentNode != NULL)
   {
      fout << currentNode->getData().getStartCity() << endl;
      fout << currentNode->getData().getDestCity() << endl;
      fout << currentNode->getData().getTransMode() << endl;
      fout << currentNode->getData().getMilesBetween() << endl;
      fout << currentNode->getData().getStartDateTime().getMonth() << " ";
      fout << currentNode->getData().getStartDateTime().getDay() << " ";
      fout << currentNode->getData().getStartDateTime().getYear() << " ";
      fout << currentNode->getData().getStartDateTime().getHour() << " ";
      fout << currentNode->getData().getStartDateTime().getMinute() << endl;
      fout << currentNode->getData().getArrivalDateTime().getMonth() << " ";
      fout << currentNode->getData().getArrivalDateTime().getDay() << " ";
      fout << currentNode->getData().getArrivalDateTime().getYear() << " ";
      fout << currentNode->getData().getArrivalDateTime().getHour() << " ";
      fout << currentNode->getData().getArrivalDateTime().getMinute() << endl;
      currentNode = currentNode->getLink();
   }
   return true;
   
   
}
each endl send to a ofstream drives another nail into the coffin of efficiency.

As for elegance, consider providing operator>> and operator<< to your classes, then it would become simply fout << currentNode->getData() << '\n';, or even fout << currentNode << '\n';

And of course, don't use while (!fin.eof()), although you didn't ask about correctness.
Topic archived. No new replies allowed.