character strings and reading in a file...

This is a discussion on character strings and reading in a file... within the C Programming forums, part of the General Programming Boards category; if anyone could help me and tell me what's wrong or give me some hints...
I am trying to read ...

This line creates an array of 50 pointers. It does NOT allocate the memory for those pointers. So when you tried to write to them, you accessed protected memory and crashed. Add these lines before your call to readline:

Note that even this is not safe- if there are more than 50 lines in the file, or if there are fewer, but lines are over 255 characters. A safer way would be to make either:

1)Low flexibility: readfile take a max # of lines, and dump any overage. This can be a max defined by readfile, or taken as a parameter.

2)Medium flexibility: Write a function to count the number of lines, and return the size of the file. Make names a char **,. and allocate the needed number of pointers, then allocate each line pointer.

3)Max flexibility: Make 2 new functions. Function 1 gets file length in lines, function 2 gets the length of each line, so no lines over 255 chars are wrapped. Function 3 will write to buffers for each line. Using this method you can minimize memory waste and eliminate any chance of problems. It is the slowest, however.

You never allocated space for filename. Either malloc it some memory, or use an array instead.

Also, its considered very odd and poor style for a function to return dynamically allocated memory to the caller, like you do in parseline. The reason being that people will frequently forget to free the memory, causing a leak. It also makes the cause of error difficult to track down, as you don't know who the owner is. The normal method is to pass memory down to the called functions.

I tried allocating space to both filename and names using Javariel's method....but I still get a segmentation fault. We have just started to learn about pointers in class, so bare with me I am just a beginner.

You still have no space allocated for the file name. Try something like:

char filename[BUFSIZ];

... stuff here ...

printf("Type the name of the file to be sorted:\n");fgets( filename, BUFSIZ, stdin );
filename[strlen(filename)-1] = '\0';

That should do the trick. fgets is a safe way to read strings. It will only read "one less than the size you indicate" characters, and it will add a null to the end. The only thing to remember is that if you have a newline (return) on the end of the string, you'll want to snip it off. That's what the last line of code does.

'stdin' is the standard input file stream, or in other words, the keyboard. Since I used 'f'-gets, I needed to specify a file to read from. On a side note, never just use 'gets'. It's unsafe.

I was wrong...it didn't exactly work. Now I am getting the segmentating fault error again. If anyone could help me again.... It compiles with no errors and no warnings....but I am thinking it is a pointer problem... I am so lost.