Solved

part of function not working...open txt file

Posted on 2004-09-29
13
209 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

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…
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 video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

810 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