LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Mobile
User Name
Password
Linux - Mobile This forum is for the discussion of all topics relating to Mobile Linux. This includes Android, Tizen, LiMo, Maemo, MeeGo, Openmoko, Ubuntu Mobile, WebOS, Firefox OS, Open Mobile Alliance and other similar projects and products.
A reminder that LQ now has a dedicated Android sister site: AndroidQuestions.org

Notices

Reply
 
Search this Thread
Old 09-23-2012, 05:19 AM   #1
priya sweet
LQ Newbie
 
Registered: Sep 2012
Posts: 9

Rep: Reputation: Disabled
small doubt in filehandling


hello everyone
please help me with the following code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream fin;
fin.open("data", ios:: out);
string str;
cin >> str;
fin << str;
fin.close();
fin.open("data", ios::in);
while(fin) {
fin.get(ch);
cout << ch;
}
return 0;
}

if the input is : priya
the output should be priya
but my code is showing the output as priyaa
please help me to sort out the problem with the above code
 
Old 09-23-2012, 05:51 AM   #2
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Perhaps you mean this:
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    fstream fin;
    fin.open("data", ios:: out);
    string str;
    cin >> str;
    fin << str;
    fin.close();
    fin.open("data", ios::in);
    char ch;
    while (fin.get(ch)) {
        cout << ch;
    }
    fin.close();
    return 0;
}
 
1 members found this post helpful.
Old 09-23-2012, 06:20 AM   #3
priya sweet
LQ Newbie
 
Registered: Sep 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
ohh thank you so much . Now its working properly. But will you please say me what is the difference. And also have a look at this code

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
fstream fin, fin1;
fin.open("copy", ios :: out );

int n;
string str;
cout << "enter no of lines" << endl;
cin >> n;
int i = 0;

do {
getline( cin, str );
fin << str << endl;
i++;
}

while ( i < n );

fin.close();
return 0;
}
In this code i am entering 5 lines but it is taking only 4 lines as input. can u please help me in this case also.what is my wrong here??

Last edited by priya sweet; 09-23-2012 at 06:23 AM.
 
Old 09-23-2012, 08:05 AM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
I'm not sure why that happens but as a workaround, you could flush input with ignore() before reading again:
Code:
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
	fstream fin, fout;

	fout.open("copy", ios::out);

	int n;
	string str;

	cout << "enter no of lines" << endl;

	cin >> n;
	cin.ignore();

	for (int i = n; i > 0; --i)
	{
		getline(cin, str);
		fout << str << endl;
	}

	fout.close();

	fin.open("copy", ios::in);

	while (cout << fin.rdbuf())
		;

	fin.close();

	return 0;
}
 
1 members found this post helpful.
Old 09-23-2012, 08:56 AM   #5
priya sweet
LQ Newbie
 
Registered: Sep 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
thank you so much. My doubt is cleared.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Small doubt for using wget cia SSH!!! rockf1bull Red Hat 2 10-11-2011 12:06 AM
small doubt in using grep tkmsr Linux - Newbie 2 03-16-2011 06:19 AM
small doubt nolinuxnollife General 6 11-26-2007 11:06 PM
small doubt in k3b neelendhar Fedora 6 09-22-2007 09:51 PM
A Small doubt about IF loop ??? indian Programming 3 02-26-2007 07:24 PM


All times are GMT -5. The time now is 09:25 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration