file handling in dev c++

does not create the file

Page 1 of 1

9 Replies - 24623 Views - Last Post: 01 January 2007 - 06:10 AM Rate Topic: **--- 4 Votes

#1 get_gaurav2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 31-December 06

file handling in dev c++

Post icon  Posted 31 December 2006 - 05:01 AM

while(ch=='y'&& ch!='n')
			{
						file.open("store.txt",ios::app);
			cout<<"enter the name of the person"<<endl;
			cin>>obj.name;
			cout<<"enter the e-mail of the person"<<endl;
			cin>>obj.email;
			cout<<"enter birth date"<<endl;
			cin>>obj.date;
			file.write((char*)&obj,sizeof(obj));
			file.flush();
		   cout<<"want to add more y/n"<<endl;
		   cin>>ch;
		   }
			file.close();



this code does not create the file "store.txt" anywhere.
what could be the problem????

i am using a dev c++ compiler.

Is This A Good Question/Topic? 0
  • +

Replies To: file handling in dev c++

#2 g3cka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 30-December 06

Re: file handling in dev c++

Posted 31 December 2006 - 05:17 AM

Hi!
First of all, this line is interesting: while(ch=='y'&& ch!='n')
You know, if ch=='y' then it surely won't be 'n' ;) So I guess you don't need to check this: ch!='n'

But this code works fine for me and creates the file in programs directory. Make sure that this code is really executed ;)
Was This Post Helpful? 0
  • +
  • -

#3 get_gaurav2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 31-December 06

Re: file handling in dev c++

Posted 31 December 2006 - 05:30 AM

View Postg3cka, on 31 Dec, 2006 - 05:17 AM, said:

Hi!
First of all, this line is interesting: while(ch=='y'&& ch!='n')
You know, if ch=='y' then it surely won't be 'n' ;) So I guess you don't need to check this: ch!='n'

But this code works fine for me and creates the file in programs directory. Make sure that this code is really executed ;)




ya ok got that
thanx
but the code does not create a file in dev c++
tell me the solution plzzzz
Was This Post Helpful? 0
  • +
  • -

#4 g3cka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 30-December 06

Re: file handling in dev c++

Posted 31 December 2006 - 05:37 AM

Ok, try this:
#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[])
{
	ofstream file;
	file.open("store.txt",ios::app);
	file.close();
	system("PAUSE");
	return EXIT_SUCCESS;
}


If this doesn't create a file in your app's directory, try writing the whole path like "D:/store.txt".

BTW, I'm using the newest Devcpp too.

This post has been edited by g3cka: 31 December 2006 - 05:38 AM

Was This Post Helpful? 0
  • +
  • -

#5 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: file handling in dev c++

Posted 31 December 2006 - 06:49 AM

try changing your open statement to
file.open("store.txt",ios::out | ios::app);


Was This Post Helpful? 0
  • +
  • -

#6 get_gaurav2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 31-December 06

Re: file handling in dev c++

Posted 31 December 2006 - 06:52 AM

View Postget_gaurav2, on 31 Dec, 2006 - 05:30 AM, said:

View Postg3cka, on 31 Dec, 2006 - 05:17 AM, said:

Hi!
First of all, this line is interesting: while(ch=='y'&& ch!='n')
You know, if ch=='y' then it surely won't be 'n' ;) So I guess you don't need to check this: ch!='n'

But this code works fine for me and creates the file in programs directory. Make sure that this code is really executed ;)




ya ok got that
thanx
but the code does not create a file in dev c++
tell me the solution plzzzz





it worked brother
thanx
but could you tell me what have you done in each step
i am not very comfortable with your code.
Was This Post Helpful? 0
  • +
  • -

#7 g3cka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 30-December 06

Re: file handling in dev c++

Posted 31 December 2006 - 07:18 AM

View Postget_gaurav2, on 31 Dec, 2006 - 06:52 AM, said:

but could you tell me what have you done in each step
i am not very comfortable with your code.

I just created a new console project in Dev cpp and then copy-pasted two lines from your code ;)
And I had to write "ofstream file;" at beginning to declare the file instance.
Was This Post Helpful? 0
  • +
  • -

#8 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: file handling in dev c++

Posted 31 December 2006 - 07:37 AM

with fstream open() you have to tell it the name of the file and, using the mode bits, if you are opening it for read and/or write see
http://www.cplusplus...tream/open.html

file.open("store.txt",ios::out | ios::app);


in this case it is opened for write and append, i.e. the mode bits are ios::out and ios::app (the bits are ORed with the | OR operator) which means it is a output file with new output appended onto the end of any existing file (do you want this?)

what you should do is to check if it opens OK, e.g. using the is_open() function
file.open("store.txt",ios::out | ios::app);
if(!file.is_open())
  { cout << " open fail "  << strerror(errno) << endl; cin.get(); exit(1); }
// file open OK carry on


This post has been edited by horace: 31 December 2006 - 07:38 AM

Was This Post Helpful? 0
  • +
  • -

#9 get_gaurav2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 31-December 06

Re: file handling in dev c++

Posted 01 January 2007 - 06:03 AM

View Posthorace, on 31 Dec, 2006 - 07:37 AM, said:

with fstream open() you have to tell it the name of the file and, using the mode bits, if you are opening it for read and/or write see
http://www.cplusplus...tream/open.html

file.open("store.txt",ios::out | ios::app);


in this case it is opened for write and append, i.e. the mode bits are ios::out and ios::app (the bits are ORed with the | OR operator) which means it is a output file with new output appended onto the end of any existing file (do you want this?)

what you should do is to check if it opens OK, e.g. using the is_open() function
file.open("store.txt",ios::out | ios::app);
if(!file.is_open())
  { cout << " open fail "  << strerror(errno) << endl; cin.get(); exit(1); }
// file open OK carry on








thanx for that
but the part i was unclear about was

" using namespace std;

int main(int argc, char *argv[]); "
and also

return exit_success;


what are these
i don't knw how to use them
Was This Post Helpful? 0
  • +
  • -

#10 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: file handling in dev c++

Posted 01 January 2007 - 06:10 AM

View Postget_gaurav2, on 1 Jan, 2007 - 01:03 PM, said:

thanx for that
but the part i was unclear about was

" using namespace std;

int main(int argc, char *argv[]); "
and also

return exit_success;

what are these
i don't knw how to use them

for namespace and return have a look at
http://www.cplusplus..._structure.html

for parameters to main() have a look at
http://www.codersour...parameters.aspx
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1