Not able to open file in C++

Discussion in 'C++' started by somenath, Jan 16, 2013.

  1. somenath

    somenath Guest

    I have the following simple program in C++. But it is not working

    enter code here

    #include<iostream>
    #include<fstream>
    #include <ios>

    using namespace std;

    int main (void )
    {
    fstream inOut("test.txt",ios_base::app | ios_base::in );
    int cnt = 0;
    char ch;
    if (inOut.fail()) {
    cout<<"not able to open "<<endl;
    inOut.clear();
    return -1;
    }
    inOut.seekg(0,ios_base::beg);
    while ( inOut.get(ch ) ) {
    cout.put(ch);
    cnt += 1;
    if ( ch == '\n' ) {
    inOut<<cnt << ' ';

    }
    }
    inOut<<cnt <<endl;
    cout <<" [ " <<cnt<<" ] "<<endl;
    return 0;
    }

    enter code here

    When I ran this program . It produce output as follows

    /a.out
    not able to open

    Can you please help me by letting me know why the program is failing?
    I have the test.txt in same directory with the following containt and
    permisson


    cat test.txt
    Hello World

    File permission
    =======================
    -rwxrwxrwx 1 root root 12 Jan 16 09:41 test.txt
    somenath, Jan 16, 2013
    #1
    1. Advertising

  2. somenath

    somenath Guest

    I have removed the unwanted sentences from the earlier post.

    I have the following simple program in C++. But it is not working



    #include<iostream>
    #include<fstream>
    #include <ios>


    using namespace std;


    int main (void )
    {
    fstream inOut("test.txt",ios_base::app | ios_base::in );
    int cnt = 0;
    char ch;
    if (inOut.fail()) {
    cout<<"not able to open "<<endl;
    inOut.clear();
    return -1;
    }
    inOut.seekg(0,ios_base::beg);
    while ( inOut.get(ch ) ) {
    cout.put(ch);
    cnt += 1;
    if ( ch == '\n' ) {
    inOut<<cnt << ' ';


    }
    }
    inOut<<cnt <<endl;
    cout <<" [ " <<cnt<<" ] "<<endl;
    return 0;



    }



    When I ran this program . It produce output as follows


    /a.out
    not able to open


    Can you please help me by letting me know why the program is failing?
    I have the test.txt in same directory with the following contain and
    permission


    cat test.txt
    Hello World


    File permission
    =======================
    -rwxrwxrwx 1 root root 12 Jan 16 09:41 test.txt
    somenath, Jan 16, 2013
    #2
    1. Advertising

  3. somenath

    Ian Collins Guest

    somenath wrote:
    > I have removed the unwanted sentences from the earlier post.
    >
    > I have the following simple program in C++. But it is not working
    >
    >
    >
    > #include<iostream>
    > #include<fstream>
    > #include <ios>
    >
    >
    > using namespace std;
    >
    >
    > int main (void )
    > {
    > fstream inOut("test.txt",ios_base::app | ios_base::in );
    > int cnt = 0;
    > char ch;
    > if (inOut.fail()) {
    > cout<<"not able to open "<<endl;
    > inOut.clear();
    > return -1;


    It's better to use EXIT_FAILURE here.

    > }
    > inOut.seekg(0,ios_base::beg);
    > while ( inOut.get(ch ) ) {
    > cout.put(ch);
    > cnt += 1;
    > if ( ch == '\n' ) {
    > inOut<<cnt << ' ';
    > }
    > }
    > inOut<<cnt <<endl;
    > cout <<" [ " <<cnt<<" ] "<<endl;
    > return 0;
    > }
    >
    > When I ran this program . It produce output as follows
    >
    > /a.out
    > not able to open
    >
    > Can you please help me by letting me know why the program is failing?
    > I have the test.txt in same directory with the following contain and
    > permission
    >
    > cat test.txt
    > Hello World
    >
    > File permission
    > =======================
    > -rwxrwxrwx 1 root root 12 Jan 16 09:41 test.txt


    Your code should work, but why is the file owned by root?

    --
    Ian Collins
    Ian Collins, Jan 16, 2013
    #3
  4. somenath

    somenath Guest

    On Wednesday, January 16, 2013 10:10:03 AM UTC+5:30, Ian Collins wrote:
    > somenath wrote:
    >
    > > I have removed the unwanted sentences from the earlier post.

    >
    > >

    >
    > > I have the following simple program in C++. But it is not working

    >
    > >

    >
    > >

    >
    > >

    >
    > > #include<iostream>

    >
    > > #include<fstream>

    >
    > > #include <ios>

    >
    > >

    >
    > >

    >
    > > using namespace std;

    >
    > >

    >
    > >

    >
    > > int main (void )

    >
    > > {

    >
    > > fstream inOut("test.txt",ios_base::app | ios_base::in );

    >
    > > int cnt = 0;

    >
    > > char ch;

    >
    > > if (inOut.fail()) {

    >
    > > cout<<"not able to open "<<endl;

    >
    > > inOut.clear();

    >
    > > return -1;

    >
    >
    >
    > It's better to use EXIT_FAILURE here.
    >
    >
    >
    > > }

    >
    > > inOut.seekg(0,ios_base::beg);

    >
    > > while ( inOut.get(ch ) ) {

    >
    > > cout.put(ch);

    >
    > > cnt += 1;

    >
    > > if ( ch == '\n' ) {

    >
    > > inOut<<cnt << ' ';

    >
    > > }

    >
    > > }

    >
    > > inOut<<cnt <<endl;

    >
    > > cout <<" [ " <<cnt<<" ] "<<endl;

    >
    > > return 0;

    >
    > > }

    >
    > >

    >
    > > When I ran this program . It produce output as follows

    >
    > >

    >
    > > /a.out

    >
    > > not able to open

    >
    > >

    >
    > > Can you please help me by letting me know why the program is failing?

    >
    > > I have the test.txt in same directory with the following contain and

    >
    > > permission

    >
    > >

    >
    > > cat test.txt

    >
    > > Hello World

    >
    > >

    >
    > > File permission

    >
    > > =======================

    >
    > > -rwxrwxrwx 1 root root 12 Jan 16 09:41 test.txt

    >
    >
    >
    > Your code should work, but why is the file owned by root?

    I have logged in as root and then created the file ( test.txt)
    somenath, Jan 16, 2013
    #4
  5. somenath

    somenath Guest

    On Wednesday, January 16, 2013 12:12:21 PM UTC+5:30, somenath wrote:
    > On Wednesday, January 16, 2013 10:10:03 AM UTC+5:30, Ian Collins wrote:
    >
    > > somenath wrote:

    >
    > >

    >
    > > > I have removed the unwanted sentences from the earlier post.

    >
    > >

    >
    > > >

    >
    > >

    >
    > > > I have the following simple program in C++. But it is not working

    >
    > >

    >
    > > >

    >
    > >

    >
    > > >

    >
    > >

    >
    > > >

    >
    > >

    >
    > > > #include<iostream>

    >
    > >

    >
    > > > #include<fstream>

    >
    > >

    >
    > > > #include <ios>

    >
    > >

    >
    > > >

    >
    > >

    >
    > > >

    >
    > >

    >
    > > > using namespace std;

    >
    > >

    >
    > > >

    >
    > >

    >
    > > >

    >
    > >

    >
    > > > int main (void )

    >
    > >

    >
    > > > {

    >
    > >

    >
    > > > fstream inOut("test.txt",ios_base::app | ios_base::in );

    >
    > >

    >
    > > > int cnt = 0;

    >
    > >

    >
    > > > char ch;

    >
    > >

    >
    > > > if (inOut.fail()) {

    >
    > >

    >
    > > > cout<<"not able to open "<<endl;

    >
    > >

    >
    > > > inOut.clear();

    >
    > >

    >
    > > > return -1;

    >
    > >

    >
    > >

    >
    > >

    >
    > > It's better to use EXIT_FAILURE here.

    >
    > >

    >
    > >

    >
    > >

    >
    > > > }

    >
    > >

    >
    > > > inOut.seekg(0,ios_base::beg);

    >
    > >

    >
    > > > while ( inOut.get(ch ) ) {

    >
    > >

    >
    > > > cout.put(ch);

    >
    > >

    >
    > > > cnt += 1;

    >
    > >

    >
    > > > if ( ch == '\n' ) {

    >
    > >

    >
    > > > inOut<<cnt << ' ';

    >
    > >

    >
    > > > }

    >
    > >

    >
    > > > }

    >
    > >

    >
    > > > inOut<<cnt <<endl;

    >
    > >

    >
    > > > cout <<" [ " <<cnt<<" ] "<<endl;

    >
    > >

    >
    > > > return 0;

    >
    > >

    >
    > > > }

    >
    > >

    >
    > > >

    >
    > >

    >
    > > > When I ran this program . It produce output as follows

    >
    > >

    >
    > > >

    >
    > >

    >
    > > > /a.out

    >
    > >

    >
    > > > not able to open

    >
    > >

    >
    > > >

    >
    > >

    >
    > > > Can you please help me by letting me know why the program is failing?

    >
    > >

    >
    > > > I have the test.txt in same directory with the following contain and

    >
    > >

    >
    > > > permission

    >
    > >

    >
    > > >

    >
    > >

    >
    > > > cat test.txt

    >
    > >

    >
    > > > Hello World

    >
    > >

    >
    > > >

    >
    > >

    >
    > > > File permission

    >
    > >

    >
    > > > =======================

    >
    > >

    >
    > > > -rwxrwxrwx 1 root root 12 Jan 16 09:41 test.txt

    >
    > >

    >
    > >

    >
    > >

    >
    > > Your code should work, but why is the file owned by root?

    >
    > I have logged in as root and then created the file ( test.txt)


    Also if I change the code as follows

    fstream inOut("test.txt", ios_base::in );
    i.e remove the "ios_base::app" then the program able to open the file.
    The output is now

    Hello World
    [ 12 ]
    I understand there is still some logical problem with the code but I am not able to figure out why it is not able to open the file in append mode ( ios_base::app)?
    somenath, Jan 16, 2013
    #5
  6. somenath

    Ian Collins Guest

    somenath wrote:
    > On Wednesday, January 16, 2013 12:12:21 PM UTC+5:30, somenath wrote:
    >> On Wednesday, January 16, 2013 10:10:03 AM UTC+5:30, Ian Collins
    >> wrote:


    Please clean up the hopeless mess that crap google interface makes of
    your quotes!

    >>> Your code should work, but why is the file owned by root?

    >>
    >> I have logged in as root and then created the file ( test.txt)


    Why? never log in as root unless you really have to.

    > Also if I change the code as follows
    >
    > fstream inOut("test.txt", ios_base::in ); i.e remove the
    > "ios_base::app" then the program able to open the file. The output is
    > now
    >
    > Hello World [ 12 ] I understand there is still some logical problem
    > with the code but I am not able to figure out why it is not able to
    > open the file in append mode ( ios_base::app)?


    There is nothing wrong with the code. Try doing things as a normal user.

    --
    Ian Collins
    Ian Collins, Jan 16, 2013
    #6
  7. In article <>,
    somenath <> wrote:
    >On Wednesday, January 16, 2013 12:12:21 PM UTC+5:30, somenath wrote:
    >> > > I have the following simple program in C++. But it is not working
    >> > > fstream inOut("test.txt",ios_base::app | ios_base::in );

    >
    >Also if I change the code as follows
    >
    >fstream inOut("test.txt", ios_base::in );
    >i.e remove the "ios_base::app" then the program able to open the file.


    ios_base::app | ios_base::in is invalid. ios_base::app must be combined
    with ios_base::eek:ut. See for example
    http://stdcxx.apache.org/doc/stdlibug/30-3.html

    fstream inOut("test.txt", ios_base::in | ios_base::eek:ut | ios_base::app )
    should work.

    Mike.
    Miquel van Smoorenburg, Jan 16, 2013
    #7
  8. somenath

    Jorgen Grahn Guest

    On Wed, 2013-01-16, Ian Collins wrote:
    > somenath wrote:

    ....
    >> int main (void )
    >> {

    ....
    >> return -1;

    >
    > It's better to use EXIT_FAILURE here.


    And EXIT_FAILURE is by the way not -1, not on any platform I've seen
    anyway. You tend to use 0 for "success" and small, positive integers
    like 1 for errors.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Jan 16, 2013
    #8
  9. somenath

    Greg Martin Guest

    On 13-01-17 02:21 AM, Juha Nieminen wrote:
    > Jorgen Grahn <> wrote:
    >> On Wed, 2013-01-16, Ian Collins wrote:
    >>> somenath wrote:

    >> ...
    >>>> int main (void )
    >>>> {

    >> ...
    >>>> return -1;
    >>>
    >>> It's better to use EXIT_FAILURE here.

    >>
    >> And EXIT_FAILURE is by the way not -1, not on any platform I've seen
    >> anyway. You tend to use 0 for "success" and small, positive integers
    >> like 1 for errors.

    >
    > I thought returning 0 from main() *is* the standard value for success
    > and a non-zero value for failure. After all, said return value can be
    > used to return *different* error codes to the calling system this way.
    >
    > If all you have is EXIT_SUCCESS and EXIT_FAILURE, then you can't use
    > different error codes, limiting your possibilities.
    >
    > --- news://freenews.netfront.net/ - complaints: ---
    >


    Choosing your own values is system dependant. The macros EXIT_FAILURE
    and EXIT_SUCCESS will work on any platform with a valid C compiler.

    If you know the expectations of the system your program will run on then
    Bob's your uncle.

    On systems running a bourne shell the return value is place in the
    variable $? On this system return values are 8 bit positive values.

    greg@satellite:~/dev/tests/c$ uname -vosr
    Linux 3.2.0-35-generic-pae #55-Ubuntu SMP Wed Dec 5 18:04:39 UTC 2012
    GNU/Linux
    greg@satellite:~/dev/tests/c$ ./rvals -f; echo $?
    Exiting 1
    1
    greg@satellite:~/dev/tests/c$ ./rvals -s; echo $?
    Exiting 0
    0
    greg@satellite:~/dev/tests/c$ ./rvals -1; echo $?
    Returning -1
    255
    greg@satellite:~/dev/tests/c$ ./rvals -2; echo $?
    Returning -2
    254
    greg@satellite:~/dev/tests/c$ ./rvals 255; echo $?
    Returning 255
    255
    greg@satellite:~/dev/tests/c$ ./rvals 256; echo $?
    Returning 256
    0
    greg@satellite:~/dev/tests/c$ ./rvals -s && echo "SUCCESS"
    Exiting 0
    SUCCESS
    greg@satellite:~/dev/tests/c$ ./rvals -f && echo "SUCCESS"
    Exiting 1

    //////////////////////////////////////////////////////////////////

    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>

    int main (int argc, char* argv[]) {

    if (strcmp (argv[1], "-f") == 0) {
    printf ("Exiting %d\n", EXIT_FAILURE);
    exit (EXIT_FAILURE);
    }

    if (strcmp (argv[1], "-s") == 0) {
    printf ("Exiting %d\n", EXIT_SUCCESS);
    exit (EXIT_SUCCESS);
    }

    printf ("Returning %s\n", argv[1]);

    return atoi (argv[1]);

    }
    Greg Martin, Jan 17, 2013
    #9
  10. On 2013-01-16 00:19, Miquel van Smoorenburg wrote:
    > In article <>,
    > somenath <> wrote:
    >> On Wednesday, January 16, 2013 12:12:21 PM UTC+5:30, somenath wrote:
    >>>>> I have the following simple program in C++. But it is not working
    >>>>> fstream inOut("test.txt",ios_base::app | ios_base::in );

    >>
    >> Also if I change the code as follows
    >>
    >> fstream inOut("test.txt", ios_base::in );
    >> i.e remove the "ios_base::app" then the program able to open the file.

    >
    > ios_base::app | ios_base::in is invalid. ios_base::app must be combined
    > with ios_base::eek:ut. See for example
    > http://stdcxx.apache.org/doc/stdlibug/30-3.html
    >
    > fstream inOut("test.txt", ios_base::in | ios_base::eek:ut | ios_base::app )
    > should work.


    In fact, the C++ standard library issue #596 makes 'ios_base::app |
    ios_base::in' valid and equivalent to the "a+" mode in stdio.
    http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#596

    According to C99, "a+" means
    "append; open or create text file for update, writing at end-of-file".

    --
    Seungbeom Kim
    Seungbeom Kim, Jan 22, 2013
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nishant
    Replies:
    0
    Views:
    310
    Nishant
    Mar 28, 2008
  2. Jayneil Dalal
    Replies:
    3
    Views:
    447
    Tom Zych
    Mar 1, 2011
  3. Jack

    Not able to open ASP file

    Jack, Dec 23, 2003, in forum: ASP .Net Security
    Replies:
    0
    Views:
    103
  4. Jack

    Not able to open ASP file

    Jack, Dec 23, 2003, in forum: ASP .Net Web Services
    Replies:
    2
    Views:
    111
    Rajagopal Pasupuleti
    Dec 30, 2003
  5. sed_y
    Replies:
    0
    Views:
    1,013
    sed_y
    Feb 15, 2012
Loading...

Share This Page