LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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, Sailfish OS, Replicant, Ubuntu Touch, webOS, and other similar projects and products.

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,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Mobile

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration