Thread: Function to read an file.

  1. #1
    C_newbie
    Join Date
    Oct 2005
    Location
    Greece
    Posts
    32

    Function to read a file-STRUCT NODE PROBLEM!

    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.
    Last edited by antonis; 10-16-2005 at 04:25 AM.
    AC/DC: Highway to Hell!
    No speed limit, no stop signs, nobody 's gonna slow me down!

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    78
    remove the '&' from the scanf. With scanf you don't take the address when arg 2 is an array. Also in the fopen remove the double quotes and '*' from filename. You need to add something like:

    Code:
    if (fp == NULL) printf("Can't open %s\n", filename);
    to handle errors.

    You also have allocation problems in your list. You need code to allocate a node for each line and set the next/previous pointers each time through. To be clear, you only need one struct list, but you need a node for each line. Take a look at some linked list example code for more help.

    -Rog

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    38
    Your fopen line should be:
    Code:
    fp = fopen(filename, "r");
    and not

    Code:
    fp = fopen("*filename", "r");
    I like to play pocket pool.

  4. #4
    C_newbie
    Join Date
    Oct 2005
    Location
    Greece
    Posts
    32
    Thank you!
    It now looks much-much better.
    Now I will allocate the node for each line!
    AC/DC: Highway to Hell!
    No speed limit, no stop signs, nobody 's gonna slow me down!

  5. #5
    C_newbie
    Join Date
    Oct 2005
    Location
    Greece
    Posts
    32
    I have problems to allocate the data....
    I think ist very complicated for a newby like me.
    So, I want to use a struct node, where very line is a node,
    and a struct list.
    here the function again with some changes:
    Code:
    void ReadFile(struct list *buffer, char *filename)
    {
            int currentpos, size;
            char line[MAXLINE];
            int a, j;
            char *s[size];
    
                    size=0;                                                     //isos fygei!
    
                    FILE *fp;
                    fp=fopen(filename,"r");
                    if (fp==NULL)
                            printf("File %s can't be read!", filename);
                    else
                    {       while ((fgets(line, MAXLINE, fp))!=NULL);
                            {
                                /*PLACE (1)*/
                            }
                    }
    }
    so, I have 3 questions:
    1.What whil I have to insert to PLACE(1) so that the programm stores the text, each line a node pointing to the next line?
    2. Have I to add something in main?
    ---------------------------------------------------------------------------------
    3.(has nothing to do with the other questions, maybe I should post a new thread) I have a programm in C which defines MALLOC ALLOC etc, as better versions of malloc alloc etc. Where in my linux system schould I put it or the .out programm so that I can use MALLOC in my programm instead of malloc?
    AC/DC: Highway to Hell!
    No speed limit, no stop signs, nobody 's gonna slow me down!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. need working read file function
    By keeper in forum C++ Programming
    Replies: 7
    Last Post: 06-08-2006, 10:00 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM