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