Hi All, I know I'm new here and a 'beginner' by many peoples standards, so I shall apologise in advance for the ensuing rant.
Arrrgggghhhh. This is like wading through treacle. Honestly, talk about making an easy job unbelievably hard work. No wonder I stick to VB. Yes I know that's probably a swear word here, but what I'm trying to achieve took me 3 minutes in VB and, up to press, I've gone through over 8 hours and goodness knows how many forums and web sites trying to find the answer for C++. Sorry, deep breaths, calm blue ocean.
It should be a simple task. I have a text file which contains three integers, comma separated in rows like this:
22,45,2
34,5,6
21,5,0
and so on...
I'm using Borland c++ builder, and would like to use the TOpenDialog control to allow the user to select the file, then place the contents in an array.
I've tried so many variations of code I've lost track. Everything I try throws some sort of error.
To explain better here's how I did it in VB:
Private Sub cmdLoad_Click()
Dim strFile As String
Dim intLoop As Integer
Dim DataIn(2,256) As Integer
dlgMain.ShowOpen
strFile = dlgMain.FileName
Open strFile For Input As #1
For intLoop = 1 To 256
Input #1, DataIn(0, intLoop), DataIn(1, intLoop), DataIn(2, intLoop)
Next
Close #1
End Sub
Simple. Please could someone show me how to do this? The bruise on my forehead is getting quite big now!
If the numbers was separated by space it would be a little easier.
1 2 3 4 5 6
std::ifstream file("filename.txt");
int n1, n2, n3;
while (file >> n1 >> n2 >> n3)
{
// do something with the three numbers
}
If you really want comma separated numbers you can read the commas into a char variable and test if they are equal to ','.
1 2 3 4 5 6 7 8 9 10 11 12
std::ifstream file("filename.txt");
int n1, n2, n3;
char c1, c2;
while (file >> n1 >> c1 >> n2 >> c2 >> n3)
{
if (c1 != ',' || c2 != ',')
{
// error, number is not separated by comma
break; // stop the loop
}
// do something with the three numbers
}
Thanks for the replies, I really do appreciate it.
Now the only thing I'm struggling with is getting the file name into the 'put file name here'
Everything I try results in an error. Again looking at it from a VB perspective, the TOpenDialog1-> Filename looks like a property of TOpenDialog so I expected to be able to drop it straight into a string:
Hey Electro. I don't quite understand your last question but here is some nice, readable code to solve your original question. Walkthrough this code and it should be clear what is going on.
#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{
ifstream input; //object to read data from the input file
int num1, num2, num3; //variables to store the three integers in each line
char ch; //garbage variable to store the commas in the file
//open the input file
input.open("inFile.txt");
//If the file could not be opened for whatever reason, terminate the program
if(!input)
{
cout << "The file could not be opened. Terminating program..." << endl;
return 0;
}
//Continue reading data from a file until the end of file is reached
while(!input.eof())
{
//Get num1, then the comma, then num2, then the comma, then num3
input >> num1 >> ch >> num2 >> ch >> num3;
//Output the three numbers to the console
cout << num1 << " " << num2 << " " << num3 << endl;
}
return 0;
}
By the way, if you want the user to enter the name of the file (I think this is what you mean?), you can store the file name in a string variable. The only thing is that the ifstream member function open only accepts a cstring, so you have to convert the string to a cstring like so..
1 2 3 4 5 6 7
ifstream input;
string fileName;
cout << "Enter the name of the input file: ";
cin >> fileName;
input.open(fileName.c_str());
The string member function c_str converts a string object into whats knows as a cstring.