C++文件读写

来源:互联网 发布:袁金明网络办公平台 编辑:程序博客网 时间:2024/05/16 10:37
#include <fstream.h>


struct OBJECT 

int number; 
char letter; 
}obj;


void main()
{
ofstream fout;
fout.open("LJJ.txt");
int num = 50;
char name[]= "lijiangjing";
fout<<"his is number:"<<num<<"\n";
fout<<"his is name:"<<name<<"\n";
//fout.flush();
fout.close();


ifstream fin("input.txt");
if (!fin.is_open())
{
cout<<"文件不存在"<<endl;
return ;
}
int number;
float real;
char letter, word[8];
char conront[100];
fin >> number;
fin >> word;
fin >> real; 
fin >> letter;
cout<<number<<" "<<word<<" "<<real<<" "<<letter<<endl;
fin.seekg(0,ios::beg);//表示跳转到离文件开始的0字符的位置。
fin >> number >> word >> real >> letter;
cout<<number<<" "<<word<<" "<<real<<" "<<letter<<endl;
fin.get();
letter = fin.peek();
cout<<"lit "<<letter<<endl;
letter = fin.get();
cout<<"lit "<<letter<<endl;
fin.seekg(-30,ios::end);//ios::end 表示文件结尾;ios::beg 表示文件开始;ios::cur表示当前位置;
//fin.ignore(5,'\n');//跳过5个字符,或者遇到'\n'结束
fin.getline(conront,100);
cout<<conront<<endl;
fin.close();

ofstream out;
out.open("LJJ.txt",ios::binary|ios::ate);
//ios::app 添加到文件尾
//ios::ate 把文件标志放在末尾而非起始。
//ios::trunc 默认. 截断并覆写文件。
//ios::nocreate 文件不存在也不创建。
//ios::noreplace 文件存在则失败。
OBJECT a;
a.number = 45;
a.letter = 'H';
int numb = 60;
out.write((char *)(&a), sizeof(a));
out.close();

}



笔记:C++文件的读取和写入


#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int main(){
char buffer[256];
ifstream myfile ("c:\\a.txt");
ofstream outfile("c:\\b.txt");

if(!myfile){
  cout << "Unable to open myfile";
        exit(1); // terminate with error

}
if(!outfile){
    cout << "Unable to open otfile";
        exit(1); // terminate with error

}
int a,b;
int i=0,j=0;
int data[6][2];
  while (! myfile.eof() )
  {
    myfile.getline (buffer,10);
    sscanf(buffer,"%d %d",&a,&b);
    cout<<a<<" "<<b<<endl;
    data[i][0]=a;
    data[i][1]=b;
    i++;
  }
myfile.close();
for(int k=0;k<i;k++)
{
     outfile<<data[k][0] <<" "<<data[k][1]<<endl;
     cout<<data[k][0] <<" "<<data[k][1]<<endl;
  }

outfile.close();
return 0;
}

 

无论读写都要包含<fstream>头文件

读:从外部文件中将数据读到程序中来处理
对于程序来说,是从外部读入数据,因此定义输入流,即定义输入流对象:ifsteam infile,infile就是输入流对象。
这个对象当中存放即将从文件读入的数据流。假设有名字为myfile.txt的文件,存有两行数字数据,具体方法:
int a,b;
ifstream infile;
infile.open("myfile.txt");      //注意文件的路径
infile>>a>>b;                   //两行数据可以连续读出到变量里
infile.close()

如果是个很大的多行存储的文本型文件可以这么读:
char buf[1024];                //临时保存读取出来的文件内容
string message;
ifstream infile;
infile.open("myfile.js");
if(infile.is_open())          //文件打开成功,说明曾经写入过东西
{
 while(infile.good() && !infile.eof())
 {
   memset(buf,0,1024);
   infile.getline(buf,1204);
   message = buf;
   ......                     //这里可能对message做一些操作
   cout<<message<<endl;
 }
 infile.close();
}

