![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2006
Location: India
Posts: 25
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#3 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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; |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Feb 2006
Location: India
Posts: 25
Rep Power: 0
![]() |
Yes!!thx Ooble bt I was just trying to find out what was wrong with this!!
thx anyways!! |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|