Need help with file handeling
Oct 17, 2013 at 7:38pm UTC
HI,
I tried to make a program with some c style syntax in c++ but i am getting a problem. When I make a new file it dose not let me enter text.Thanks in advance.
Here's the code :
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
#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <fstream>
#include <string>
using namespace std;
class file{
public :
void file_check(){
ifstream ifile;
ifile.open("D:\\1text.txt" );
if (!ifile)
{system("cls" );
printf("ERROR \n FILE NOT FOUND.....\n" );
}
else
{system("cls" );
printf("File Found.....\n" );
string word;
ifile>>word;
printf("%s" ,word);
}
}
void file_make(){
ofstream ofile;
char word[100];
system("cls" );
ofile.open("D:\\1text.txt" );
cout<<"Enter text for your new file\n" ;
cin.getline(word,100);
printf("Saving text in new file.....\n" );
ofile<<word;
}
};
void main(){
int ans;
file ifile;
cout<<"What do you want to do :\n" ;
cout<<"1)Check File\n2)Make File\n3)exit" <<endl;
cin>>ans;
if (ans==1){ifile.file_check();}
else if (ans==2){ifile.file_make();}
else {exit(0);}
//---------------------------------------------------------------------------------
system("pause" );
}
Oct 17, 2013 at 8:57pm UTC
a) cstdio and stdio.h both isnt neccesary
b) int main not void
c) try closing the file
Oct 17, 2013 at 10:02pm UTC
It still dose not work
Oct 17, 2013 at 11:40pm UTC
what error are you getting?
Oct 18, 2013 at 9:40am UTC
I am not getting any errors but when i want to make a new file it doese not let me enter text for new file
Oct 18, 2013 at 9:57am UTC
Topic archived. No new replies allowed.