写:将程序中处理后的数据写到文件当中
对程序来说是将数据写出去,即数据离开程序,因此定义输出流对象ofstream outfile,outfile就是输出流对象,这个对象用来存放将要写到文件当中的数据。具体做法:
ofstream outfile;
outfile.open("myfile.bat");  //myfile.bat是存放数据的文件名
if(outfile.is_open())
{
  outfile<<message<<endl;    //message是程序中处理的数据
  outfile.close(); 
}
else
{
  cout<<"不能打开文件!"<<endl;
}


c++对文件的读写操作的例子

/*/从键盘读入一行字符,把其中的字母依次放在磁盘文件fa2.dat中,再把它从磁盘文件读入程序,
将其中的小写字母改成大写字母,再存入磁盘fa3.dat中*/ 
#i nclude<fstream>
#i nclude<iostream>
#i nclude<cmath>
using namespace std;
 //////////////从键盘上读取字符的函数
 void read_save(){
      char c[80];
      ofstream outfile("f1.dat");//以输出方式打开文件
      if(!outfile){
                   cerr<<"open error!"<<endl;//注意是用的是cerr 
                   exit(1);
                   }
          cin.getline(c,80);//从键盘读入一行字符
          for(int i=0;c[i]!=0;i++) //对字符一个一个的处理,直到遇到'/0'为止 
                if(c[i]>=65&&c[i]<=90||c[i]>=97&&c[i]<=122){//保证输入的字符是字符 
                   outfile.put(c[i]);//将字母字符存入磁盘文件 
                   cout<<c[i]<<"";
                   }
                   cout<<endl;
                   outfile.close();
                   }
 void creat_data(){
      char ch;
      ifstream infile("f1.dat",ios::in);//以输入的方式打开文件 
      if(!infile){
                  cerr<<"open error!"<<endl;
                  exit(1);
                  }
    ofstream outfile("f3.dat");//定义输出流f3.dat文件
    if(!outfile){
                 cerr<<"open error!"<<endl;
                 exit(1);
                 }
     while(infile.get(ch)){//当读取字符成功时 
     if(ch<=122&&ch>=97)
     ch=ch-32;
     outfile.put(ch);
     cout<<ch;
     }
     cout<<endl;
     infile.close();
     outfile.close();
     }
     int main(){
         read_save();
         creat_data();
        system("pause");
         return 0;
         }  
 
一些其他的:

C++ 中关于文件的读入和输出(转载)- -

                                      

C++ 中关于文件的读入和输出。
Example : in.txt 文件中读入数据,并输出到out.txt,输出的文件中每行之间有一空行相隔

#include 
#include 
#include 
using namespace std;
int main (int){
     
    string s;
     
    ifstream inf;
     inf.open("in.txt");

     //打开输出文件
    ofstream outf;
    outf.open("out.txt");
    
     
    //
in.txt 文件中读入数据,并输出到out.txt
    /*其中 getline(1,2); 作用是从 inf 指向的文件中
      每次读入一行,把数据存到字符串中,从第一行开始
     每读完一行后,系统自动地把指针指向下一行,不用人为
     干预*/
    while( getline(inf,s ) ){    
        outf << s  << '\n\n';                //
我这里并没有用到字符串
        cout << s  << endl << endl;            //数组,而是只用了一个串
    }                            //S,是因为我每次读入一行
                                //后,立即就把它输出到
                                               

                            //out.txt中,跟着读下一行

    inf.close();
    outf.close();
    return 0;
}

========================================方法二============================================

#include 
#include 
#include 
#include 
using namespace std;
int main (int){
     
    
     
    ifstream inf;
    inf.open("in.txt");
    

    ofstream outf;
    outf.open("out.txt");
    
    /*
这道题有许多解法的,重要的要了它文件输入输出的原理
     你可以一行行地读入,也可以一个字一个字地读入,或一个词
     一个词地读入,整型或浮点型读入,看你定义的是哪种数据类型*/
    
    char c;
    inf >> noskipws;            //
不忽略空白,把每行最后那个'\n'
                                //
也读进来。
    while(inf >>c)
    {
        if (c == '\n'){            //
遇到 '\n' 回车、换行。
            outf << "\n\n";        //输出到文件
            cout << "\n\n";        //输出到屏幕
        }
            
        else{
            outf << c;            //
输出到文件
            cout << c;            //输出到屏幕
        }
    }
    /* 
同样的原理,从文件中读入单个字符,每次读入一个后,
      系统自动地把指针指向下一个字,而不用你指定这次读哪个,
        下次读哪个,除非你不想从第一个开始读,比如说:我想从
        100个字开始读,或者我想读最后50个字。这就需要调用
        相应的函数,并指定相应的位置。*/


    inf.close();
    outf.close();
    return 0;
}

 
 
