I having 2 problem with file handling .
- File(txt) is not creating using fstream.Here is my code
#include<iostream> #include<conio.h> #include<fstream> void main() {char string[80]; int len; std::cout<<"Enter a string "; std::cin>>string; len=strlen(string); std::fstream file; file.open("RO1.TXT",std::ios::out|std::ios::in); for(int i=0;i<len;i++) file.put(string[i]); file.seekg(0); char ch; while(file) {file.get(ch); std::cout<<ch; } file.close(); getch(); }
The problem there is no file is created of the name RO1.TXT
is my fstream corrupted?
2.If i build using ofstream and ifstream then file is creating but an extra character is being displayed code
#include<iostream>
#include<conio.h>
#include<fstream>
void main()
{std::ofstream file;
char string[80];
int len;
file.open("RO1.TXT");
std::cout<<"Enter a string ";
std::cin>>string;
len=strlen(string);
for(int i=0;i<len;i++)
file.put(string[i]);
file.close();
std::ifstream file1;
file1.open("RO1.TXT");
char ch;
while(file1)
{file1.get(ch);
std::cout<<ch;
}
file1.close();
getch();
}
The problem is extra character is being displayed