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-Sep-2004, 14:40
Dream86 Dream86 is offline
Junior Member
 
Join Date: Jun 2004
Posts: 54
Dream86 is on a distinguished road

Opening a Binary File


Hi,
My program is suppose to scan the directory, and count the number of files in it. The number of files in the direcotry should be appended to the name of the output file which can be selected by the user. After that, it is suppose to read the first 512 bytes of the first file in the direcoty according to a certain format. (the files are image files output of a microscope and they have 512 b header that contains the information about that specific image.) after the program scans and counts the number of directories, the pointer is put at the beginning of the directory, then it skips "." and " .. " and start by reading the byte 16 of the first file. I used seekg to put the pointer at byte 16.
The tellg(), however gives back a value of -1. When i checked to see if the file opens properly or not using fail(), i got the error message, showing that the file has not been opened corectly. How can i know what is going wrong here?

here is the the corresponding code for this part:

CPP / C++ / C Code:
 while ( (ent = readdir(dir)) != NULL)
  {
 
    if ( (strcmp((char*)ent->d_name, ".") != 0) && (strcmp((char*)ent->d_name, "..") != 0) )
    {
  
        ifstream inFile(ent->d_name, ios::in |ios::binary);

        if (inFile.fail())
        {

         //ERR message

         }

         else

         {...}

...
}

Thanks
  #2  
Old 23-Sep-2004, 08:35
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,260
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by Dream86
Hi,
My program is suppose to scan the directory
...

When i checked to see if the file opens properly or not using fail(), i got the error message, showing that the file has not been opened corectly. How can i know what is going wrong here?



Thanks

There is no way that I can tell you why the file failed to open. You can get clues from your own program.

Are you sure that you are trying to open the right file? Put in a line to show the file name. Something like the following:

CPP / C++ / C Code:
  cout << "Trying to open <" << ent->d_name << ">" << endl;
   ifstream inFile(ent->d_name, ios::in |ios::binary);

If the file name is correct, does the file exist in the directory where your executable is?



Regards,

Dave
  #3  
Old 23-Sep-2004, 09:42
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,031
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Hm, I remember this problem from another thread....
I only suggested the fail thing, but then I failed to answer your next question. Unfortunatelly, what I wanted to suggest was something like Dave already did
Print the exact path to your file as you have it in your program (obtained through argv). Maybe it's not what you would expect (I'm thinking something like "c:\bla\bla", and then your filename, instead of "c:\bla\bla\" and then your filename - I'm only suggesting this because it happened to me ;-) )
So, please get back to us when you did it.

Best regards,
Luci
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #4  
Old 23-Sep-2004, 21:14
Dream86 Dream86 is offline
Junior Member
 
Join Date: Jun 2004
Posts: 54
Dream86 is on a distinguished road
I have already tried to print out the name of the file that is supposed to be opened. It seems to be fine. I mean it prints the correct file name.
I do not compeltely understand what you mean by defining the path of the file. The program is suppose to go through the files in a (sub)directory one by one. It asks for the name of the directory, and the direcoty that contains the files is in the same directory as the program.
do i need to specify the path of the directory in the ifstream ? something like :
CPP / C++ / C Code:
ifstream inFile(c:\ent->d_name, ios::in | ios::binary);

Thank you
  #5  
Old 23-Sep-2004, 22:21
Dream86 Dream86 is offline
Junior Member
 
Join Date: Jun 2004
Posts: 54
Dream86 is on a distinguished road
well, i actually tried
CPP / C++ / C Code:
ifstream inFile(c:\ent->d_name, ios::in | ios::binary);
and it did not work.
Since it gives back the name of the file that is supposed to be read correctly, does not that mean the program has already located the file properly?

thanks
  #6  
Old 23-Sep-2004, 22:24
Dream86 Dream86 is offline
Junior Member
 
Join Date: Jun 2004
Posts: 54
Dream86 is on a distinguished road
Quote:
Originally Posted by LuciWiz
Print the exact path to your file as you have it in your program (obtained through argv). Maybe it's not what you would expect (I'm thinking something like "c:\bla\bla", and then your filename, instead of "c:\bla\bla\" and then your filename -Luci

I am sorry but i do not quite follow you here. would you please explain...

thank you
  #7  
Old 24-Sep-2004, 03:12
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,031
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by Dream86
I am sorry but i do not quite follow you here. would you please explain...

thank you

I'm sorry I didn't answer very promptly, but when you posted it was 5-6 A.M. in my country. I was sleeping like a baby

I hope you didn't literrally try this:
CPP / C++ / C Code:
ifstream inFile(c:\ent->d_name, ios::in | ios::binary);

but, rather like this:

CPP / C++ / C Code:
ifstream inFile("c:\\" + ent->d_name, ios::in | ios::binary);

Observe the double "\\"(that's Windows)
Of course, this kind of concatenation will not work under C (you'll get something about adding 2 pointers). It works with VC CStrings, though
You' can do something with std:string and Insert, I don't mean to bore you...
However, since it is in a (sub)directory of your application's directory, you still need something like:

CPP / C++ / C Code:
destFile = directoryName + "\\" +  ent->d_name;
ifstream inFile(destFile, ios::in | ios::binary);

Quote:
Originally Posted by Dream86
Since it gives back the name of the file that is supposed to be read correctly, does not that mean the program has already located the file properly?

Does it print "SubDirectoryName\FileName"?

BTW, I'm away for the weekend (Motivational Weekend ), so I hope you solve this....

Best of luck,
Luci
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #8  
Old 28-Sep-2004, 16:33
Dream86 Dream86 is offline
Junior Member
 
Join Date: Jun 2004
Posts: 54
Dream86 is on a distinguished road
My Problem was resolved

Thank you
 
 

Recent GIDBlogProblems with the Navy (Officers) 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
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
Binary file: hitting eof too soon? Alotau C++ Forum 20 18-May-2004 16:36
Re: Programming Techniques WaltP C Programming Language 0 10-Mar-2004 00:56

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

All times are GMT -6. The time now is 18:29.


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