File Handling ** Reading **

open file and read contetnts

Page 1 of 1

5 Replies - 442 Views - Last Post: 25 July 2009 - 04:53 PM Rate Topic: -----

#1 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 36
  • View blog
  • Posts: 710
  • Joined: 09-May 09

File Handling ** Reading **

Post icon  Posted 24 July 2009 - 10:58 AM

Hey guys im looking to open a file and read its contents and have it displayed within my list box

Heres what im trying

	CStdioFile File;
	CString Line;
 
	if(File.Open("d:\\test.txt", CFile::modeRead))
	{
		while(File.ReadString(Line))
		{
		  listBox.SetWindowText(Line);
		}
	}else{
		AfxMessageBox("could not open file", MB_ICONINFORMATION);

	}


}



For some reson it wont open the file ??

im so puzzled with this

Kevin

Is This A Good Question/Topic? 0
  • +

Replies To: File Handling ** Reading **

#2 Speedy_92  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 57
  • Joined: 02-July 09

Re: File Handling ** Reading **

Posted 24 July 2009 - 11:04 AM

Are you sure, that the file exists? ;)
Was This Post Helpful? 0
  • +
  • -

#3 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 36
  • View blog
  • Posts: 710
  • Joined: 09-May 09

Re: File Handling ** Reading **

Posted 24 July 2009 - 11:07 AM

lol yes im sure, i created the file myself.

to even make sure i just opened now lol

Kevin
Was This Post Helpful? 0
  • +
  • -

#4 alm0614  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 85
  • Joined: 23-June 09

Re: File Handling ** Reading **

Posted 24 July 2009 - 11:16 AM

why dont you use fstream
Was This Post Helpful? 0
  • +
  • -

#5 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 36
  • View blog
  • Posts: 710
  • Joined: 09-May 09

Re: File Handling ** Reading **

Posted 24 July 2009 - 11:18 AM

i cant because its MFC

cant include iostream or fstream
ive tried:(


kevin

ps im a beginner at MFC but ive been doing console apps for ages :(

This post has been edited by deery5000: 24 July 2009 - 11:24 AM

Was This Post Helpful? 0
  • +
  • -

#6 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 36
  • View blog
  • Posts: 710
  • Joined: 09-May 09

Re: File Handling ** Reading **

Posted 25 July 2009 - 04:53 PM

Hey guys the problem has been solved

Solution for this who wish to know


// TODO: Add your control notification handler code here
	this->UpdateData();

	CFile f;

	char strFilter[] = { "txt Files (*.txt)|*.txt|All Files (*.*)|*.*||" };

	CFileDialog FileDlg(TRUE, ".txt", NULL, 0, strFilter);

	if( FileDlg.DoModal() == IDOK )
	{
		f.Open(FileDlg.GetFileName(), CFile::modeRead);
			
		CArchive ar(&f, CArchive::load);

		CString movieNo;
	
		ar >> movieNo;	
		
		movieNoEditBox.SetWindowText(movieNo);
		ar.Close();
	}
	else
		return;

	f.Close();
	this->UpdateData(FALSE);



The reason why this code didnt work before was the txt file was not ARCHIVED.

I had created the file myself in the folder (wrong)

i had the complier create the file in the folder and when i loaded the file it open with no problems :)

no more ACCESS DENIED or unamed file had reached end of file

Hope this Helps
Kevin
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1