How to realize linked list and File access in C language
Today, I would like to share with you how C language to achieve linked lists and file access related knowledge points, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you can learn something after reading this article, let's take a look at it.
Here is the content of the main function int main (void) {char filename [50]; printf ("How many?:"); scanf ("% d", & n); / * enter the number of students * / printf ("please input filename:"); scanf ("% s", filename); / * the path and name of the input file * / Create () / / call function to create linked list save (filename); / / call function to save to file free (phead); / / release phead memory show (filename); / / call function output file system ("pause"); return 0;} 1. Input data to the linked list.
Create a linked list and enter data into the linked list
The code is as follows:
Typedef struct stu {char name [20]; char adr [20]; int tel; struct stu* pnext;} stu;int n; / / n number of messages stored stu* phead=NULL;//phead is the first address of the linked list void Create () / / establish the linked list {stu* pend,*pnew;// tail node, new node pend=phead = (stu*) malloc (sizeof (stu)) / / allocate memory to the first node printf ("please first input Name, Adress and telephone:\ n"); for (int item0 alternate ipnextnew. / / the pointer of the original tail node to the new node pnew- > pnext=NULL; / / the new node is NULL printf ("NO.%d:", iNode 1); scanf ("% s", pend- > name); scanf ("% s", pend- > adr) Scanf ("% d", & pend- > tel); pend=pnew; / / after assignment, point to tail node} pnew=pnew- > pnext;// to NULL free (pnew); / / release pnew memory} II. Store linked list data in file
File manipulation functions such as fopen, fprintf, fclose are used here.
The code is as follows:
Void save (char * filename) {FILE * wumbpoke / file pointer if ((w = fopen (filename, "wb")) = = NULL) {/ * binary write-only open file * / printf ("cannot open file\ n"); exit (1);} for (int I = 0; I
< n; i++) //链表数据循环输入到文件内 { fprintf(w,"%s ",phead->Name); fprintf (w, "% s", phead- > adr); fprintf (w, "% d", phead- > tel); fprintf (w, "% s", "\ r\ n"); / / newline phead=phead- > pnext;// points to the next node} fclose (w); / / close file} III, output file
First save the contents of the file to the structure array, and then output it to the screen through the array.
The code is as follows:
Void show (char * filename) / / output file {FILE * fp;// file pointer stu info; / / responsible for storing the data in the file, and then output if ((fp = fopen (filename, "rb")) = = NULL) {/ * binary read-only open file * / printf ("cannot open file\ n"); exit (1);} for (int I = 0; I)
< n; i++) { fscanf(fp,"%s",&(info[i].name));//输出数据到数组 fscanf(fp,"%s",&(info[i].adr)); fscanf(fp,"%d",&(info[i].tel)); printf("ssd\n", info[i].name, info[i].adr, info[i].tel); //输出数据到屏幕 } fclose(fp); //关闭文件}完整代码/*此代码为《C语言从入门到精通(第二版)》第十四章(文件)的【例14.7】的改进版*/#include#include#includetypedef struct stu{ char name[20]; char adr[20]; int tel; struct stu* pnext;} stu;int n; //n存着信息条数stu* phead=NULL;//phead为链表首地址 void Create()/*建立链表*/{ stu *pend,*pnew;//尾节点,新节点 pend=phead =(stu*)malloc(sizeof(stu));//分配内存给首节点 printf("please first input Name, Adress and telephone:\n"); for (int i = 0; i < n; i++) { pnew=(stu*)malloc(sizeof(stu)); //分配新节点 pend->Pnext=pnew; / / the pointer of the original tail node to the new node pnew- > pnext=NULL; / / is NULL printf ("NO.%d:", iTun1); scanf ("% s", pend- > name); / / input data is stored in the linked list scanf ("% s", pend- > adr); scanf ("% d", & pend- > tel); pend=pnew / / after assignment, point to tail node} pnew=pnew- > pnext;// and point to NULL free (pnew); / / release pnew memory} void save (char * filename) / * save to file * / {FILE * w if / file pointer if ((w = fopen (filename, "wb")) = = NULL) {/ * binary write-only open file * / printf ("cannot open file\ n"); exit (1) } for (int I = 0; I
< n; i++) //链表数据循环输入到文件里 { fprintf(w,"%s ",phead->Name); / / data is stored in the file fprintf (w, "% s", phead- > adr); fprintf (w, "% d", phead- > tel); fprintf (w, "% s", "\ r\ n"); / / newline phead=phead- > pnext;// points to the next node} fclose (w); / / close the file} void show (char * filename) / * output file * / {FILE * fp / / File pointer stu info [100]; / / responsible for storing the data in the file, and then output if ((fp = fopen (filename, "rb")) = = NULL) {/ * binary read-only open file * / printf ("cannot open file\ n"); exit (1);} for (int I = 0; I < n) ) {fscanf (fp, "% s", & (info.name)); / / output data to array fscanf (fp, "% s", & (info.adr)); fscanf (fp, "% d", & (info.tel)); printf ("ssd\ n", info.name, info.adr, info.tel) / output data to screen} fclose (fp); / * close file * /} int main (void) {char filename [50]; printf ("How many?:\ n"); scanf ("% d", & n); / * enter number of students * / printf ("please input filename:"); scanf ("% s", filename) / * path and name of input file * / Create (); / / call function to create linked list save (filename); / / call function to save to file free (phead); / / release phead memory show (filename); / / call function output file system ("pause"); return 0 } these are all the contents of the article "how to realize linked list and File access in C language". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.