Reputation: 862
i am trying to read each line in the file B-small-practice.in into a char array but i am getting segmentation fault.
5
this is a test
foobar
all your base
class
pony along
#include<iostream>
#include<fstream>
using namespace std;
main()
{
ifstream a("B-small-practice.in",ios::in);
int b,i=0;
a>>b;
char c[b][128];
while(a.getline(c[i],128))
{
cout<<c[i];
i++;
}
}
Upvotes: 2
Views: 674
Reputation: 47784
Your b
is 5, still you have several extra blank lines in your input file.
If you increase c
size by doing char c[b*2][128];
,it won't crash.
Else remove extra newlines from your input file.
Upvotes: 1
Reputation: 20063
Since you are not doing any bounds checking the likely cause is you storing data past the end of the array. You can check the bounds in the while
loop with something like the following...
while(i < b && a.getline(c[i],128))
{
cout<<c[i];
i++;
}
Upvotes: 1