> 文章列表 > C 文件读写

C 文件读写

C 文件读写

C 文件读写

本章我们将介绍 C 程序员如何创建、打开、关闭文本文件或二进制文件。

一个文件,无论它是文本文件还是二进制文件,都是代表了一系列的字节。C 语言不仅提供了访问顶层的函数,也提供了底层(OS)调用来处理存储设备上的文件。本章将讲解文件管理的重要调用。

打开文件

您可以使用 fopen( ) 函数来创建一个新的文件或者打开一个已有的文件,这个调用会初始化类型 FILE 的一个对象,类型 FILE 包含了所有用来控制流的必要的信息。下面是这个函数调用的原型:

FILE *fopen( const char *filename, const char *mode );

在这里,filename 是字符串,用来命名文件,访问模式 mode 的值可以是下列值中的一个:

模式 描述
r 打开一个已有的文本文件,允许读取文件。
w 打开一个文本文件,允许写入文件。如果文件不存在,则会创建一个新文件。在这里,您的程序会从文件的开头写入内容。如果文件存在,则该会被截断为零长度,重新写入。
a 打开一个文本文件,以追加模式写入文件。如果文件不存在,则会创建一个新文件。在这里,您的程序会在已有的文件内容中追加内容。
r+ 打开一个文本文件,允许读写文件。
w+ 打开一个文本文件,允许读写文件。如果文件已存在,则文件会被截断为零长度,如果文件不存在,则会创建一个新文件。
a+ 打开一个文本文件,允许读写文件。如果文件不存在,则会创建一个新文件。读取会从文件的开头开始,写入则只能是追加模式。

如果处理的是二进制文件,则需使用下面的访问模式来取代上面的访问模式:

"rb", "wb", "ab", "rb+", "r+b", "wb+", "w+b", "ab+", "a+b"

关闭文件

为了关闭文件,请使用 fclose( ) 函数。函数的原型如下:

 int fclose( FILE *fp );

如果成功关闭文件,fclose( ) 函数返回零,如果关闭文件时发生错误,函数返回 EOF。这个函数实际上,会清空缓冲区中的数据,关闭文件,并释放用于该文件的所有内存。EOF 是一个定义在头文件 stdio.h 中的常量。

C 标准库提供了各种函数来按字符或者以固定长度字符串的形式读写文件。

读取文件

下面是从文件读取单个字符的最简单的函数:

int fgetc( FILE * fp );

函数 fgets() 从 fp 所指向的输入流中读取 n - 1 个字符。它会把读取的字符串复制到缓冲区 buf,并在最后追加一个 null 字符来终止字符串。

如果这个函数在读取最后一个字符之前就遇到一个换行符 ‘\\n’ 或文件的末尾 EOF,则只会返回读取到的字符,包括换行符。您也可以使用 int fscanf(FILE *fp, const char *format, …) 函数来从文件中读取字符串,但是在遇到第一个空格和换行符时,它会停止读取。
举例:

#include <stdio.h>int main()
{FILE *fp = NULL;char buff[255];fp = fopen("/tmp/test.txt", "r");fscanf(fp, "%s", buff);printf("1: %s\\n", buff );fgets(buff, 255, (FILE*)fp);printf("2: %s\\n", buff );fgets(buff, 255, (FILE*)fp);printf("3: %s\\n", buff );fclose(fp);}

写入文件

下面是把字符写入到流中的最简单的函数:

int fputc( int c, FILE *fp );

数据类型转换

一般默认从文件中读取的是字符格式或者字符串格式的数据,如果是数字要转化为float等格式怎么办呢?

方法一:直接定义负责接受的变量数据类型,按行分后再按单词分

下面这个例子就是实际应用中经常用到的例子,比如一个人有多个手机号:

一行代表一个人的信息,以行为单位存储信息:

