Solved

part of function not working...open txt file

Posted on 2004-09-29
13
205 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MSVCR80.dll crash 2 134
Modify static variables within a class in VC++ 3 95
What is sub-make ? 2 39
Copy output image from TWindowsMediaPlayer 6 23
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

920 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

11 Experts available now in Live!

Get 1:1 Help Now