I
Ioannis Vranos
Hi, I am experimenting with fstream type, I have written the following code:
#include <iostream>
#include <fstream>
#include <string>
int main()
{
using namespace std;
char s[200]= {};
fstream file("test.txt");
file<< "This is a test\n"<< 3*9<< "\nThis is the end\n";
file>> s;
if(!file)
cerr<<"Malfunction!\n";
cout<< s<< endl;
file<< 123<< "\n";
}
The file test.txt already exists.
The output of the program to the standard output is:
john@john-desktop:~/Projects/foobar-cpp/src$ ./foobar-cpp
Malfunction!
john@john-desktop:~/Projects/foobar-cpp/src$
The contents of test.txt are:
john@john-desktop:~/Projects/foobar-cpp/src$ cat test.txt
This is a test
27
This is the end
john@john-desktop:~/Projects/foobar-cpp/src$
Question:
Why the file object does not output to the char array?
#include <iostream>
#include <fstream>
#include <string>
int main()
{
using namespace std;
char s[200]= {};
fstream file("test.txt");
file<< "This is a test\n"<< 3*9<< "\nThis is the end\n";
file>> s;
if(!file)
cerr<<"Malfunction!\n";
cout<< s<< endl;
file<< 123<< "\n";
}
The file test.txt already exists.
The output of the program to the standard output is:
john@john-desktop:~/Projects/foobar-cpp/src$ ./foobar-cpp
Malfunction!
john@john-desktop:~/Projects/foobar-cpp/src$
The contents of test.txt are:
john@john-desktop:~/Projects/foobar-cpp/src$ cat test.txt
This is a test
27
This is the end
john@john-desktop:~/Projects/foobar-cpp/src$
Question:
Why the file object does not output to the char array?