#include "libHelloSLAM.h"
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include <sstream>
struct people{std::string name;std::vector<int> phonenum;
};
int main( int argc, char argv )
{
std::ifstream fin;
fin.open("/home/bing/slambook14/slambook/ch2/test.txt",std::ios::in);
if(!fin.is_open())
{std::cerr<<"cannot open the file";}
char line[1024]={0};
std::vector<people> People;
//从文件中提取“行”
while(fin.getline(line,sizeof(line)))
{//定义局部变量people p;//从“行”中提取“单词”std::stringstream word(line);word>>p.name ;int num;while(word>>num)p.phonenum.push_back(num);People.push_back(p);
}std::cout<<People[1].name<<"'s phonenumber is:"<< People[1].phonenum[1];  }
文件:
gyb 1333 12212
lck 212  33113
ddl 332  41311输出:
lck's phonenumber is:33113

反正最重要的是以下两个:

//从文件中提取“行”

fin.getline(line,sizeof(line))

//从“行”中提取“单词”
std::stringstream word(line);

这里用到了stringstream,需要包含, 主要用来进行数据类型转换

如:

#include <string>
#include <sstream>
#include <iostream>
#include <stdio.h>
using namespace std;int main()
{stringstream sstream;string strResult;int nValue = 1000;// 将int类型的值放入输入流中sstream << nValue;// 从sstream中抽取前面插入的int类型的值,赋给string类型sstream >> strResult;cout << "[cout]strResult is: " << strResult << endl;printf("[printf]strResult is: %s\\n", strResult.c_str());
//这里的  str() 方法是将 stringstream 类型转换为 string 类型return 0;
}

其他:

1、stringstream其实和ostringstream一样可以看错一个内存,起到暂时存储的作用 其实还有个tringsteam可以读写,待深入研究;

2、一般流的读写用<< 和>> ,而不用=

如读:word>>p.name ;把word中的东西读到p.name中

写:cin>>word

3、读写操作可以作为条件使用,若没东西可以读写了,则返回-1

如:

while(word>>num){}


补充看到的orbslam中的读取文件的代码

void LoadImages(const string &strPathToSequence, vector<string> &vstrImageFilenames, vector<double> &vTimestamps)
{// step 1 读取时间戳文件ifstream fTimes;string strPathTimeFile = strPathToSequence + "/times.txt";fTimes.open(strPathTimeFile.c_str());while(!fTimes.eof()){string s;getline(fTimes,s);// 当该行不为空的时候执行if(!s.empty()){stringstream ss;ss << s;double t;ss >> t;// 保存时间戳vTimestamps.push_back(t);}string strPrefixLeft = strPathToSequence + "/image_0/";const int nTimes = vTimestamps.size();vstrImageFilenames.resize(nTimes);for(int i=0; i<nTimes; i++){stringstream ss;ss << setfill('0') << setw(6) << i;vstrImageFilenames[i] = strPrefixLeft + ss.str() + ".png";}

最好把文件名用全局变量定义出来

string filename=“./test.txt"
//多文件,按文件名顺序读写
//使用boost::format进行字符串的格式化
boost::format fmt("./%s/%d.%s");
for(int i =0 ;i<5;i++)
cv::imread((fmt%"color"%(i+1)%"png").str())

读特定列:

int main()
{
ifstream ff1;
ff1.open("/home/bing/ORB/ORB_SLAM2/LiGT/LiGT_results.txt");
char line[1024] = {0};if(!ff1.is_open()) cerr<<"can not open";float globalt[1000][3];
int ii=0;
while(ff1.getline(line,sizeof(line)))
{std::stringstream word(line);
for(int qq=0;qq<12;qq++)
{float tamp;word>>tamp;
//   cout<< tamp<< "   "<<qq<<endl;
if (qq == 9)    globalt[ii][0] = tamp;
if (qq == 10)globalt[ii][1] = tamp;
if (qq == 11)globalt[ii][2]= tamp;
}
ii++;
}

XP系统