Short C++ program segmentation fault. Using GCC 4.4.5 on Ubuntu 32bit
Hi... I'm very excited to post a question, since I always do everything by myself.
I'm reading several lines from a text file using istream::getline(char *, streamsize n). I get the file name from the command line. Here is the code: Code:
#include <iostream> Strange thing is that i swap the lines: char *line = new char; ifstream in (*++argv); And everything works fine. Using GCC 4.4.5 on Ubuntu 32bit Btw: I know assembly language and know what a segmentation fault is |
Hi -
You didn't actually allocate any space for your lines. You just allocated one character; you need a character array: Code:
const int MAX_LINE = 1000; PS: If this is a text file, you might be better off using a C++ "string" type, instead of a low level C "char[]" array. |
Thanks so much for your help, I did not expect such a fast reply.
You are right, I did not consider the fact that I was overwriting memory because I had allocated only one char. Also, why do you say that I'm better off with a string class? Is it because it offers better functionality or because it's more reliable? |
Quote:
Code:
#include <iostream> |
Thanks!!, much to improve my programming!!
|
All times are GMT -5. The time now is 05:12 PM. |