Solved

part of function not working...open txt file

Posted on 2004-09-29
13
202 Views
Last Modified: 2010-08-05
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
Comment
Question by:KazIT
13 Comments
 

Author Comment

by:KazIT
ID: 12178119
const char IN_FILE_NAME[] = "excursion.txt";

0
 
LVL 19

Expert Comment

by:mrwad99
ID: 12178120
>> 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
 
LVL 19

Expert Comment

by:mrwad99
ID: 12178122
Ah.  No.  Sorry :)
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 12178135
Try using a different ifstream object for the one of two file openings; you are using the same one twice.
0
 

Author Comment

by:KazIT
ID: 12178156
i have tried not using it at all and it still gives the same response

      inFile.open(IN_FILE_NAME);
0
 

Author Comment

by:KazIT
ID: 12178163
what is frustrating is I have used the exact same coding in another function and it opens the file fine.??????????
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:KazIT
ID: 12178224
once the console window is closed that will close any opened file from that session won't it?
0
 
LVL 13

Accepted Solution

by:
SteH earned 250 total points
ID: 12178355
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
 

Author Comment

by:KazIT
ID: 12178410
i put inFile.clear(); before the call to open and now it stops at the step before
      while(inFile.is_open())
0
 

Author Comment

by:KazIT
ID: 12178437
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
 

Author Comment

by:KazIT
ID: 12178462
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
 

Author Comment

by:KazIT
ID: 12185637
Can anyone offer any assistance ?

Kaz
0
 
LVL 4

Assisted Solution

by:smpoojary
smpoojary earned 250 total points
ID: 12197656
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now