主  题:  请问VC++,怎么实现读取.txt文件时的行定位,就是读取指定的行的内容.
 
 
   
 

请问VC++,怎么实现读取.txt文件时的行定位,就是读取指定的行的内容.

 

 
 

一行一行的读到你想要的那行。
CStdioFileReadString()方法是读一行。

 
 [VC] 
文件对话框读写文本文件 
2005
 0123 
文件对话框读写文本文件/*************************************************
读文本文件
**************************************************/
//
显示文件打开对话框
CFileDialog dlg(TRUE, "SQL", "*.txt",OFN_HIDEREADONLY
                     |OFN_OVERWRITEPROMPT,"Text Files(*.txt)|*.txt|SQL Files(*.sql)|*.sql|All Files(*.*)|*.*||"); 
if ( dlg.DoModal()!=IDOK ) return;
//
获取文件的绝对路径
CString sFileName=dlg.GetPathName();
//
打开文件
CStdioFile out;
out.Open(sFileName, CFile::modeRead);
CString sSql="",s;
//
读取文件
do{
    out.ReadString(s);
    sSql=sSql+s+"\r\n";
}while ( out.GetPosition()out.Close(); 
//AfxMessageBox(sSql);


/*************************************************
写文本文件
**************************************************/
//
显示文件保存对话框
CFileDialog dlg(FALSE, "SQL", "*.txt",OFN_HIDEREADONLY 
                     | OFN_OVERWRITEPROMPT,"Text Files(*.txt)|*.txt|SQL Files(*.sql)|*.sql|All Files(*.*)|*.*||"); 
if ( dlg.DoModal()!=IDOK ) return;
//
获取文件的绝对路径
CString sFileName=dlg.GetPathName();
CStdioFile out;
//
打开文件
out.Open(sFileName, CFile::modeCreate | CFile::modeWrite);
//
保存文件
CString sSql="文本文件内容";
out.WriteString(sSql);
out.Close();
 
我要读一个文件,但是我想让一行一行的读,而且每行有不一定长度的,应该怎么做呢?
(cqwally
发表于2001-8-9 23:37:43)

1)
程序如下:
{

   .....................
   CStdioFile file;
   CString str;
   file.Open("test.txt",CFile::modeRead,NULL);
   file.ReadString(str);
   MessageBox(str);//
第一行
   file.ReadString(str);
   MessageBox(str);//
第二行
.....................
}

看看两次显示的有什么不同。test.txt文件如下:
第一行
第二行


2)
我用CFile::Write,怎样才可以写入回车,空格?而且我要写两栏要对齐?
回车换行:"\r\n"

如何正确的使用CFile::Open中的参数?
(taxiblackangel
发表于2001-8-14 10:58:16)

  [问题提出]
  
我设计了一个从记事本中读数据的程序。将数据显示在视中。
  代码如下:

  void CTry1View::OnShow() 
  {  
    // TODO: Add your command handler code here
    CStdioFile file;
     
    CString filename;
    CFileDialog opendlg(true,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,".(*.txt)|*.txt|All Files(*.*)|*.*||",NULL);
    if(opendlg.DoModal()==IDOK)
        filename=opendlg.GetPathName();
    if(file.Open(filename,CFile::modeCreate|CFile::modeReadWrite|CFile::typeText)==0)
    {

        AfxMessageBox("error");
        return;
    } 
                 
        while(file.ReadString(string))
                 {
                     strList.AddTail(string);
         string.ReleaseBuffer();
                 }
         
        flag=true;
        Invalidate();
  }
  
