Solved

part of function not working...open txt file

Posted on 2004-09-29
13
212 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
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!

 
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
 

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

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

713 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