C++对txt文件的操作

来源:互联网 发布:鬼故事粤语软件 编辑:程序博客网 时间:2024/05/18 02:23

简单总结一下txt文件使用c++读取和写入的操作:

参考:http://www.cplusplus.com/reference/cstdio/

主要就是c语言的标准输入输出库里的方法的使用。

首先是确定流的概念,即stream。文件操作通过流来实现到内存的读入和读出。具体不再展开。

需要用到的有:

fscanf、fgetc、fclose等几个操作,可以在参考网站找到示例代码。

网站对c的标准输入输出库的介绍:

This library uses what are called streams to operate with physical devices such as keyboards, printers, terminals or with any other type of files supported by the system. Streams are an abstraction to interact with these in an uniform way; All streams have similar properties independently of the individual characteristics of the physical media they are associated with.
翻译:

该库使用被称为流的东东来操作物理硬件如键盘打印机命令行或者其他能够被系统支持的文件,流是一个和这些设备以一个标准方式交互的的方法抽象,所有的流都有相似的属性,这些属性与和他们相关联的物理媒体的独有属性想独立。

Streams are handled in the cstdio library as pointers to FILE objects. A pointer to a FILE object uniquely identifies a stream, and is used as a parameter in the operations involving that stream.

流在c标准输入输出库中作为一个指向FILE对象的指针来被操作,一个指向FILE对象的对象指针特定地标识一个数据流,并且在代码中作为function的参数来被调用。

例如调用fclose函数,

FILE* pf_1;

pf_1 = fopen("1.txt","r");

fclose(pr_1);//fclose的参数就是那个FILE类型的指针变量pf_1,第二句代码是将文件1.txt以read的形式打开并和pf_1相关联起来。


以上是基本的操作,讲的是几个常用的功能:

1)计算txt文件的行数;

首先确定的方法就是计算文件中回车的个数,因为每个换行符代表的就是一行。txt的最后一行要看看有没有回车,要是没有记得加上。

比如1.txt文件里面是这样的:

-89.241,333.031,-30.166,254,255,255
-89.243,333.028,-30.225,254,255,255
-89.235,333.031,-30.281,255,255,255
-89.236,333.029,-30.339,255,255,255
-89.237,333.027,-30.398,254,255,253
-89.234,333.027,-30.455,254,254,254
-89.234,333.026,-30.513,254,255,255
-89.241,333.021,-30.573,255,255,255
-89.243,333.018,-30.631,255,255,255
-90.041,332.573,-30.820,255,255,255
-89.249,333.012,-30.747,253,254,255
-90.043,332.569,-30.933,252,253,255
-89.314,332.975,-30.815,252,253,255
-90.010,332.586,-30.985,252,253,255
-89.257,333.005,-30.864,252,253,255
-90.040,332.568,-31.046,254,254,254
-89.283,332.989,-30.926,254,254,254
-90.047,332.563,-31.104,255,255,255
-89.289,332.984,-30.985,255,255,255
-89.244,333.008,-31.035,253,255,254

要想计算行数,需要使用的函数及时fgetc来逐个获取文件的每个字节,在得到\n之后,计数器加1,注意是\n,写成/n就sb了。

feof函数的介绍:

A non-zero value is returned in the case that the end-of-file indicator associated with the stream is set.
Otherwise, zero is returned.

一个非0的值会被返回如果到达了文件最后,否则会返回0.

int numofLines(char* fileName)

{

FILE* pf_1;

int c;//定义整形c,用来接收fgetc返回的整形数据

int n  = 0;//计数器初始0

pf_1 = fopen(fileName,"r");

if(pf_1 == NULL) cert<<"can not open the file"<<endl;//该句要养成习惯

else{

do{

c = fgetc(pf_1);

if(c == '\n') ++n;

}

while(c != EOF);

}

fclose(pf_1)

return n;

}

以上function返回n即为行数。

2)将txt中的各行数据读入到array中。

首先创建一个结构体,内部定义一个6元素的double数组a[6],数组a的每个元素对应每行数据的一个元素。

再创建一个结构体数组,这样结构体数组的每个元素就代表了一行数据。

int n;//n为点数

struct st

{

double a[6];

};

st st_1[n];//这样就定义了一个结构数组,对应每一个行。

FILE* pf_1;

pf_1 = fopen(fileName,"r");//打开文件

for(int i = 0;i < n;i++){

if(6==fscanf(pt_1,"%lf,%lf,%lf,%lf,%lf,%lf\n",&st_1[i].a[0],&st_1[i].a[1],&st_1[i].a[2],&st_1[i].a[3],&st_1[i].a[4],&st_1[i].a[5],)){

}

}

此时就将文件内部的数据逐行读入到数组里面了。



0 0