need help in urgent, why segmentation fault during read file

experts
i got a annoying error message from my program
here is the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main(int argc, char *argv[])
{
   string file = argv[1];
   ifstream fin;
   char *str
   char character;
   int index = 0;
   fin.open(file.c_str());
   if (!fin){
      cout << "cant get " << file << endl;
      exit(0);
   }
   while(!fin.eof()){
      fin.get(character);
      str[index] = character;
      index++;
   }
      str[index] = '\0';
   fin.close();


the input file contains a article which include normal numbers, words, spaces and punctuations.

and it can be compiled, during running, error message shows: Segmentation Fault (core dumped).

cus i wanna input this whole file input a char*
the reason i use char* is that i dont know how long is the article, so i cant declare like char[].

so need help, experts!!
Last edited on
What's str?
Using a char* does not mean unlimited memory. You can use it to decide how much memory you need at run time instead of compile time, but you still need to know it. If you don't know how much you'll need, use a vector or string, ect. which ran be easily resized.
Topic archived. No new replies allowed.