c语言中添加记录的代码,linux中用C语言实现的自动在文件末尾不断添加记录的完整代码...

程序能自动获取挑战到文件test.txt的结尾,自动添加一条记录,并且能够不断添加记录的代码:

#include

#include

#include

void InsertLine(char* FileName, char str[256]);

void print(char *filepath);

int main()

{

char *KeyFrameNo,*Time;

KeyFrameNo="KeyFrameNo: ";

int a=429496729,b=15;

char frameno[10];

char frametime[10];

sprintf(frameno, "%d", a);

Time="time: ";

char *result = malloc(strlen(KeyFrameNo)+strlen(frameno)+strlen(Time)+strlen(frametime)+2);//+1 for the zero-terminator

int p;

for( p=0;p<3000;p++){

sprintf(frametime, "%d", p);

strcpy(result, KeyFrameNo);

strcat(result, frameno);

strcat(result, Time);

strcat(result, frametime);

strcat(result, "\n");

InsertLine("test.txt", result);

}

print("test.txt");

free(result);

return 0;

}

/**********************************************************************

* 函数名称: InsertLine

* 功能描述: 向文件指定行增加一行

* 访问的表: 无

* 修改的表: 无

* 输入参数: char* FileName 文件

int Line  行;

* char str[256]  要增加的内容

*

***********************************************************************/

void InsertLine(char* FileName, char str[256])

{

int Lid=0;

FILE* fp=NULL;

char Buf[1024]="";

char tmp[3000][512]={0};

if ((fp=fopen(FileName,"ar+")) == NULL)

{

printf("Can't open file!/n");

return;

}

while (fgets(Buf, 1024,fp))

{

Lid++;

strcpy(tmp[Lid],Buf);

}

rewind(fp);

fputs(str,fp);

fclose(fp);

}

//输出到控制台

void print(char *filepath)

{

int nl = 0;

FILE *stream;

char s[256];

char *p = s;

stream = fopen(filepath, "r+");

while ((p = fgets(s, 256, stream)) != NULL)

{

nl++;

printf("Line %d: %s", nl, s);

}

fclose(stream);

}

输出结果为:

KeyFrameNo: 429496729time: 0

KeyFrameNo: 429496729time: 1

KeyFrameNo: 429496729time: 2

KeyFrameNo: 429496729time: 3

KeyFrameNo: 429496729time: 4

KeyFrameNo: 429496729time: 5

KeyFrameNo: 429496729time: 6

KeyFrameNo: 429496729time: 7

KeyFrameNo: 429496729time: 8

KeyFrameNo: 429496729time: 9

KeyFrameNo: 429496729time: 10

KeyFrameNo: 429496729time: 11

KeyFrameNo: 429496729time: 12

KeyFrameNo: 429496729time: 13

KeyFrameNo: 429496729time: 14

KeyFrameNo: 429496729time: 15

在C语言中,我们可以使用结构体来表示联系人,并通过单链表的数据结构来存储通讯录信息。以下是基本的实现步骤: 1. **定义数据结构**: ```c typedef struct Contact { char name[50]; char phone[20]; struct Contact* next; } Contact; ``` 这里`Contact` 结构体包含姓名、电话号码以及指向下一个联系人的指针。 2. **链表操作函数**: - **初始化列表**: 创建一个空链表 ```c Contact* createList() { return NULL; } ``` - **插入联系人**: 在链表末尾添加新的联系人 ```c void addContact(Contact* list, char* name, char* phone) { Contact* newNode = (Contact*)malloc(sizeof(Contact)); strcpy(newNode->name, name); strcpy(newNode->phone, phone); newNode->next = NULL; if (list == NULL) { list = newNode; } else { Contact* temp = list; while (temp->next != NULL) { temp = temp->next; } temp->next = newNode; } } ``` - **查找联系人**: 搜索并返回指定姓名的节点 ```c Contact* searchContact(Contact* list, char* name) { Contact* temp = list; while (temp != NULL && strcmp(temp->name, name) != 0) { temp = temp->next; } return temp; } ``` - **删除联系人**: 删除指定名称的节点,如果存在 ```c void deleteContact(Contact** list, char* name) { if (*list == NULL || (*list)->name == NULL) { return; } Contact* prev = NULL, *current = *list; if (strcmp(current->name, name) == 0) { *list = current->next; free(current); return; } while (current != NULL && strcmp(current->name, name) != 0) { prev = current; current = current->next; } if (current != NULL) { prev->next = current->next; free(current); } } ``` - **修改联系人**: 更新指定名称的联系人信息 ```c void modifyContact(Contact** list, char* oldName, char* newName, char* newPhone) { Contact* temp = searchContact(*list, oldName); if (temp != NULL) { strcpy(temp->name, newName); strcpy(temp->phone, newPhone); } } ``` - **遍历并输出联系人**: 展示通讯录内容 ```c void displayContact(Contact* list) { Contact* temp = list; while (temp != NULL) { printf("%s\t%s\n", temp->name, temp->phone); temp = temp->next; } } ``` 3. **主函数**: ```c int main() { // 初始化链表 Contact* contactList = createList(); // 用户交互操作... return 0; } ``` 这个基础框架演示了如何使用单链表在C语言中构建一个简单的通讯录系统。用户可以调用上述函数来进行输入、查找、添加、删除、修改和输出联系人信息。记得处理用户输入错误和边界情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值