C++对文本的操作

偶尔用到,但老是忘记,现在写下整理出来

一.c++部分:

(1)将“You Are Beautiful" 输出到工程中,并保存在名为data.txt“的文本文档中

# include<fstream>
using namespace std;
int main()
{
ofstream ocout;//定义输出流的一个对象
ocout.open("data.txt");
ocout<<"You Are Beautiful;";
ocout.close();
return -1;
}

      这句话的意思就是调用ofstream类中的构造函数来创建这个文本文件。另外,我们需要特别注意一点,在完成对整个文件的操作之后,一定要用close()函数将这个文件关闭了,否则在程序结束后,所操作的文件将什么都不会保存下来!!!

(2)将“data.txt”文件中的内容输出到cmd上。

#include<fstream>
#include<iostream>
using namespace std;

int main()
{
ifstream icin;
icin.open("data.txt");
char temp[100];//定义一个数组,用来存放文本中的数组
icin>>temp;//将文件中的数组输出到temp中去
cout<<temp<<endl;//将temp中的内容输出到cmd(屏幕)上去;
//icin.close();
return -1;
}

我们发现执行上述代码时是没有问题的,可是,存在一个问题,即cmd上显示的并不是完整的文本文档的内容,二是仅仅小部分的内容:如下所示:

原因是什么呢?

这是因为C++的插入操作符有一个毛病,它一遇到空字符便会停止输出。这里的空字符是指空格,或者“\0"。因此,当文件中一旦遇到空格,或者”\0",便会停止输出。解决的办法是采用getline()函数。

#include<fstream>
#include<iostream>
using namespace std;

int main()
{
ifstream icin;
icin.open("data.txt");
char temp[100];//定义一个数组,用来存放文本中的数组
icin.getline(temp,100);//将文件中的数组输出到temp中去
cout<<temp<<endl;//将temp中的内容输出到cmd(屏幕)上去;
//icin.close();
return -1;
}

那么getline函数是怎样做到的呢?

下面是getline(_Istr, _Str,_Delim)函数的参数介绍:

_Istr:输入字符串

(The input stream from which a string is to be extracted.)

_Str:读取的字符串的个数

(The string into which are read the characters from the input stream.)

_Delim:结束符:

(The line delimiter.)

getline后面可以指定2个参数或3个参数。

第一个参数可以是字符串的首地址;2个参数时,第2个参数就是读取的位数;3个参数时,第3个参数是终止符,只要2个参数中有一个满足了,就停止读入。

上面代码中的意思是连续读100个字符,然后终止。

更具体的介绍可以参见下面的链接:

http://blog.sina.com.cn/s/blog_61afc0260100kqb4.html

返回到原话题。通过用getline()函数,我们会得到正确的结果:

下面插入一段简单的代码用于说明数据类的输出:

int main()
{
int a[10]={1, 2 ,3, 4 ,5 ,6 ,7 ,8 ,9, 0};
int i=10;
ofstream ocout;
ocout.open("data.txt");
for(i=0;i<=9;i++)
{
ocout<<a[i]<<endl;
}
ocout.close();
return -1;
}



(3)二进制文件和文本文件的区别:

我们知道:

C语言把文件看作是一个字符(字节)的顺序排列。根据数据的组织形式,可以分为ASCII(文本文件)和二进制文件。文本文件的每一个字节放一个ASCII代码,代表一个字符。二进制文件是把内存中的数据按其在内存中的存储形式原样的输出到磁盘文件中。例子说明一切问题:假如一个int a=10000;那么,a在内存中占用2个字节(每个字节占8位)。若以二进制文件输出,那么在磁盘中占用的存储空间也为2个字节,但是如果以ASCII的形式存放,那么在磁盘中则组要5个字节。如下所示:

00100111 00010000    (10000在内存中的组织形式)    

00110001 00110000 00110000  00110000 00110000 (10000以ASCII形式在磁盘中的存储方式)

00100111 00010000(10000以二进制形式在磁盘中的存储方式)

用二进制的形式来输出数值,可以节省外在的空间和转换空间,但一个字节并不对应一个字符,不能直接输出字符的形式。

我们用代码来解释以下:

用文本文件的形式输出:

#include<iostream>
#include<fstream>
#include"stdio.h"

using namespace std;

struct list
{
char name[20];
int num;
int age;
char grade[3];
}student;


int main()
{
/*int a[10]={1, 2 ,3, 4 ,5 ,6 ,7 ,8 ,9, 0};
int i=10;
ofstream ocout;
ocout.open("data.txt");
for(i=0;i<=9;i++)
{
ocout<<a[i]<<endl;
}
ocout.close();
return -1;
*/


list student ={"李明",10123,21,"A"};//初始化
//student.name="LIMING";
//student.num=030123;
//student.age=21;
//student.grade="A";
ofstream ocout;
ocout.open("data.txt");
ocout<<student.name;
ocout<<student.num;
ocout<<student.age;
ocout<<student.grade;

ocout.close();

//输出数据显示在cmd窗口上
ifstream icin;
icin.open("data.txt");
char Temp[100];
icin.getline(Temp,100);
cout<<Temp<<endl;
icin.close();

return -1;

}

在cmd和txt文件中显示的内容是一致的。先写到这里。要吃饭了。在调试的过程中还是出现了写小问题,下一步在解决。

原文地址:https://www.cnblogs.com/CBDoctor/p/2237970.html