FILE READ/OPEN ISSUE( UNABLE TO OPEN FILE)

I am having an issue with opening a file using the below code in c++. The code used to work before.
I am running this program in unix (sun c++ v4.2 compilers). The file I am reading currently is now > 2GB. I am able to read files < 2GB using this code. This is a production issue. Using unix cat and copy I am able to access file > 2GB.

This is a production issue I am working on.
Your quick help is greatly appreciated. Thank you.

//*******************************************************
#include
#include
int main()
{
char buffer[500];
ifstream inFile("FILE.TXT", ios::in);
// Check if there was an error opening the file
if (!inFile)
{
cout << "Unable to open the file
";
return 1;
}
while (inFile >> buffer)
cout << buffer << endl;
inFile.close();
return 0;
}

Comments

  • : I am having an issue with opening a file using the below code in c++. The code used to work before.
    : I am running this program in unix (sun c++ v4.2 compilers). The file I am reading currently is now > 2GB. I am able to read files < 2GB using this code. This is a production issue. Using unix cat and copy I am able to access file > 2GB.
    :
    : This is a production issue I am working on.
    : Your quick help is greatly appreciated. Thank you.
    :
    : //*******************************************************
    : #include
    : #include
    : int main()
    : {
    : char buffer[500];
    : ifstream inFile("FILE.TXT", ios::in);
    : // Check if there was an error opening the file
    : if (!inFile)
    : {
    : cout << "Unable to open the file
    ";
    : return 1;
    : }
    : while (inFile >> buffer)
    : cout << buffer << endl;
    : inFile.close();
    : return 0;
    : }
    :
    :

    You are not coding right - it is obvious that you need more practice.
    Here is your code:
    [code]
    #include <iostream.h>
    #include


    void main()
    {
    ifstream file("myFile.exe", ios::nocreate); //ios::nocreate
    // tells the compiler not to
    // create this file if it
    // dosent exist
    char *buffer = new char[2000]; // similar to malloc function
    if(file.fail()) //This function return a true value if this fail
    //cannot be opened - you cant use the ! operator un-
    //less you define it in your class
    {
    file.close(); // close this varible so there won't be any memorey
    // lick.
    delete buffer; // similar to free function
    cout << "Failed openning file" << endl;
    }

    while(!file.eof() && !file.fail()) // file.eof() - returnes true if
    // this is the end of the file
    {
    file.getline(buffer,sizeof(buffer));
    cout << buffer;
    }
    delete buffer;
    file.close(); //close this file
    }
    [/code]

    Hope I helped
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion