Hi,
I have to write a programm that reads a text (*.txt) one line a time and stores every line up to MAXLINE characters in a char line[ ***] table and returns a pointer to the first character of the line or NULL if there are no more lines. So I have written the functions ( there have to be some addons and corrections), and the programm, it all gets compiled but I get an segmentation error at the start of the function where everything schould be ok!
here the main()- without some code that has to do with the computer-user dialog:
Code:
#include <stdio.h>
#define MAXLINE 128
#define MAXIMUMLINES 500
struct node {
char *line;
struct node *prev;
struct node *next;
};
struct list {
struct node *head;
struct node *tail;
int size;
int currentpos;
char *current;
};
void ReadFile(struct list *buffer, char *filename);
void WriteFile(struct list *buffer, char *filename);
void PrintLine(struct list *buffer);
void GoToLine(struct list *buffer, int lineNumber);
void DeleteLine(struct list *buffer);
void InsertBefore(struct list *buffer, char *newline);
void InsertAfter(struct list *buffer, char *newline);
main()
{
char *filename;
struct list *buffer = malloc(sizeof (struct list));
buffer->head = 0;
buffer->tail = 0;
buffer->size = 0;
buffer->currentpos = 0;
buffer->current = 0;
scanf("%s", &filename);
ReadFile(buffer, filename);
}
/*And here the FUNCTION: */
void ReadFile(struct list *buffer, char *filename)
{
char *currentpos;
char line[MAXLINE];
int a, j;
char *s;
FILE *fp;
fp=fopen("*filename","r"); // <-----Untill here everything works.
//Then the segmentation fault appears.
if (fgets(line, MAXLINE, fp)!=NULL);
{
for (j=1; j<=MAXIMUMLINES; ++j)
sscanf(line,"%s", line[j]);
}
*currentpos=line[0];
fclose(fp);
}
Whats the mistake. I think it must be something with pointers again, but cant find it.