Reading a tab separated text file, visual C++ 6 URGENT

Jun 2, 2012 at 11:53am
Purpose of MFC SDI: read a text file having floating values with tab separated not comma

I added a menu Contour plot menu and 1. colour and 2. Gray Scale as sub menu. On pressing Colour a dialog box appears, whose purpose is to browse a text file. I browse and selct it and the path is visible in an EDIT box beside the browse button
and a plot button in dialog box to plot on the SDI window.

I am successful in browsing and selecting the text file. When I click plot button it should read and further porcess it which i am not able to implement. I ahve the logic to process it. I need to read the text file and store the floating point values in an 2D array. Anyone can help me please. its urgent!!
Dialog box picture looks somewhat like this

--------------------------------------------------------------------------|
|
-----------------------------------------------| |
|C://Mytext.txt | Browse Button |
| | |
------------------------------------------------

PLOT BUTTON
--------------------------------------------------------------------------








The dialog class i am attaching the code


void CColourdlg::OnBrowse() //BROWSE BUTTON
{
// TODO: Add your control notification handler code here

//Code to Browse Button and display in the edit box
CFileDialog FileDialog(TRUE,
"*.*",
NULL,
OFN_HIDEREADONLY,
"Text Files: (*.txt)|*.txt||");

if(FileDialog.DoModal() == IDOK)
{
CString path = FileDialog.GetPathName();
SetWindowText (path);
CEdit* cedit;
cedit = reinterpret_cast<CEdit *>(GetDlgItem(IDC_EDIT1));
cedit->SetWindowText(path);



}
}

void CColourdlg::OnPlot() //PLOT BUTTON
{
// TODO: Add your control notification handler code here
int i=0;
double array[10];
char cNum[10];
ifstream ifs;
ofstream ofs;

ifs.open(m_path,ios::nocreate); //m_Path is a variable atached
//to edit box but i am not able
// to get the path of the txt fil
while (ifs.good())
{
ifs.getline(cNum,256,' ');
array[i]= atof(cNum);
i++;

}
//OnOK();


}
Jun 3, 2012 at 4:23pm
I got how to take from edit box the path of the selected file...

Now i am stuck in reading the text file

My data is somewhat like this

24.54 /t 45.54 /t 34.5/t 34.4 /n
......../t..../t......................./n
I have tab delimiters and /n

As i could find i need to use ifstream, vector and string..
istream::getline function doesnot work as expected ..
Can any one help me out in this aspect.. please...
Jun 3, 2012 at 4:40pm
If you don't care about the lines, you can just read each value in a loop. If the lines are significant, you need to read an entire line at a time, then read the value from that as in:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <fstream>
#include <sstream>
#include <string>

int main()
{
	std::string line;
	std::ifstream is("test.csv");
	while (std::getline(is, line))	// read one line at a time
	{
		double value;
		std::istringstream iss(line);
		while (iss >> value)	// read one value at at time from the line
		{
			;		// do something with the value
		}
	}

	return 0;
}

Last edited on Jun 3, 2012 at 4:40pm
Jun 3, 2012 at 4:49pm
ok
Last edited on Jun 3, 2012 at 5:27pm
Jun 3, 2012 at 4:54pm
ok
Last edited on Jun 3, 2012 at 5:27pm
Jun 3, 2012 at 4:56pm
iss>>value... doesnot works it says illegal left operand is double
Jun 3, 2012 at 5:41pm
Yess now the code works properly.. small mistakess.. now its fine
Jun 5, 2012 at 12:25pm
When i click the dialog box open and does processing and a 2D vector lies in the dialog pushbutton function. How do i send it to ONDRAW function defined in VIEW.CPP file There i will plot this vector in terms of pixels.. Can u please suggest me with small example code to be written in pushbutton() and ONDRAW().. I would be highly thankful to u.. Please help me out!
Jun 6, 2012 at 12:37am
You don't talk to views, to talk to the document. You need to call UpdateAllViews on the document. That causes the views to refresh.
Topic archived. No new replies allowed.