GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 22-Jul-2009, 17:24
Unit1 Unit1 is offline
New Member
 
Join Date: Aug 2006
Posts: 21
Unit1 is an unknown quantity at this point

Error in opening the file


Hi all,

I am a beginner in C++ and I have the following problem:

I try to open the file Test.txt, but it will not open the file.

The Text.txt file is in the same directory where my C++ files resides.

How can I check why the program fails to open the file.

CPP / C++ / C Code:

bool TMainForm::LeesEventLogNieuw()
{

ifstream inputFile;

char myString[50];

	inputFile.open("Test.txt");

	if (!inputFile) {

	ShowMessage("Error in opening the file");
	return 1;

	}

	inputFile.getline(myString, 50, '\n');

	ShowMessage(myString);

return 0;

}


Thanks in advance,

Johan
  #2  
Old 22-Jul-2009, 21:46
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Error in opening the file


How are you running it? IDE or EXE? What compiler? What O/S? Please provide details.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #3  
Old 22-Jul-2009, 22:08
Howard_L Howard_L is online now
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 802
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Error in opening the file


Really , try to provide more next time.
The function looks OK as far as file reading goes.
Did you check file permissions on Test.txt?
  #4  
Old 23-Jul-2009, 02:26
Unit1 Unit1 is offline
New Member
 
Join Date: Aug 2006
Posts: 21
Unit1 is an unknown quantity at this point

Re: Error in opening the file


I use CodeGear RAD Studio as my compiler, so IDE.
OS: Windows XP SP3.
File permissions: How do I check them in Windows XP?


I have the same function working in C, but I would like to change it to C++.


For standard C, see the working code below.

CPP / C++ / C Code:

bool TMainForm::LeesEventLog()
{


   char  aFileName[20];
   char *p, *ch;
   int line_number = 0;

   p = &Regel[0];

   String FileName,FileDir,FileExt,FilePath;

	 MainForm->OpenDialog->Filter = "Energy|*.txt";

		if (MainForm->OpenDialog->Execute())
		  {
			 FileName = ExtractFileName(MainForm->OpenDialog->FileName);
			 FileDir  = ExtractFileDir(MainForm->OpenDialog->FileName);
			 strcpy(aFileName,FileName.c_str()); // Converteer AnsiString naar chars
		  }                                      // "rb" = binairy read, "wb" = binairy write, "ab" = binairy append
												 // "r" = read, "w" = write, "a" = append
	if ((fp = fopen(aFileName, "r")) == NULL)    // fp == NULL;  Cancel button
	   {
		  MessageBox(NULL,"Er is geen file geselecteerd. Er is geannuleerd.",NULL,0);
		  return 0;
	   }

	  p = &Regel[0];

	  while ( !feof(fp) )  {


	  while ( fgets(Regel, sizeof(Regel), fp) != NULL) // ReturnCode == NULL wanneer er en fout ontstaat. The EOF is considered to be an ERROR!

		{

		 while (p != NULL)

			{
				ShowMessage(p);
				p = strchr(p + 1,'\t');

			}

		 }

}

	  fclose(fp);

	  MainForm->UpDateStatusLine(StatusLijnRechts,FileName + "  data file geladen !!!");

return true;

}

  #5  
Old 23-Jul-2009, 11:02
Howard_L Howard_L is online now
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 802
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Error in opening the file


dude, that's not standard C. And is is more involved that the first one you posted.
You should be simplifing your code until you can read the file properly , then start building it up.
Yes in XP I don't think file permissions are an issue. In linux it definitely can be.
If you are running in an IDE , the txt file may not be in the same directory that you are executing the program from.
Who knows , but In that case you might try the full path for the filename:
c:\this\that\theother\Test.txt

Here is an adaptation of you first code to try.
It works for me in linux and Xp. I use the gnu gcc compiler on the command line for each:
CPP / C++ / C Code:
#include <iostream>
#include <fstream>

using namespace std;

bool LeesEventLogNieuw()  // I just change this to standalone function
{
  ifstream inputFile;
  char myString[50];

  inputFile.open("Test.txt");

  if (!inputFile)
  {
    // ShowMessage("Error in opening the file"); //didn't supply this function
    cout << "Error in opening the file" << endl;
    return 1;
  }

  inputFile.getline(myString, 50, '\n');

  //ShowMessage(myString);  //You did not supply this function
  cout << "myString is: <" << myString << endl;

  inputFile.close();   // you should always close the file when done with it
  return 0;
}

int main(void)
{
  cout << "LeesEventLogNieuw() returns: " << LeesEventLogNieuw() << endl;

  return 0;
}
Code:
I created a file like this in the SAME DIRECTORY that I'm running the program from (pwd) : cat > Test.txt We hold these truths to be self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty and the pursuit of Happiness. EOF And I compile and run: forums> g++ -Wall -W -pedantic 090722_file_func-1.cpp -o 090722_file_func-1 forums> ./090722_file_func-1 myString is: <We hold these truths to be self-evident, that all LeesEventLogNieuw() returns: 0 --- OK so that printed string is: We hold these truths to be self-evident, that all 12345678901234567890123456789012345678901234567890 10 20 30 40 50 49 characters long , looks right to me...
See if you can get that to work.
  #6  
Old 25-Jul-2009, 15:17
Unit1 Unit1 is offline
New Member
 
Join Date: Aug 2006
Posts: 21
Unit1 is an unknown quantity at this point

Re: Error in opening the file


Thanks Howard_L,

It works now.

The Text.txt file was in the same directory as where I execute the program from, but there was something wrong with the path.

Now, I navigate to the directory where the file is located, and now it works fine.

CPP / C++ / C Code:
MainForm->OpenDialog->Filter = "txt|*.txt";

	if (MainForm->OpenDialog->Execute())  {

		FileName = ExtractFileName(MainForm->OpenDialog->FileName);
		strcpy(aFileName,FileName.c_str());

	}

	inputFile.open(aFileName);

Thanks for your help.

Johan
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
After execution - Error cannot locate /Skin File? WSCH C++ Forum 1 05-Mar-2005 21:03
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 11:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 12:28
CD Buring Failed skanth2000 Computer Hardware Forum 1 15-Nov-2003 04:52

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:22.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.