Reading/Writing text files

Hi all, I'm new to these forums and fairly new to C++ programming. I'm entering my first year at University, and I'm trying to learn some C++ so I'm not clueless when my course starts.

I can do the basics, initializing & setting variables, if/and/or statements, etc. However, I'm lost when it comes to reading and writing text files, and the tutorial on this website didn't quite cover what I was after.

What I'm looking to do is simple (I hope). I want to read the lines of a .txt file (all numbers), have the program prompt the user to search for either the highest number or lowest number, validate the selection, and then have the program display the details on screen. Oh, and I also want to convert part of a line to a string.

My text file looks like this:

XX YYY.ZZ
XX YYY.ZZ
etc for as long as needed


What I want to do is read the text file, assign the XX value a name based on what it is (For example, if I wanted each number to represent a person, the program would assign their name to the output) and then post the YYY.ZZ value. So if the line was 01 517.13, the program would output it as "Name 517.13".

Once the program has read all of the lines (Going to use 10), I want it to write out the new converted lines to another file, so the lines look like this:

NN XX(Converted) YYY.ZZ Calculation


NN is the line number, straightforward. 01 for the first line, 02 for the second, etc. The XX(Converted) is the original XX value from the first file, except for it is now the name, not the numerical value. The YYY.ZZ value stays the same, and the calculation is a the YYY.ZZ value after having a mathematical calculation done on it.


Any help is appreciated, as I am fairly lost. My thoughts are as follows:

Initialize variables
Set variables (the names that will be assigned to the XX values, and the calculations I'll be doing on the YYY.ZZ values)
Open the text file
Read the lines (using a While loop, stopping when eof reached)
Assign each line to two variables, so "if XX = 01 assign name, else continue" and then it would check if it was 02, 03, 04, etc. The second variable is the YYY.ZZ value.
Preform the mathematical calculation on the YYY.ZZ value.
Ask the user to enter 1 for lowest to highest, and 2 for highest to lowest.
Take the input and sort the YYY.ZZ values accordingly.
Display either the highest or the lowest value (according to selection)
Write all of the lines to a new text file, based on user selection.
Get familiar with Classes:
http://www.cplusplus.com/doc/tutorial/classes.html
http://www.cplusplus.com/doc/tutorial/classes2.html

I would also google for some basic tutorials on object orientated development.

Some File IO:
http://www.cplusplus.com/doc/tutorial/files.html

And the STL Vector and Maps:
http://www.sgi.com/tech/stl/table_of_contents.html
To build on what Zaita suggests, a class is a good choice to hold such an object that can also do I/O.
1
2
3
4
5
6
7
8
9
10
11
12
struct LineData  // a struct is the same as a public class
  {
  unsigned line_number;  // NN
  unsigned name_index;   // XX (unconverted)
  string   name;         // XX (converted)
  double   value;

  friend istream& operator >> ( istream& ins, LineData& line_data );
  friend ostream& operator << ( ostream& outs, const LineData& line_data );
  friend bool operator < ( const LineData& left, const LineData& right );
  friend bool operator > ( const LineData& left, const LineData& right );
  };

Now all there is to do is write the operator functions that read and write the data.

You can store an array of those objects in a vector:
vector <LineData> lines;

Then you can use the sort() algorithm (#include <algorithm>) to sort the class using either the ascending or descending comparison functions (the < or > operators).
1
2
3
4
5
6
7
// ascending sort
sort( lines.begin(), lines.end() );

// descending sort
sort( lines.rbegin(), lines.rend() );
// or
sort( lines.begin(), lines.end(), (bool(*)( const Foo&, const Foo& ))( operator > ) );

Line 5: descending sort because reverse iterators are used
Line 7: descending sort because the > operator is used as the comparison function (instead of the default < operator).

Turning name_index into a name can be done either in the read operator, the write operator, or some other function which you add to the class, or even directly in your main code.

You can use an array/vector to hold a list of names, or you can use a map to hold an associative array that takes a number and turns it into a name. The map is convenient because you can easily ask the user to supply a name for each number no matter what order the numbers are listed, without having to ask the user for the same name more than once.

Oh yeah, before I forget, if you just want the highest or lowest value in the range you don't necessarily need to sort it. You can use the max_element() and min_element() standard algorithms (#include <algorithm>).

Good luck!
Topic archived. No new replies allowed.