本文目录一览:
- C语言链表写入文件
- C语言 怎么把结构体数组写入链表?
- 关于C语言文件数据写入链表的问题
- 如何用c语言将文件中的数据写入链表中
- C语言怎么从文件中将信息导入链表中
- c语言怎么把文本信息出去写入链队,然后处理后保存到文本文件里?提供一个思路就可以了。谢谢大神们了。
C语言链表写入文件
你的程序有什么问题,关P1指针什么事? 你的save不能写入文件吗? 你的程序在哪里释放P1指针了?
C语言 怎么把结构体数组写入链表?
- 用头插法。因为数据追加和删除比较多,追加的话,头插法可以直接插,用尾插降低了时间效率,删除用两个一样。
-
应该把结构体结点定义成链表的成员,这样链表才对。如果像你那样定义的话,完全不用定义结构体,链表就搞定了。因为你在链表里面把结构的成员都又定义了。/*结构体定义*/ struct client{ char account[14]; char name[10]; char identity[20]; char address[15]; long int money; }; /*链表结点定义*/ struct node{ struct client band_inf; struct node *next; };
- 1),定义结点:
p1=(struct node*)malloc(sizeof(struct node)) ;
表示定义一个node型的结点指针 使用,这个要看具体怎么用了。比如说删除searchp,priorp是searchp的前面一个结点,这样写
即可 插入newnode在searchp的后面,可以这样:priorp->next=searchp->next; delete searchp;
newnode->next=searchp->next;
关于C语言文件数据写入链表的问题
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
typedef struct Node {
int num;
int score;
struct Node* next;
} Node, *Linklist;
void InitLinklist(Linklist* L) //初始化单链表,建立空的带头结点的链表
{
*L = (Node*)malloc(sizeof(Node));
(*L)->next = NULL;
}
void CreateLinklist(Linklist L) //尾插法建立单链表
{
Node *r, *s;
r = L;
int iNum, iScore;
printf("Please input the number and score:\n");
scanf("%d", &iNum);
scanf("%d", &iScore);
while(0 != iScore) //当成绩为负时,结束输入
{
s = (Node*)malloc(sizeof(Node));
s->num = iNum;
s->score = iScore;
r->next = s;
r = s;
printf("Please input the number and score:\n");
scanf("%d", &iNum);
scanf("%d", &iScore);
}
r->next = NULL; //将最后一个节点的指针域置为空
}
int WriteLinklistToFile(const char* strFile, Linklist L)
{
FILE *fpFile;
Node *head = L->next;
if(NULL == (fpFile = fopen(strFile, "w"))) //以写的方式打开
{
printf("Open file failed\n");
return FALSE;
}
while(NULL != head)
{
fprintf(fpFile, "%d\t%d\n", head->num, head->score);
head = head->next;
}
if(NULL != fpFile)
fclose(fpFile);
return TRUE;
}
int ReadFromFile(const char* strFile)
{
FILE *fpFile;
if(NULL == (fpFile = fopen(strFile, "r"))) //以读的方式打开
{
printf("Open file failed\n");
return FALSE;
}
printf("The contents of File are:\n");
while(!feof(fpFile))
putchar(fgetc(fpFile));//证明fprintf()输出到文件中的绝对是字符串
if(NULL != fpFile)
fclose(fpFile);
return TRUE;
}
void Destroy(Linklist L)
{
Node *head = L;
while (head)
{
Node* temp = head;
head = head->next;
free(temp);
}
}
int main()
{
char* strName = "test.txt";
Linklist L;
InitLinklist(&L);
CreateLinklist(L);
WriteLinklistToFile(strName, L);
ReadFromFile(strName);
Destroy(L);
return 0;
}
如何用c语言将文件中的数据写入链表中
sw是我链表的首地址 fp是文件的指针 下面定义链表类型:num域存放的是int型数据,可根据你的情况来改变。
typedef struct node {
int num;
struct node *next;
} node;
node *p; //指向链表中的首元结点
while(p != NULL) {
fprintf(fp, "%d,%s", p->num);
p = p->next;
}
其实,这样操作是非常简单的。
C语言怎么从文件中将信息导入链表中
这个实现起来挺简单的,就是写起来麻烦点,不是一点代码能写完的!
- 建立一个链表,链表的节点struct定义为联系人信息的格式;
- 读取文件,把内容存入链表;
- 查询就根据姓名关键字遍历链表就行了;
- 把内容存入文件; 首先建立链表,以及插入节点,查询链表函数写出来; 文件的读取和存入到不是很麻烦; ----------------------------下面是简单的实现,可以输入,存入文件,从文件读取,打印,如果还想要实现其他的稍修改一下就行了------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 80
struct stu {
int id;
char name[MAX];
struct stu* next;
};
typedef struct stu STU;
typedef STU* STUPTR;
STUPTR insert(STUPTR head, int id, char* name);
void print_st(STUPTR head);
void save_to_file(FILE* fp, STUPTR head);
FILE* crt_file(void);
STUPTR read_to_link(STUPTR head);
int main(void)
{
int choice;
int stu_id;
char stu_name[MAX];
STUPTR head;
FILE* fp;
head = NULL;
printf("please enter the choice!\n");
scanf("%d", &choice);
while(choice != 9) {
switch(choice) {
case 1:
printf("enter the insert ----id---name!\n");
scanf("%d%s", &stu_id, stu_name);
head = insert(head, stu_id, stu_name);
break;
case 2:
print_st(head);
break;
case 3:
puts("save the info to file e:\\stu_info.txt!\n");
fp = crt_file();
save_to_file(fp, head);
puts("save the data sucessful!\n");
fclose(fp);
break;
case 4:
puts("read the file to link!\n");
head = NULL;
head = read_to_link(head);
puts("read the file successful!\n");
break;
}
printf("please enter the choice!\n");
scanf("%d", &choice);
}
}
STUPTR insert(STUPTR head, int id, char* name)
{
STUPTR new, cur;
cur = head;
new = malloc(sizeof(STU));
new->id = id;
strcpy(new->name, name);
new->next = NULL;
if(cur == NULL)
head = new;
else {
while(cur->next != NULL) {
cur = cur->next;
}
cur->next = new;
}
return head;
}
void print_st(STUPTR head)
{
STUPTR cur = head;
int i;
i = 1;
if(cur == NULL) {
printf("has no student info!\n");
} else {
while(cur != NULL) {
printf("%d:------%d---%s\n", i, cur->id, cur->name);
i++;
cur = cur->next;
}
}
}
void save_to_file(FILE* fp, STUPTR head)
{
STUPTR cur;
cur = head;
while(cur != NULL) {
fprintf(fp, "%d %s\n", cur->id, cur->name);
cur = cur->next;
}
}
FILE* crt_file(void)
{
FILE* fp;
fp = fopen("e:\\stu_info.txt", "w");
if(fp == NULL)
puts("shit!!!!!!!!!!");
return fp;
}
STUPTR read_to_link(STUPTR head)
{
int id;
char name[MAX];
FILE* fp;
fp = fopen("e:\\stu_info.txt", "r");
while(!feof(fp)) {
fscanf(fp, "%d%s", &id, name);
head = insert(head, id, name);
}
return head;
}
c语言怎么把文本信息出去写入链队,然后处理后保存到文本文件里?提供一个思路就可以了。谢谢大神们了。
你的描述有点不清楚噶。我大概说一下,把文本文件以数据流的方式读出来,读到文件尾。把读出来的数据放到链表或者队列,一个一个处理,处理一个就往文本文件里面填一个。最后保存文件就可以了。