Not able to open file in C++

S

somenath

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
 
S

somenath

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
 
I

Ian Collins

somenath said:
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?
 
S

somenath

somenath said:
I have removed the unwanted sentences from the earlier post.

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





#include <ios>


using namespace std;


int main (void )

fstream inOut("test.txt",ios_base::app | ios_base::in );
int cnt = 0;
if (inOut.fail()) {
cout<<"not able to open "<<endl;

return -1;



It's better to use EXIT_FAILURE here.


}

while ( inOut.get(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


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


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)
 
S

somenath

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 <ios>
using namespace std;
int main (void )
fstream inOut("test.txt",ios_base::app | ios_base::in );
int cnt = 0;
if (inOut.fail()) {
cout<<"not able to open "<<endl;
return -1;
It's better to use EXIT_FAILURE here.
while ( inOut.get(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
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
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)?
 
I

Ian Collins

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

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.
 
M

Miquel van Smoorenburg

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.
 
J

Jorgen Grahn

somenath wrote: ....

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
 
G

Greg Martin

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: (e-mail address removed) ---

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

}
 
S

Seungbeom Kim

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".
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top