#include <iostream>
#include <fstream>
#include<iomanip>
#include<string>
using namespace std;
struct node
{ string data;
string word;
string sign;
//string name; struct node* next;
};
//建立以head指標的具有n個結點的鏈表,head為參考引數,
//以使對應的實參為該鏈表的表頭指標
void creat(node*& head, int n)
{ if (n < 0) { cout << "n值無效" << endl; exit(1); } if (n == 0) {//置表頭指標為空后回傳 head = NULL; return; } cout << "從鍵盤上輸入" << n << "組資料" << endl; string x;int i;string w1,w2; node* last, * p; last = head = NULL; //置表頭、表尾指標為空,此時為空鏈表 p = new node; //產生一個p所指的動態結點 head = last = p; cout<<"輸入一個數字"<<endl;
cin >>x;
cout<<"輸入一個字符"<<endl;
cin>>w1;
cout<<"輸入一個字符"<<endl;
cin>>w2;
cout<<endl<<endl<<endl; p->data = x;p->word=w1;p->sign=w2;//資料區已修改 p->next = NULL; if (n == 1) return; for (i = 1; i <= n - 1; i++) { p = new node; last->next = p; cout<<"輸入一個數字"<<endl; cin >>x; cout<<"輸入一個字符"<<endl; cin>>w1; cout<<"輸入一個字符"<<endl; cin>>w2; cout<<endl<<endl<<endl; p->data = x;p->word=w1;p->sign=w2;//資料區已修改 last = p; } p->next = NULL;
}
void save(node*& head)//函式名稱已修改
{
node *p; ofstream file("yourinfo.txt",ios::app);//打開這個檔案 file << left <<setw(10)<<head->data<<setw(10)<<head->word<<setw(10)<<head->sign<< endl; p = head->next; while (p != NULL) { file << left <<setw(10)<<p->data<<setw(10)<<p->word<<setw(10)<<p->sign<< endl; p = p->next; } file.close();//關閉檔案
}
void readfile() //讀取檔案中的資料
{
node *head,*rear,*G;
head=new node;
G=new node;
rear=head;
ifstream read("yourinfo.txt");
if(!read)
{
cout<<"讀取檔案失敗!!"<<endl;
system("pause");
exit(1);
}
while(read>>setw(10) >>G->data>>setw(10) >>G->word>>setw(10)>>G->sign)
{
G=new node;
rear->next=G;
rear=G;
read>>setw(10) >>G->data>>setw(10) >>G->word>>setw(10)>>G->sign;
}
rear->next=NULL;
read.close();
}
int main()
{ cout << "需要存盤多少組資料" << endl; node* head; int n; cin >> n; creat(head, n); cout << endl << endl << endl; save(head); return 0;
}
uj5u.com熱心網友回復:
讀和寫需要一一對應uj5u.com熱心網友回復:
初學c++ 請問您能詳細指導下嘛uj5u.com熱心網友回復:
初學c++ 請問您能指導下嘛uj5u.com熱心網友回復:
你得描述具體的問題是什么uj5u.com熱心網友回復:
就是我在讀取資料的時候 readfile檔案有錯誤嗎轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/32542.html
標籤:C++ 語言
上一篇:擲骰子游戲1)假如有n個學生排成一圈,編號從1到n,每個人有初始積分,不防設為100。2)從1號開始擲骰子,骰子出現后,根據點數減去擲骰人的相應積分, 如
下一篇:C語言陣列問題,求大佬解答
