I am writing a program that will have .dat files within a folder. The user will input the folder, and there should be a few .dat files within. How do I open up the files to read them into an array?! Please respond ASAP!! Thank you in advance for your help.
*NOTE: Use the most basic C++ you can; I am a beginner! Thanks! The entire getDir() was given to us to complete the assignment, I didn't write that!*
const int MAX_SIZE = 31;
int getDir(string dir, stringVector &files);
int main()
{
ifstream in_file;
ofstream out_file;
int i=0, array[MAX_SIZE], next;
float average_temp, maximum_temp;
int start;
string dir, intro;
intro = "C://*/*/*/*/*/*/Homework 06";
cout << "Enter directory with files available for use: "; cin >> dir;
cout << endl << intro + dir << endl;
stringVector files = stringVector();
getDir (dir, files);
for (unsigned int i = 0; i < files.size(); i++)
{
cout << files[i] << endl;
in_file.open(files[i].c_str());
while (in_file >> next) {
array[i] = next;
i++;
}
for (int i = 0; i < MAX_SIZE; i {
cout << array[i] << " ";
}
}
in_file.close();
out_file.close();
cin >> start;
return 0;
}
int getDir (string dir, stringVector &files)
{
DIR *dp;
struct dirent *dirp;
I would think intro = "C://*/*/*/*/*/*/Homework 06"; is asking for trouble.
If your filename has C:, this is probably a Windows application, but you're using Unix file/directory functions.
What platform are you using? What compiler are you using? File/Directory handing isn't part of the C++ language, it's dependent on supporting libraries; and in this case, environment specific libraries.
Well, you added another for loop to get rid of all the number unused. However, I tried what you told me to, and this time I get very very large numbers (not what I entered into the dataset).
see the first loop will read the whole file, or to the limit of the array.
second loop will print the array till it is filled, "i" will tell till what limit it is filled.
ok, tell me whats in the file and what output you are getting then i may say something.
or better paste some part of your file/dataset here.
i executed the program which i posted above with the data you gave, the output is coming out nicely.
i created a file test.txt with the above data, and i got this output..
36
54
76
65
63
64
... more numbers...
Copy and paste the code that you used to access the data? i dont think we have the same code then. I really don't think that compiler matters for the program that I'm running...
That works (as expected), but that's not the problem. I need help with the directory portion of the code...How to access a certain file, read the dataset, and then close that file, and then go to the next, read the dataset, and then close, etc.