C++中对文件操作的一个例子

C++中对文件操作的一个例子
//  FileOp.cpp : Defines the entry point for the console application.
//

#include 
" stdafx.h "

// 要用ifstream与ofstream进行文件的输入与输出操作,必须包含此头文件
#include  < fstream >
#include 
< iostream >
#include 
< stdio.h > // FILE需要



int  _tmain( int  argc, _TCHAR *  argv[])
{
    
// 技术扶自:  http://www.mini188.com/showtopic-954.aspx

/* ********************************************************************** */
/* * 输入文件流 ofstream
/***********************************************************************
*/
    
/*
    //声明文件输入的操作对象(输出流对象)
    std::ofstream fout;
    //打开一个文件,提示:如果Output.txt文件不存在,则系统会自动为你创建一个
    fout.open("Output.txt");
    //对文件进行写操作。
    fout << "This is a first line." << "\n";
    fout << "This is a second line." << "\n";
    
    int num = 150;
    char name[] = "John Doe";
    fout << "Here is a number: " << num << "\n";
    fout << "Now here is a string: " << name << "\n";

    //将文件流内容保存到硬盘
    fout.flush();
    //关闭文件流
    fout.close();
    
*/


/* ********************************************************************** */
/* * 输入文件流 ifstream  12 GameDev 15.45 L This is really awesome!
/***********************************************************************
*/
    
/*
    std::ifstream fin("Input.txt");
    
    int number;
    float real;
    char letter, word[8];
    fin >> number >> word >> real >> letter;
    char sentence[1000];
    fin.getline(sentence, 1000);

    fin.close();
    
*/


/* ********************************************************************** */
/* * 文件流操作 fstream
/***********************************************************************
*/
//      std::fstream fFile;
//      fFile.open("Output.txt", std::ios::in | std::ios::out);
    
// fFile.open("无线iPhone平台开发基础培训交流圈.jpg");     // 经测试,非文本文件资源是打不开的。
    
    
// 将整个文件的内容读取出来,并显示
    
// 注意:用这种方法读出来的,都是忽略空格与换行符的
//      if (fFile.is_open())
//      {
//          char letter;
//           // while (fFile.good() && !fFile.eof())
//          while (!fFile.eof())     // 用这个与上面那个都是一样的效果
//          {
//              fFile >> letter;
//              if (fFile.eof())
//                  break;
//              std::cout << letter << std::endl;
//          }
//          getchar();
//      }


    
// 注意:用这种方法读限出来的,都是没忽略末尾的换行符的
//      if (fFile.is_open())
//      {
//          char line[2048];    
//          while (fFile.good() && !fFile.eof())
//          {
//              fFile.getline(line, 2048);
//              static int count = 0;
//              if (count < 3)
//              {
//                  count += 1;
//                  fFile.seekg(0, std::ios::beg);     // 这个是改变读的指针位置。如果是想改变写的指针位置用fFile.seekp(0, std::ios::beg/end);
//              }
//              std::cout << line << std::endl;
//          }
//  
//           // 将第一行的字符串改:"The first line string is changed."
//          fFile.seekp(0, std::ios::beg);
//           // fFile << "The first line string is changed.";     // 写内容不是这样写的。如果是ofstream可以这么写。但对于fstream需要用下面的方法来写。测试结果发现,仍是写不进去
//           // char* pszTempForWrite = "The first line string is changed.";
//           // fFile.write(pszTempForWrite, strlen(pszTempForWrite));
//          fFile.seekg(0, std::ios::beg);
//          fFile.getline(line, 2048);
//          std::cout << line << std::endl;
//          getchar();
//      }

//       // * fstream的其他一些方法
//       // read方法
//      char* pszOutputFileText = NULL;
//      fFile.seekg(0, std::ios::end);
//      int nSize;
//      nSize = fFile.tellg();
//       // std::cout << fFile.tellg() << std::endl;
//       // 用read方法一次性将整文件给读取出来(注意:这些读出来后,末性居然会带了一个乱码。这个郁闷。)
//      pszOutputFileText = new char[nSize + 1];
//      fFile.seekg(0, std::ios::beg);
//      fFile.read(pszOutputFileText, nSize);
//      pszOutputFileText[nSize] = '\0';
//      std::cout << pszOutputFileText << std::endl;
//      delete [] pszOutputFileText;
//      getchar();
//  
//      fFile.close();


/* ********************************************************************** */
/* * 二进制文件的读写
/***********************************************************************
*/


/* ********************************************************************** */
/* * 字符串长度
/***********************************************************************
*/
//      char* pszString = "Hello";
//      std::cout << strlen(pszString) << std::endl;     // 输出5
//  
//      std::string sString = "Hello";
//      std::cout << strlen(sString.c_str()) << std::endl; // 输出5
//  
//      char szTest[5] = { 'H', 'e', 'l', 'l', 'o' };
//      std::cout << szTest[5] << std::endl;     // 越界了,所以会报。
//      getchar();


/* ********************************************************************** */
/* * 使用FILE类对文件进行操作 (FILE在stdio.h中
/***********************************************************************
*/
    FILE
*  materialFile  =  fopen( " DefaultObjectStd.material " " r " );
    
if  (materialFile  ==  NULL)
    {
        std::cout 
<<   " Open the file \ " DefaultObjectStd.material\ "  failure. "   <<  std::endl;
        
return   0 ;
    }

    
const   int  MAX_COUNT  = 2048 ;
    
char  everyline[MAX_COUNT]  =  {  ' \0 '  };
    
while  (fgets(everyline, MAX_COUNT, materialFile))
    {
        std::cout 
<<  everyline;     // 注意:通过fgets()函数读取出来的定符串,末尾是带有换行符的。这与上面的是不一样的。
    }

    
// 取得文件的长度(即:文件的大小)
     int  nMaterialFileSize;
    fseek(materialFile, 
0 , SEEK_END);
    nMaterialFileSize 
=  ftell(materialFile);
    std::cout 
<<  std::endl  <<  nMaterialFileSize  <<  std::endl;
    
    getchar();

    fclose(materialFile);
    

    
return   0 ;
}

你可能感兴趣的:(C++中对文件操作的一个例子)