LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-09-2011, 02:38 AM   #1
Puneet Jindal
Member
 
Registered: Aug 2010
Location: India(Punjab)
Posts: 38

Rep: Reputation: 0
Post A simple C++ file handelling


After executing following code when I enter full name(eg:- "fjh jkgfv") with spaces after choosing II option(Write) I am stuck in an infinite loop.I think it is problem related to some input buffer.Can you please explain the error.Thanks.....
CODE

class File
{
private:
struct rec
{
char name[20];
int age;
rec()
{
age = 0;
}
}p;
fstream file;
public:
File()
{
file.open("/home/puneet/Desktop/temp.dat",ios::binary|ios::in|ios:ut|ios::app);
if(!file)
{
cerr<<"Error opening the file"<<endl;
cerr<<"Press any key to exit . . . . . ."<<endl;
getchar();
exit(1);
}
}

~File()
{
file.close();
}

void Read()
{
file.clear();

if(file.eof() == false)
{
file.read((char *)&p,sizeof(p));
cout<<"Record"<<(file.tellg()/sizeof(p))<<":"<<endl;
cout<<"Name "<<p.name<<endl;
cout<<"Age "<<p.age<<endl<<endl;
}
}

void Write()
{
file.clear();

file.seekp(0L,ios::end);

cout<<"Your name "<<endl;
cin.getline(p.name,20); // Expected error in input & stucks in infinite loop

cout<<"Your age"<<endl;
cin>>p.age;

file.write((char *)&p,sizeof(p));
}

void Display()
{
file.clear();
file.seekg(0L,ios::beg);

while(file.read((char *)&p,sizeof(p)))
{
cout<<"Record"<<(file.tellg()/sizeof(p))<<":"<<endl;
cout<<"Name "<<p.name<<endl;
cout<<"Age "<<p.age<<endl<<endl;
}
}
};

int main()
{
File writer;
while(true)
{
system("clear");
cout<<"1.Read next account"<<endl;
cout<<"2.Write an account at the end"<<endl;
cout<<"3.Display all the records"<<endl;
cout<<"4.Exit"<<endl;
int choice = 0;
cin>>choice;

switch(choice)
{
case 1:
system("clear");
writer.Read();
cout<<"\n\nPress any key to exit . . . . . ."<<endl;
getchar();
break;
case 2: // Choose this option
system("clear");
writer.Write();
cout<<"\n\nPress any key to exit . . . . . ."<<endl;
getchar(); // Not executed
break;
case 3:
system("clear");
writer.Display();
cout<<"\n\nPress any key to exit . . . . . ."<<endl;
getchar();
break;
case 4:
return 0;
default:
break;
}
}
return 0;
}

Last edited by Puneet Jindal; 03-09-2011 at 02:41 AM.
 
Old 03-09-2011, 08:25 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Try changing while(file.read((char *)&p,sizeof(p))) to while(file.read((char *)&p,sizeof(p)).good()). The implicit conversion to void* only returns NULL for errors, not EOF.
Kevin Barry
 
Old 03-12-2011, 06:27 AM   #3
Puneet Jindal
Member
 
Registered: Aug 2010
Location: India(Punjab)
Posts: 38

Original Poster
Rep: Reputation: 0
Thanks for this information but problem was in "Write" function.I tried using cin.ignore() after "cin>>choice" in main() function after cout statements.And my problem was solved.Does it mean that after using "cin>>some_var" -> '\n' remains in input stream and "getline" takes this as input(in Write() getline(p.name,20)) and goes to next getline?? Please correct me if I am wrong....
 
Old 03-12-2011, 07:07 AM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by Puneet Jindal View Post
Does it mean that after using "cin>>some_var" -> '\n' remains in input stream and "getline" takes this as input(in Write() getline(p.name,20)) and goes to next getline?? Please correct me if I am wrong....
getline() will read up to the first newline character found; and yes, there is a newline in the input stream under the scenario you described, thus getline() will return immediately.

You can remove the unwanted newline (and any unwanted characters leading up to it) using ignore(). Something like:
Code:
cout << "Enter your age: ";
cin  >> age;

cin.ignore(1024, '\n');   // ignore up to 1024 characters, including the newline, when found.

cout << "Enter your name: ";
cin.getline(name, sizeof(name));
 
1 members found this post helpful.
  


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
A simple Bash file for importing SAMBA users from a file jax8 Linux - Newbie 11 02-09-2009 09:12 AM
Catching SIGSEGV - handelling segmentation faults telg Programming 7 06-02-2008 08:57 PM
simple php script to add line/file to text file dnoy Programming 1 05-21-2008 05:08 PM
Simple file sharing ionmich SUSE / openSUSE 2 02-13-2007 03:59 AM
i need a simple file manager chaits Linux - Software 6 02-10-2004 07:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:59 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