结果不但在视中没有任何显示,而且记事本中的数据也全部丢失。变成了一片空白。真是搞不懂了。
  记事本中的数据是我随便写的。如下:
  11
  222
  3333
  44444
  .......

  [解决方法]
  
file.Open(filename,CFile::modeCreate|CFile::modeReadWrite|CFile::typeText),CFile::modeCreate去掉,modeCreate的意思是没有此文件就建立,有了此文件,清空文件.


最新评论 [发表评论]  查看所有评论  推荐给好友  打印

 最好是这样file.Open(filename,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeReadWrite|CFile::typeText),因为CFile::modeNoTruncate意思是文件即使存在也不清空。 ( lichihang 发表于 2003-12-29 8:33:00)

如何使用CStdioFile::ReadString来读文件?
(yello2000i
发表于2001-8-16 17:04:39)

  [问题提出]
  
一数据文件一行一条记录,我用file.readstring()一次读一行,并对读取的数据做一些处理;
  
请问:while(文件还没到结尾
  {
  file.readstring();
  ...
  }
  ??
文件还没到结尾如何判断?
  
如果,到了指定位置不读了,过一会儿再读又如何做?
  [
解决方法]
  while()
中不用另加判断条件了,因为CStdioFile::ReadString()本身就是判断标志,若没有了(文件到头)返回NULL,因此:while(file.ReadString(s)){}就可.
  [
程序实现]
  
假设你已有了名为ts.txt的文件在你的工程目录下:
  {
   CStdioFile file;   
   CString sss;
   char ccc[100];
   DWORD o=0;
   int ol=0;
   file.Open("ts.txt",CFile::modeRead);
   while(file.ReadString(sss))
   {
      ol++;
      if(ol>1)//
读两次就不读了.
         break;      
      
   }
   o=file.GetPosition();//
记录上次的结果(读到哪了)
   .................
   file.Seek(o,0);//
接着上回读
   while(file.ReadString(sss))
   {
      strcpy(ccc,sss);
      AfxMessageBox(ccc);
   }
  }


 这样的命名叫人看了好难受的  又是o
  
又是0
我觉得要测试的字符就是"\n, \r, \r\n, or EOF. "
就可以了
( wshust 发表于 2004-3-4 21:28:00)

 上面lichihang,我试了一下,事实并不是如你所说的那样读到空行就停了,而是一直读下去呀!! ( pamir 发表于2004-2-9 8:31:00)

 我不同意这个回答!
因为CStdioFile::ReadString()返回NULL的条件是遇到文件尾或者一个空行,所以如果说txt文件的某一条记录后面连续出现了若干条空行,那么CStdioFile::ReadString()函数也会返回NULL,这样文件并没有读取完毕! ( lichihang 发表于 2003-12-29 8:26:00)


删除目录及目录下所有文件与子目录
(Hermess发表于2002-5-24 22:10:27)

  VC++只提供了删除一个空目录的函数,而在实际应用中往往希望删除其下有很多子目录与文件的目录。为了实现这一功能,我编写了DeleteDirectory 函数,它可以实现这一功能。

函数原型:BOOL DeleteDirectory(char *DirName);
返回值:成功删除时返回TRUE,否则返回FALSE
参数DirName为要删除的目录名,必须为绝对路径名,如“c:\\temp"

函数定义如下:
BOOL DeleteDirectory(char *DirName)
{
   CFileFind tempFind;
   char tempFileFind[200];
   sprintf(tempFileFind,"%s\\*.*",DirName);
   BOOL IsFinded=(BOOL)tempFind.FindFile(tempFileFind);
   while(IsFinded)
   {
      IsFinded=(BOOL)tempFind.FindNextFile();
      if(!tempFind.IsDots())
      {
         char foundFileName[200];
         strcpy(foundFileName,tempFind.GetFileName().GetBuffer(200));
         if(tempFind.IsDirectory())
         {
            char tempDir[200];
            sprintf(tempDir,"%s\\%s",DirName,foundFileName);
            DeleteDirectory(tempDir);
         }
         else
         {
            char tempFileName[200];
            sprintf(tempFileName,"%s\\%s",DirName,foundFileName);
            DeleteFile(tempFileName);
         }
      }
   }
   tempFind.Close();
   if(!RemovwDirctory(DirName))
   {
      MessageBox(0,"
删除目录失败!","警告信息",MK_OK);
      return FALSE;
   }
   return TRUE;

 
搜集--- 关于按行读取”“查找特定字符串”“替换字符串操作
气死了,刚刚误操作,画了一半的表格全都没了。哼哼,没有好兴致说些有趣的话了,爱看不看,爱听不听。气死了!偏偏还要在网页上写,还要不保存!

不说上午的事情了,

直奔主题气死!

关键词:文本文件按行读取查找特定字符串替换字符串

                    C/C++                                                            VC/MFC

 按行:fgets();getline();                   StdioFile::ReadString(buf)CArchive::ReadString(CString &str)

 查找:strstr();_tcschr();fseek()          buf.Find(str)bufCString类型的)

 替换:替换字符串

代码:

CStringFile CLASS


我用fopen打开一个txt文件,想查找某一字符串,找到这个字符串后,再读取它对应行的

其他数据,请教大侠指点小弟如何去做,用什么函数。谢谢。

 

. fopen过后,逐行读入str,  strstr 判断是否含有特定字符串例子:

   FILE *fd=fopen("你要打开的文件","r+");

   char tmpLine[200];

   while(fgets(tmpLine,200,fd)!=NULL)

   {

       if( strstr(tmpLine,"你要找的字符串"))

        {

           printf("find the string!\n");

           break;

         }

       memset(tmpLine,0,200);

    }

   free(tmpLine);

 

//从指定文件中提取指定字符串

CStrOP& CStrOP::GetSpecifyString(LPCTSTR szSpec, CArchive *ar, BOOL bMid, int iMid, int *pRow, int *pCol)

{

    do

    {

        //读取文件内容

        if(ar)

        {

            //判断是否到了文件最末

            if(!ar->ReadString(*this))

            {

                //未找到找到字符串

                if(pRow)

                {

                    *pRow = 0;

                }

                if(pCol)

                {

                    *pCol = 0;

                }

                (*this).Format("%s", "");

                break;             

            }

        }

        //获得需要查找的字符串所在行

        if(pRow)

        {

            (*pRow)++;

        }

        //去掉空白字符

        TrimLeft();

        TrimRight();

        //判断是否已经找到需要的字串

        if(Find(szSpec) >= 0)

        {

            //获得需要查找的字符串所在列

            if(pCol)

            {

                *pCol = Find(szSpec) + 1;

            }

            //判断是否需要提取字符串

            if(bMid)

            {

            //判断提取字符串的规则,如果iMid为-1,则不论字串在任何位置,都可以

            //进行提取;否则,字串必须在指定位置iMid时才可以提取

                if(iMid == -1)

                {

                    (*this).Format("%s", Mid(Find(szSpec) + lstrlen(szSpec)));

                    break;

                }

                else if(iMid >= 0)

                {

                    if(Find(szSpec) == iMid)

                    {

                        (*this).Format("%s", Mid(iMid + lstrlen(szSpec)));

                        break;

                    }

                }

            }

        }

        else

        {

            if(!ar)

            {

                //未找到找到字符串

                if(pRow)

                {

                    *pRow = 0;

                }

                if(pCol)

                {

                    *pCol = 0;

                }

                (*this).Format("%s", "");

                break;

            }

        }

    } while(1);

 

    return *this;

}

其中CStrOP是我新建的类,函数声明为

CStrOP& GetSpecifyString(LPCTSTR szSpec, CArchive *ar = NULL, BOOL bMid = TRUE, int iMid = 0, int* pRow = NULL, int* pCol = NULL);

这个函数能实现在指定文本格式的文件中进行特定字符串的查找、提取、定位等工作,其中参数CArchive ar初始化为:

CFile filecMyTxtFileCFile::modeRead

CArchive ar(&file, CArchive::load)


0 0
原创粉丝点击