• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

part of function not working...open txt file

Hi,
Back yet again.

This function stops at opening the txt file and say it can't open the file.
I have the const header file included  and the txt file is included in the project...any tips?

Kaz
//rewrite one binary record
void reWriteBinaryRecord(void)
{
      fstream  outFile;
      ifstream inFile;
      Sighting sighting;
      int count = 0, i;
      
      BirdRecord birds[256];
      
      inFile.open(outFileName, ios::in | ios::binary);

            if(inFile.fail() )  //if file opening fails.
      {
            cerr<<"File " << outFileName << " Could Not Be Opened ";
            getch();
            exit(1);
      }

      while(!inFile.eof())
      {

            inFile.read(reinterpret_cast<char*>(&birds[count]),
                  sizeof(BirdRecord));
            count++;
      }

      inFile.close();
      
      inFile.open(IN_FILE_NAME, ios::in);  STOPS HERE AND CANT OPEN FILE

      if(inFile.fail() )
      {
            cerr<<"File " << IN_FILE_NAME << " Could Not Be Opened ";
            getch();
            exit(1);
      }

      while(inFile.is_open())
      {
            if(readSighting(sighting,inFile) != NULL)
            {
                  for(i = 0; i < count; i++)
                  {
                        if(strcmp(birds[i].type,sighting.aType.c_str())
                              == 0 && strcmp(birds[i].location,
                              sighting.aLocation.c_str()) == 0)
                        {
                              birds[i].numberOfBirds += sighting.number;
                              birds[i].latestDate     = sighting.aDate;
                              i = count + 1;
                        }
                  }
                  
                  if(i != count + 1)
                  {
                        birds[count].firstDate  = sighting.aDate;
                        birds[count].latestDate = sighting.aDate;
                        strcpy(birds[count].type,sighting.aType.c_str());
                        strcpy(birds[count].location,sighting.aLocation.c_str());
                        birds[count].numberOfBirds = sighting.number;
                        count++;
                  }
            }
            
            else {
                  inFile.close();
            }
      }
      
      outFile.open(outFileName, ios::out | ios::trunc | ios::binary);
      
      if(outFile.fail() )  //if file opening fails.
      {
            cerr<<"File " << outFileName << " Could Not Be Opened ";
            getch();
            exit(1);
      }
      
      for(i = 0; i < count; i++)
      {
            outFile.write(reinterpret_cast<char*>(&birds[i]),sizeof(BirdRecord));
      }
      
      outFile.close();
}
0
KazIT
Asked:
KazIT
2 Solutions
 
KazITAuthor Commented:
const char IN_FILE_NAME[] = "excursion.txt";

0
 
mrwad99Commented:
>> IN_FILE_NAME

Is this a #define ?  If so try opening the file just using a string as the filename without a pre-processor directive.
0
 
mrwad99Commented:
Ah.  No.  Sorry :)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mrwad99Commented:
Try using a different ifstream object for the one of two file openings; you are using the same one twice.
0
 
KazITAuthor Commented:
i have tried not using it at all and it still gives the same response

      inFile.open(IN_FILE_NAME);
0
 
KazITAuthor Commented:
what is frustrating is I have used the exact same coding in another function and it opens the file fine.??????????
0
 
KazITAuthor Commented:
once the console window is closed that will close any opened file from that session won't it?
0
 
SteHCommented:
How do you know that the file was not opened. Because the inFile.failed () returns true?
See my comments in here.

     while(!inFile.eof())
     {

          inFile.read(reinterpret_cast<char*>(&birds[count]),
               sizeof(BirdRecord));
          count++;
     }
     inFile.close(); // inFile.eof is set here. (fail and bad most likely as well)
     inFile.open(IN_FILE_NAME, ios::in);  // state of inFile::eof, ::bad ::fail remains on a call to open.
                                                          // The same is true for C FILE*

     // add the line
     inFile.clear (); // either here or just before the call to open.

     if(inFile.fail() )
     {
 
0
 
KazITAuthor Commented:
i put inFile.clear(); before the call to open and now it stops at the step before
      while(inFile.is_open())
0
 
KazITAuthor Commented:
I changed
while(inFile.is_open())
to
      while(!inFile.eof() && !inFile.fail())
and the function seems to execute...but now when I go to read the updated binary file this is my console output
          Bird Watching Records Program
        ---------------------------------------------------

        1. View sightings from the last excursion

        2. Update the records with the latest sightings

        3. View sightings of a particular type of bird

        4. View sightings of birds in a particular location

        5. View all sightings

        0. Exit

        ---------------------------------------------------

Please enter your selection:  5
   Sightings
   First        Latest          Bird Type       Location  Number of Birds
------------------------------------------------------------------------------

00/00/0000      00/00/0000      &#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;
&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;&#9568;        -858993460
Press any key to continue

before updating the binary file read fine
0
 
KazITAuthor Commented:
luckily I still have a copy of the original binary file. so there must be something wrong with my udating of the records from the txt file to the binary??
0
 
KazITAuthor Commented:
Can anyone offer any assistance ?

Kaz
0
 
smpoojaryCommented:
hi KazIT,

       Following code is working. But you have to modify it for your purpose.

//rewrite one binary record
#include <iostream.h>
#include <fstream.h>
#include <conio.h>
#include <process.h>
typedef struct
{
      char data[100];

}BirdRecord;
#define outFileName "out.txt"
#define IN_FILE_NAME "in.txt"
int main()
{
      void reWriteBinaryRecord(void);
      reWriteBinaryRecord();
      return 0;
}

void reWriteBinaryRecord(void)
{
     fstream  outFile;
     ifstream inFile;
     //Sighting sighting;
     int count = 0;
     
     BirdRecord birds[256];

   
     inFile.open(outFileName, ios::in | ios::binary);

     if(inFile.fail() )  //if file opening fails.
     {
          cerr<<"File " << outFileName << " Could Not Be Opened ";
          getch();
          exit(1);
     }

     while(!inFile.eof())
     {
          inFile.read(reinterpret_cast<char*>(&(birds[count])), sizeof(BirdRecord));
          count++;
     }

     inFile.close();
     
     inFile.open(IN_FILE_NAME, ios::in| ios::binary);

     if(inFile.fail() )
     {
          cerr<<"File " << IN_FILE_NAME << " Could Not Be Opened ";
          getch();
          exit(1);
     }

       count = 0;
     while(!inFile.eof())
     {
          inFile.read(reinterpret_cast<char*>(&(birds[count])), sizeof(BirdRecord));
          count++;
     }
     inFile.close();
}

out.txt file containts "fsdfsd asfjdskf asfdjkflsdkf sdfsdfdjs asfdsjfksfj
uoruorueoruwerower erueor orewre rweeore rioweruweioruwer
weoru weo rweuroeuro werower euroweur ioweruewor eoweru ewor
uweior eoeureo wr uweor weoruweoru ewerweurweorur orejroewruewor e
roqwerjweoruweqor weoruweor ueweoruweoru  rueoru we"

in.txt file contains "mahesh gllf elsjd kf asfdjkflsdkf sdfsdfdjs asfdsjfksfj
weoru weo rweuroeuro werower euroweur ioweruewor eoweru ewor
uweior eoeureo wr uweor weoruweoru ewerweurweorur orejroewruewor e
ragu gllf elsjd kf asfdjkflsdkf sdfsdfdjs asfdsjfksfj
weoru weo rweuroeuro werower euroweur ioweruewor eoweru ewor fdffkl
dfjdklk asdfsjl qruoeipf"

   Enjoy
   (Otherwise mail your code(with input file) to smpoojary@yahoo.com)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now