Write the contents of a file to a linked list

Source: Internet
Author: User

In school, learned a little C, but the level is very low, now learn to feel very laborious, but every day to learn a little, or very happy. Programming this thing, write it, think about it.

All of the following code is written in the Vim editor in the Linux environment and debugged through GCC.

The idea of writing the contents of a file into a linked list:

(1) First we need to build a file, in the example I used Stu_data.txt, create a file *fp pointer to the file

(2) We know to write the contents of the file to the list, in fact very similar to our usual, the standard input and output to the linked list. So as long as in the operation of the document, complete the linked list of the building

Stand on the good.

Realize:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

struct STU

{

int data;

struct stu* next;

};

typedef struct STU Snode;

typedef struct STU * SLINK;

Slink head = NULL;

Slink Receive_txt ()

{

FILE *FP;

fp = fopen ("Stu_data.txt", "R");

if (fp = NULL)

{

printf ("Open error.../n");

Exit (0);

}

Rewind (FP);

Slink h = NULL;

Slink r = NULL;

Slink s =null;

while (FGETC (FP)!=eof)

{

s = malloc (sizeof (Snode);

FSCANF (FP, "%d", s->data);

if (H ==null)

{

h = s;

R = S;

}

Else

{

R->next = s;

R = S;

}

R->next = NULL;

}

return h;

}

void display (Slink head)

{

Slink p = head;

while (P!=null)

{

printf ("%d", p->data);

p = p->next;

}

}

void Main ()

{

Head = Receive_txt ();

Display (head);

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.