Help with ifstream function in C ++

I have read many textbooks and cannot find anything in common on this subject.

I wrote the following code to execute one function:

#include <fstream>
#include <string>
#include <iostream>

using namespace std;

ifstream ReadFile( ifstream& openInputFile, string& sLine, int& chgLine );


int main()
{
    int chgLine;
    string MyFile, sLine, 
    sFile = "test.txt";

    cout << "Enter a file: ";
    cin >> MyFile ;

    ifstream openInputFile;


    if ( MyFile != sFile )    // file must be 'hello.cpp' to proceed
    {
       cout << "Error";
       exit(0);
    }
                         // if correct file is entered print the contents of it

        ReadFile( openInputFile, sLine, chgLine );

    system("pause");
    return 0;
}

ifstream ReadFile( ifstream& openInputFile, string& sLine, int& chgLine )
{ 

       while ( getline ( openInputFile, sLine ) )
       {
            if ( sLine.length() == 0 ) continue;  // to proceed

             for ( chgLine = 0; chgLine < sLine.length(); chgLine++ )
             {
                if ( sLine[chgLine] >= 97 && sLine[chgLine] <= 122 || sLine[chgLine] >= 65 && sLine[chgLine] <= 90  )
                {
                     cout << sLine[chgLine];
                }

             }
        }
}

But now I decided to break it all down into three functions that do what I want separately and then call them from the function main().

The first function opens the file:

#include <iostream>
#include <fstream>
using namespace std;

ifstream openInputFile()

{
       // use a pointer..return a pointer
       // ifstream definition is the challenge
  ifstream *fp;
  //fp = new ifstream openInputFile;
  return openInputFile;

}

int main()

{
    cout << "Hello, world!" << endl;

    system("pause");
    return 0;
}

I'm stuck trying to get the pointer back. I do not understand what I am doing wrong. How can I make the last bit of code work? And how to return a pointer with ifstreamif it has a function type?

+3
source share
3 answers

++ - , , . , , RAII.

// FancyFile.h:

class FancyFile
{
    private:
        std::ifstream stream;

    public:
        void DoMagic();

        InputFile(const std::string FilePath)
            { stream.open(FilePath.c_str()); }
        ~InputFile(void)
            { stream.close(); }
};
// FancyFile.cpp:

#include <fstream>
#include <string>

#include "FancyFile.h"

void FancyFile::DoMagic()
{
    //.. your custom file handling code goes here
}
// main:

#include "FancyFile.h"

int main()
{    
    FancyFile myFancyFile("test.txt");

    myFancyFile.DoMagic();

    system("pause");
    return 0;
}
+1

, .

, (RVO). RVO ( ) .

void openInputFile(ifstream& file)
{
    file.open(...);
}

int main()
{
    ifstream file;
    openInputFile(file);

    // Work with file, since file is
    // on the stack you don't have to worry about cleanup/etc
}

, , , . , , , ifstream.

, , , , . , :

    ifstream* Blah();

.

0

The example does not show how you open ifstream with the specified file name?
This example asks the user to enter a file name, and then it can be only one value?

As Doug T. said, it would probably be better from a code example to use a scope to control ifstream.
Anyway, here is an example returning a pointer

ifstream * openInputFile( ) {
  string MyFil;
  string sFile = "test.txt";

  cout << "Enter a file: ";
  cin >> MyFile ;

  if ( MyFile == sFile )   
  {
    ifstream * inputFile = new ifstream( MyFile.c_str() );
    // do some other stuff here?
    return inputFile;
  } else {
    return null;
  }
}

Then the main one may be

int main()
{
  int chgLine;
  string sLine;
  ifstream *inputFile = openInputFile();
  if( NULL == inputFile )
  {
    cout << "Error";
    exit(0);
  }
...
0
source

All Articles