C++数据链表在txt中储存与读取问题
来源:互联网 发布:java封装修饰符 编辑:程序博客网 时间:2024/05/22 18:23
// text_savedata.cpp : 定义控制台应用程序的入口点。
//
#include "StdAfx.h"
#include <string>
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <fstream>
#include <conio.h>
# define LEN sizeof(struct news)
# define NULL 0
using namespace std;
struct news
{
char type;
//int number;
char title [30];
char content [300];
struct news * next;
};
struct news * creat(struct news * head)
{
int n;
char TC;
struct news * p1, * p2;
n = 0;
p1 = p2 = (struct news *) malloc(LEN);
cout<<endl<<"请输入该新闻的类型标号:";
cin>>p1->type;
cout<<endl<<"请输入该新闻的标题:";
cin>>p1->title;
cout<<endl<<"请输入该新闻的内容:";
cin>>p1->content;
cout<<endl<<"退出请输入T "<<endl;
cin>>TC;
head = NULL;
head = p1;
while(TC != 'T')//最好按回车即退出
{
n = n+1;
/*if(n == 1)
head = p1;
else */
p2->next = p1;
p2 = p1;
p1 = (struct news *)malloc(LEN);
cout<<"请输入该新闻的类型标号:";
cin>>p1->type;
cout<<"请输入该新闻的标题:";
cin>>p1->title;
cout<<"请输入该新闻的内容:";
cin>>p1->content;
cout<<"退出请输入T "<<endl;
cin>>TC;
//cout<<n;
}
p2->next = p1; //将最后一个结构体连接到链表中
p1->next = NULL;
return (head);
}
void print (struct news * head) //输出链表中的数据
{
int a = 0;
struct news * p;
cout<<"依次输出链表中的数据:"<<endl;
p = head;
if(head != NULL)
do
{ a++;//前边的编号
cout<<a;
cout<<"新闻类型:"<<p->type<<endl<<"新闻标题:"<<p->title<<endl<<"新闻内容:"<<p->content<<endl;
p = p->next;
}while(p != NULL);
}
void savedata(struct news * head) //保存数据
{
struct news head1;
fstream file("data.txt",ios::out);
if(head != NULL)
{
while(head)
{
file.write((char *) head,sizeof(head1)); //获取结构体长度的二进制码
head = head->next; //指针下移
}
}
file.close();
cout<<"保存成功!";
}
struct news * readdata () //读取数据
{
news * P = new news ,* Q = P ;
P->next =NULL;
struct news news1;
fstream file("data.txt",ios::in);
while(1)
{
file.read((char *)& news1,sizeof(news1));
if(!file)break;
else
{
struct news * V = (struct news *) malloc(LEN);
//cout<<news1.type<<endl;
//cout<<news1.title<<endl; //测试用的
//cout<<news1.content<<endl;
V->type = news1.type;
strcpy(V->title,news1.title); //复制文件
strcpy(V->content,news1.content);
//cout<<V->type<<V->title<<V->content<<endl; //测试用的
//cout<<"********************************************";
Q->next = V;
Q->next ->next = NULL;
Q = Q->next;
}
}
file.close();
return (P->next); //返回指针如果返回P则有个指向Q的多余指针
}
int main ()
{
struct news * head;
head = NULL;
head = creat(head);
savedata(head);
head = readdata ();
print(head);
getch();
return 0;
}
- C++数据链表在txt中储存与读取问题
- 读取文件(.txt、.excel、.csv),在c++、matlab环境中
- C中printf与scanf函数读取与储存参数实现办法
- C++按行读取文件内容,储存在数组中
- 在j2me中读取txt文件数据
- 在j2me中读取txt文件数据
- 在j2me中读取txt文件数据
- C#读取txt文档中乱码问题
- 【C#】读取txt、csv等二维表
- C读取txt教程
- C\C++读取txt
- C#--读取txt文件
- C读取txt文档
- java 读取TXT 文件 在切分 添加在数据库中
- 读取()txt文件中的内容与向txt文件中写入内容(覆盖重写与在末尾续写+FileOutputStream与FileWriter)
- 在sql server2005中读取txt文件的内容
- 从txt文件读取数据显示在文本框中
- winform 读取TXT文件 放在Label中
- 看了DOS中断后我内牛满面
- 怎样的CMS系统才是好的网站管理平台
- Quartz任务调度快速入门
- Useful Method 'IsViewReadOnly'.
- 请高手指点taskmgr.exe是什么进程?谢谢!
- C++数据链表在txt中储存与读取问题
- AS3: ByteArray几个要注意的方法和属性
- 以后
- 从Txt文件读取数据到数据视图
- 解读iPhone平台的一些优秀设计思路
- mscorsvw.exe是windows的什么进程!!
- 关于Hibernate你必须知道的六个方面
- 使用ASP.NET Membership 和 角色管理器
- Using proxy as Event