C++读文件若干方法

第一个示例:

c++里怎样读文件呀,是这样的一个文件,每行行有两个数据,共有n行,要分别把每行的第一,二个元素读取出来,分别存到a,b两个向量当中,不知道该怎么处理。

-6.0000000e+001 2.6356764e+000
-5.9899487e+001 2.6289755e+000
-5.9798974e+001 2.6222747e+000
-5.9698462e+001 2.6155738e+000
-5.9597949e+001 2.6088730e+000
-5.9497436e+001 2.6021721e+000
-5.9396923e+001 2.5954713e+000
-5.9296411e+001 2.5887704e+000
-5.9195898e+001 2.5820696e+000
-5.9095385e+001 2.5753687e+000
-5.8994872e+001 2.5686679e+000
-5.8894359e+001 2.5619670e+000
-5.8793847e+001 2.5552662e+000
-5.8693334e+001 2.5485653e+000
-5.8592821e+001 2.5418645e+000
-5.8492308e+001 2.5351636e+000
-5.8391796e+001 2.5284627e+000
-5.8291283e+001 2.5217619e+000
-5.8190770e+001 2.5150610e+000
-5.8090257e+001 2.5083602e+000
-5.7989744e+001 2.5016593e+000
-5.7889232e+001 2.4949585e+000
-5.7788719e+001 2.4882576e+000
-5.7688206e+001 2.4815568e+000
-5.7587693e+001 2.4748559e+000
-5.7487181e+001 2.4681551e+000

解答如下:

#include<iostream> #include<fstream>
#include
<sstream>
#include
<string>
#include
<vector>
using namespace std;
int main()
{
    ifstream infile(
"d://aaa.txt");
    //默认是输出模式,附加模式(即不破坏原有内容)则: ofstream outfile   ("d://bbb.txt",ios_base::app)
    ofstream outfile(
"d://bbb.txt");  
   
string sline;//每一行
    string s1,s2;
    vector
<string> vec1,vec2;
   
while(getline(infile,sline))
    {
        istringstream sin(sline);
        sin
>>s1>>s2;
        vec1.push_back(s1);
        vec2.push_back(s2);
    }
    outfile
<<"第1列:"<<endl;
    vector
<string>::const_iterator it;
   
for(it=vec1.begin();it!=vec1.end();++it)
        outfile
<<*it<<"/n";
    outfile
<<"第2列:"<<endl;
   
for(it=vec2.begin();it!=vec2.end();++it)
        outfile
<<*it<<"/n";
    outfile
<<endl;
   
return 0;
}

+++++++++++++++++++++华丽的分割线+++++++++++++++++++++++
第二个示例
#include<stdio.h>
int main()
{
char str[80];
char get[10];
int i;
FILE *fp,*fget;
fp=fopen("D://1.txt","a+");
scanf("%s",str);
fprintf(fp,"%s",str);
fseek(fp,0,SEEK_SET);//函数的第一个参数的要操作的文件指针
//第三个参数是你的偏置量的参考基点(可以是文件头/文件尾/当前位置)
//第二个参数是你的偏置量
fscanf(fp,"%s",get);
fget=fopen("D://2.txt","w");
fprintf(fget,"%s",get);
printf("%s",get);
getchar();
getchar();
}

+++++++++++++++++++++++++++华丽的分割线++++++++++++++++++++++++
示例三:

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

void main()
{
ifstream fin;
char st[10];
fin.open("input.txt", ios::in|ios::nocreate); // 以输入方式打开文件,如果文件不存在,不会创建文件
fin.getline(st, 10, '/n'); // 10为字符串数组最大可用空间,包括作结束标记的0;'/n'为输入的结束字符
cout<<st<<endl;
}
上面关于可以指代文件的结束点,恩值得注意一下,没有测试过

 

 

+++++++++++++++++++++++++++华丽的分割线++++++++++++++++++++++++

  ifstream fin; 
  char st[10]; 
  fin.open(m_txtPath, ios::in/*|ios::nocreate*/); // 以输入方式打开文件,如果文件不存在,不会创建文件 
  fin.getline(st, 10, '/n'); // 10为字符串数组最大可用空间,包括作结束标记的0;'/n'为输入的结束字符 
  cout<<st<<endl;

 

 

 

 

 

win32 API有:

CreateFile
OpenFile
ReadFile
WirteFile

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值