Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Not able to open file in C++ (http://www.velocityreviews.com/forums/t956554-not-able-to-open-file-in-c.html)

somenath 01-16-2013 04:24 AM

Not able to open file in C++
 
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 01-16-2013 04:28 AM

Re: Not able to open file in C++
 
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




Ian Collins 01-16-2013 04:40 AM

Re: Not able to open file in C++
 
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

somenath 01-16-2013 06:42 AM

Re: Not able to open file in C++
 
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 01-16-2013 06:46 AM

Re: Not able to open file in C++
 
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)?


Ian Collins 01-16-2013 07:01 AM

Re: Not able to open file in C++
 
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

Miquel van Smoorenburg 01-16-2013 08:19 AM

Re: Not able to open file in C++
 
In article <f672f5a6-5780-491c-89c4-72814c1d0399@googlegroups.com>,
somenath <somenathpal@gmail.com> 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::out. See for example
http://stdcxx.apache.org/doc/stdlibug/30-3.html

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

Mike.

Jorgen Grahn 01-16-2013 07:56 PM

Re: Not able to open file in C++
 
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 .

Greg Martin 01-17-2013 04:57 PM

Re: Not able to open file in C++
 
On 13-01-17 02:21 AM, Juha Nieminen wrote:
> Jorgen Grahn <grahn+nntp@snipabacken.se> 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: news@netfront.net ---
>


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]);

}




Seungbeom Kim 01-22-2013 02:53 AM

Re: Not able to open file in C++
 
On 2013-01-16 00:19, Miquel van Smoorenburg wrote:
> In article <f672f5a6-5780-491c-89c4-72814c1d0399@googlegroups.com>,
> somenath <somenathpal@gmail.com> 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::out. See for example
> http://stdcxx.apache.org/doc/stdlibug/30-3.html
>
> fstream inOut("test.txt", ios_base::in | ios_base::out | 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/wg...fects.html#596

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

--
Seungbeom Kim


All times are GMT. The time now is 06:51 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57