C++ (IO操作小案列) 基于本地文本文档的读写
2017-05-27 16:20
459 查看
基于本地文本文档的读写。
1,把学生的姓名 ,年龄, 籍贯,通过控制台保存到本地的txt文档里面。
2,读取txt文档里面学生的姓名,年龄,籍贯,并按一定的格式输出控制台。
IDE:VS2012 ,使用语言:c++。
主文件:Main.cpp
#include "stdafx.h"
#include "Student.h"
#include <string>
#include <fstream>
#include <iostream>
#include <stdio.h>
#include "stdafx.h"
using namespace std;
//显示菜单
void MainMenu();
void ReadFile();
void WriteFlie();
int main(int argc, char *argv[])
{
//输入选项
int nSelect = -1;
do
{
nSelect = -1;
MainMenu();
cin>>nSelect;
cin.clear();
cin.sync();
switch (nSelect)
{
case 1:
WriteFlie();
break;
case 2:
ReadFile();
break;
case 0:
break;
default:
cout<<"输入错误!\n"<<endl;
break;
}
}while ( 0 != nSelect);
return 0;
}
void MainMenu()
{
cout<<"1 写入学生信息 "<<endl;
cout<<"2 读取学生信息 "<<endl;
cout<<"0 退出"<<endl;
cout<<"请输入选项: ";
}
void ReadFile()
{
string err;
fstream inFile;
try
{
inFile.open("student.txt", ios::in); //打开文件
if(!inFile.good())
{
err ="打开文件失败!\n\n";
throw err;
}
CStudent stu;
cout.setf(ios::left);
cout<<"\n "<<endl;
cout<<setw(10)<<"姓名"<<setw(8)<<"年龄"<<setw(30)<<"家庭住址"<<"\n"<<endl;
while(!inFile.eof())
{
cout.setf(ios::left);
inFile>>stu; //读文件
cout<<stu<<endl;
if(!inFile.good() && !inFile.eof())
{
err ="读取文件失败!\n\n";
inFile.close();
throw err;
}
}
inFile.close();
if(!inFile.good()&& !inFile.eof())
{
err ="关闭文件失败!\n\n";
throw err;
}
}
catch(string e)
{
cout<<e;
}
}
void WriteFlie()
{
fstream outFile;
outFile.open("student.txt",ios::out | ios::app);
CStudent stu;
cout<<"请输入学生的信息(eg:Cheney 22 Chinese):\n ";
cin>>stu;
outFile<<stu; //写文件
//outFile.flush(); //清空件缓存
outFile.close(); //关闭文
}
Student类文件:Student.cpp
#include "stdafx.h"
#include "Student.h"
#include <iostream>
#include <fstream>
#include <stdio.h>
CStudent::CStudent(void)
{
strcpy(m_strName," ");
m_nAge = 0;
strcpy(m_strAdress," ");
}
CStudent::CStudent(char strName[10], int nAge, char strAdress[30])
{
strcpy(m_strName,strName);
m_nAge = nAge;
strcpy(m_strAdress,strAdress);
}
//析构函数
CStudent::~CStudent(void)
{
}
ostream &operator<<(ostream &outfile,CStudent &stu)
{
//格式化输出CStudent对象
outfile<<setw(10)<<stu.m_strName<<setw(8)<<stu.m_nAge<<setw(30)<<stu.m_strAdress;
return outfile;
}
istream &operator>>(istream &infile,CStudent &stu)
{
infile>>stu.m_strName>>stu.m_nAge>>stu.m_strAdress;
return infile;
}
Student头文件:Student.h
#pragma once
#include <iostream>
#include <iomanip>
using namespace std;
class CStudent
{
public:
CStudent(void);
CStudent(char strName[10],int nAge,char strAdress[30]);
~CStudent();
public:
friend ostream &operator<<(ostream &outfile,CStudent &stu);
friend istream &operator>>(istream &infile,CStudent &stu);
private:
char m_strName[10]; //姓名
int m_nAge; //年龄
char m_strAdress[30]; //家庭地址
};
Coding所遇问题:
如何解决This function or variable may be unsafe的问题。
1,用VS2013打开出现错误的代码文件,
2,在工程文件名处右击鼠标打开快捷菜单,找到“属性”选项,进入项目属性页面
3,在属性页面中找到“C/C++"——”预处理器“
4,在下面的编辑窗口中添加一句命令:_CRT_SECURE_NO_WARNINGS
添加完成后应用并退出
5,再次编译并运行即可正常运行。
1,把学生的姓名 ,年龄, 籍贯,通过控制台保存到本地的txt文档里面。
2,读取txt文档里面学生的姓名,年龄,籍贯,并按一定的格式输出控制台。
IDE:VS2012 ,使用语言:c++。
主文件:Main.cpp
#include "stdafx.h"
#include "Student.h"
#include <string>
#include <fstream>
#include <iostream>
#include <stdio.h>
#include "stdafx.h"
using namespace std;
//显示菜单
void MainMenu();
void ReadFile();
void WriteFlie();
int main(int argc, char *argv[])
{
//输入选项
int nSelect = -1;
do
{
nSelect = -1;
MainMenu();
cin>>nSelect;
cin.clear();
cin.sync();
switch (nSelect)
{
case 1:
WriteFlie();
break;
case 2:
ReadFile();
break;
case 0:
break;
default:
cout<<"输入错误!\n"<<endl;
break;
}
}while ( 0 != nSelect);
return 0;
}
void MainMenu()
{
cout<<"1 写入学生信息 "<<endl;
cout<<"2 读取学生信息 "<<endl;
cout<<"0 退出"<<endl;
cout<<"请输入选项: ";
}
void ReadFile()
{
string err;
fstream inFile;
try
{
inFile.open("student.txt", ios::in); //打开文件
if(!inFile.good())
{
err ="打开文件失败!\n\n";
throw err;
}
CStudent stu;
cout.setf(ios::left);
cout<<"\n "<<endl;
cout<<setw(10)<<"姓名"<<setw(8)<<"年龄"<<setw(30)<<"家庭住址"<<"\n"<<endl;
while(!inFile.eof())
{
cout.setf(ios::left);
inFile>>stu; //读文件
cout<<stu<<endl;
if(!inFile.good() && !inFile.eof())
{
err ="读取文件失败!\n\n";
inFile.close();
throw err;
}
}
inFile.close();
if(!inFile.good()&& !inFile.eof())
{
err ="关闭文件失败!\n\n";
throw err;
}
}
catch(string e)
{
cout<<e;
}
}
void WriteFlie()
{
fstream outFile;
outFile.open("student.txt",ios::out | ios::app);
CStudent stu;
cout<<"请输入学生的信息(eg:Cheney 22 Chinese):\n ";
cin>>stu;
outFile<<stu; //写文件
//outFile.flush(); //清空件缓存
outFile.close(); //关闭文
}
Student类文件:Student.cpp
#include "stdafx.h"
#include "Student.h"
#include <iostream>
#include <fstream>
#include <stdio.h>
CStudent::CStudent(void)
{
strcpy(m_strName," ");
m_nAge = 0;
strcpy(m_strAdress," ");
}
CStudent::CStudent(char strName[10], int nAge, char strAdress[30])
{
strcpy(m_strName,strName);
m_nAge = nAge;
strcpy(m_strAdress,strAdress);
}
//析构函数
CStudent::~CStudent(void)
{
}
ostream &operator<<(ostream &outfile,CStudent &stu)
{
//格式化输出CStudent对象
outfile<<setw(10)<<stu.m_strName<<setw(8)<<stu.m_nAge<<setw(30)<<stu.m_strAdress;
return outfile;
}
istream &operator>>(istream &infile,CStudent &stu)
{
infile>>stu.m_strName>>stu.m_nAge>>stu.m_strAdress;
return infile;
}
Student头文件:Student.h
#pragma once
#include <iostream>
#include <iomanip>
using namespace std;
class CStudent
{
public:
CStudent(void);
CStudent(char strName[10],int nAge,char strAdress[30]);
~CStudent();
public:
friend ostream &operator<<(ostream &outfile,CStudent &stu);
friend istream &operator>>(istream &infile,CStudent &stu);
private:
char m_strName[10]; //姓名
int m_nAge; //年龄
char m_strAdress[30]; //家庭地址
};
Coding所遇问题:
如何解决This function or variable may be unsafe的问题。
1,用VS2013打开出现错误的代码文件,
2,在工程文件名处右击鼠标打开快捷菜单,找到“属性”选项,进入项目属性页面
3,在属性页面中找到“C/C++"——”预处理器“
4,在下面的编辑窗口中添加一句命令:_CRT_SECURE_NO_WARNINGS
添加完成后应用并退出
5,再次编译并运行即可正常运行。
相关文章推荐
- C++文件读写操作(二)逐字符读取文本和逐行读取文本
- Java文本文档的读写操作(复习笔记)
- C++文件读写操作(三)如何统计文本的行数及如何读取文件某一行内容
- C++文本的读写操作
- 文件读取C++文件读写操作(二)逐字符读取文本和逐行读取文本
- python对文本文档的读写和删除操作-2
- C++对txt文本进行读写操作
- IO 文档操作案列
- 打开指针C++文件读写操作(一)将字母表写入TXT文本文件
- C++文件读写操作(将字幕写入txt文件文本)
- python对文本文档的读写和删除操作-1
- c#/.net 基于文件流FileStream读写的文本操作小程序
- C++对txt文本进行读写操作
- 文件读取C++文件读写操作(三)如何统计文本的行数及如何读取文件某一行内容
- C++文件读写操作(一)将字母表写入TXT文本文件
- c#/.net 基于文件流FileStream读写的文本操作小程序
- C++ 文件IO操作 读写
- C/C++ 文件读写操作总结
- C++中对文件进行读写操作
- C++中对文件进行读写操作