Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 14th, 2006, 9:56 AM   #1
sharadpro
Newbie
 
Join Date: Feb 2006
Location: India
Posts: 25
Rep Power: 0 sharadpro is on a distinguished road
Processor Exception Error!!

#include<iostream.h>
#include<iomanip.h>
#include<fstream.h>

int main()
{
 char *buffer1 ,*buffer2;
 int i=0,j=0;
 fstream f;
 char ch;
 f.open("Tele.txt",ios::in);
 buffer1= new char[20];
 buffer2=new char[10];
 while(f.eof()==0)
 {
	 ch = f.get();

	 while(ch!=' ')
	 {

	 buffer1[i]=ch;
	 i++;
	 ch = f.get();

	 }
	 buffer1[i]='\0';
	 i=0;
	 ch = f.get();
	 while(ch!='\n')
	 {
	 buffer2[j++]=ch;
	 ch = f.get();
	}
	 buffer2[j]='\0';
	 j=0;
	 cout<<buffer1<<"\t\t\t"<<buffer2<<endl;

}
return 0;
}


I m getting general processor exception error when i run this prog!!!!how can i solve it?The is meant to read a file say "Tele.txt" which is something like as follows :
//Tele.txt

Abc 23445
sdf 45667
werrt 55555
sharadpro is offline   Reply With Quote
Old May 14th, 2006, 9:58 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Please read the forum's FAQ/Rules, a "How to Post..." thread, and incorporate code tags into your post. Your indenting, presuming you use any, has been lost, at a cost of readability.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old May 14th, 2006, 11:38 AM   #3
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Why're you using the fstream::get() function? You'd be better off using the standard I/O stream functions:

f >> buffer1 >> buffer2;
cout << buffer1 << "\t\t\t" << buffer2 << endl;
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 14th, 2006, 12:20 PM   #4
sharadpro
Newbie
 
Join Date: Feb 2006
Location: India
Posts: 25
Rep Power: 0 sharadpro is on a distinguished road
Yes!!thx Ooble bt I was just trying to find out what was wrong with this!!
thx anyways!!
sharadpro is offline   Reply With Quote
Old May 14th, 2006, 2:07 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Because you've allocated a buffer 20 characters long, but you're stuffing shit into it until you get a space. Did God tell you that there'll never be more than 20 characters between spaces? If you stash stuff beyond 20 characters (INCLUDING the '\0'), there's gonna be trouble in River City. It might be mild, like, "This acts funny!", and it might cause your system to run off into the weeds and hang itself with a new rope.

Check the various flavors of input available to you. There's very rarely a need to go for a character at a time (on your part -- let the stream do it). Check your functions' returns for error -- you and your users are gonna cause a few crashes. Read, despite your propensity to jump in where crocodiles fear to swim.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old May 15th, 2006, 5:11 AM   #6
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Don't use deprecated headers, use C++ strings and the ifstream constructor.
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
	string buffer1, buffer2;
	ifstream f("Tele.txt");

	while(!f.eof())
	{
		f >> buffer1 >> buffer2;
		cout << buffer1 << "\t\t\t" << buffer2 << endl;
	}
	return 0;
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:11 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC