-
Sorry CornedBee, but I am real stuck here! I did look at numbers.dat and created the necessary separators (snippet below). However, in spite of my research at a couple of discussion boards, I am unable to get a handle of how to use std::string that you recommended to be able to open numbers.dat. Sadly, even my C++ book does not address this issue. I know you have already walked me this far, but I will appreciate even more if you give me a clue of what I have to do to use std::string to solve the subject problem. What do I do with std::string in this case? BTW, the limitation imposed by use of the 15 characters originated from the source of this problem example - Walter Savitch. I trust, if I find my way around this problem, I will be able to contemplate better ways to solve similar problems in future. Many thanks and sorry for all the trouble!!
Code:
#include <fstream>
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int num1, num2, num3, num4;
//string numbers;
cout << "Enter the four numbers: " << endl;
cin >> num1 >> num2 >> num3 >> num4;
cout << "The numbers are: " << num1 << " "<< num2 << " " << num3 << " " << num4;
ofstream writer("numbers.dat");
if(!writer)
{
cout << "Error opening file for output" << endl;
return -1;
}
writer << num1 << ", " << num2 <<", " << num3 <<", " << num4 << endl;
writer.close();
return 0;
}
-
Here's how I suggest you read the filenames:
Code:
std::string filename;
std::getline(std::cin, filename);
std::ifstream input(filename.c_str());
This allows the filename to be any length and contain spaces. (I'm somewhat doubtful about the quality of the book you're using if it used raw char arrays by the way.)
Second, I suggest you only use spaces as separators. The commas will be in the way.
Third, if your second program still can't find the file, you need to understand the concept of a program's "current working directory" and relative paths. This is something that is largely hidden from today's desktop users, is very relevant when you work on the console, and also matters to programs. If you want to be a programmer, this is something you need to learn, even though authors usually tend to forget that today's computer users don't learn this anymore and therefore don't write about it.
-
Thank you very much CornedBee for seeing me through this. I really appreciate. With all your insight and a little more online research, I now have the exact solution to my problem. By inputting in the appropriate directory string in the second program (I had to discard the old one and adopt a completely new approach as you advised!), I have a program that open numbers.dat and manipulates the contents as required of it, and then outputs on the screen the results.
Here below is the new program and it works real fine. Again, thank you very much!
Code:
#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
using namespace std;
int main()
{
ifstream finput;
string dir, filepath;
int num1, num2, num3, num4;
DIR *dp;
struct dirent *dirp;
struct stat filestat;
cout << "dir to get files of: " << flush;
getline( cin, dir ); // gets everything the user ENTERs
dp = opendir( dir.c_str() );
if (dp == NULL)
{
cout << "Error(" << errno << ") opening " << dir << endl;
return errno;
}
while ((dirp = readdir( dp )))
{
filepath = dir + "/" + dirp->d_name;
// If the file is a directory (or is in some way invalid) we'll skip it
if (stat( filepath.c_str(), &filestat )) continue;
if (S_ISDIR( filestat.st_mode )) continue;
// Endeavor to read a single number from the file and display it
finput.open( filepath.c_str() );
if (finput >> num1 >> num2 >> num3 >> num4)
cout << filepath << ": " << num1 << " " << num2 << " " << num3 << " " << num4 << " "
<< num1+num2+num3 << endl;
finput.close();
}
closedir( dp );
return 0;
}