Besides what Jochen pointed out in his solution 1, the biggest problem with your code is that you are buffering the input in various arrays of fixed size, 50 at the moment. These are NodeName, x, y, and z. As long as these array have a fixed capacity of 50 you will not be able to read files with more than 50 lines.
The solution to this is: First determine the number of lines in your file. Then go back to the beginning. Now allocate your arrays dynamically with new or by using std::vector. And finally read the file again and parse the lines into your arrays.
Better yet: Use the above approach, but these variables into a structure and then allocate an array of these structures, either with new or std::vector<mystructure>. That simplifies looping and you always have all data belong to one object in a structure. This will simplifiy argument passing when your program is being expanded.
[AMENDED]
Ok, let me explain a little more detailed. First I would define a structure that hold all the data of a single line. For example:
struct Node
{
long name;
double x, y, z;
};
To store an arbitrary number of such structures you could use an STL vector. Here is how you would define that:
#include <vector> // at the beginning of your .cpp file
...
std::vector<Node> nodes;
To access the 5th such node you would simply write: nodes[4]. Of course you have to tell the array, for how many nodes it should allocate storage. That is done by calling the resize function:
nodes.resize (1000);
This would allocate 1000 structures of type Node. The problem is that you don't know how many nodes you need until you have read the entire file. You could call resize before reading the next record of your file, each time with a number that is 1 higher than the previous time. Another way is to use the push_back function, which appends just one more structure to the end of the array. So your loop would look like this:
while (fgets(line, sizeof line, pFile) != NULL)
{
Node newNode;
int cntRead = sscanf_s(line, "%d %lf %lf %lf",
&newNode.name[i], &newNode.x[i], &newNode.y[i], &newNode.z[i]);
if (cntRead != 4)
{
... output error message
return -1; }
nodes.push_back (newNode);
}
After having read the entire file you can now access your nodes in arbitrary order. For example a backwards loop would look like:
for (int idx = (int) nodes.size() - 1; idx >= 0; --idx)
{
const Node& node = nodes[idx];
}
I hope that gives you an idea on how to proceed.
BTW, if you only want to re-order the lines in that file, you don't have to parse it with sscanf. Simply store the lines of your file in a vector, must like shown above, and then output those lines in reverse order. I leave that as a little exercise for you.