// FileOp.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
//要用ifstream與ofstream進(jìn)行文件的輸入與輸出操作,必須包含此頭文件
#include <fstream>
#include <iostream>
#include <stdio.h>//FILE需要
int _tmain(int argc, _TCHAR* argv[])
{
//技術(shù)扶自: http://www.mini188.com/showtopic-954.aspx
/************************************************************************/
/** 輸入文件流 ofstream
/************************************************************************/
/*
//聲明文件輸入的操作對(duì)象(輸出流對(duì)象)
std::ofstream fout;
//打開一個(gè)文件,提示:如果Output.txt文件不存在,則系統(tǒng)會(huì)自動(dòng)為你創(chuàng)建一個(gè)
fout.open("Output.txt");
//對(duì)文件進(jìn)行寫操作。
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";
//將文件流內(nèi)容保存到硬盤
fout.flush();
//關(guān)閉文件流
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平臺(tái)開發(fā)基礎(chǔ)培訓(xùn)交流圈.jpg"); //經(jīng)測(cè)試,非文本文件資源是打不開的。
//將整個(gè)文件的內(nèi)容讀取出來,并顯示
//注意:用這種方法讀出來的,都是忽略空格與換行符的
// if (fFile.is_open())
// {
// char letter;
// //while (fFile.good() && !fFile.eof())
// while (!fFile.eof()) //用這個(gè)與上面那個(gè)都是一樣的效果
// {
// 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); //這個(gè)是改變讀的指針位置。如果是想改變寫的指針位置用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."; //寫內(nèi)容不是這樣寫的。如果是ofstream可以這么寫。但對(duì)于fstream需要用下面的方法來寫。測(cè)試結(jié)果發(fā)現(xiàn),仍是寫不進(jìn)去
// //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方法一次性將整文件給讀取出來(注意:這些讀出來后,末性居然會(huì)帶了一個(gè)亂碼。這個(gè)郁悶。)
// 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();
/************************************************************************/
/** 二進(jìn)制文件的讀寫
/************************************************************************/
/************************************************************************/
/** 字符串長(zhǎng)度
/************************************************************************/
// 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; //越界了,所以會(huì)報(bào)。
// getchar();
/************************************************************************/
/** 使用FILE類對(duì)文件進(jìn)行操作 (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()函數(shù)讀取出來的定符串,末尾是帶有換行符的。這與上面的是不一樣的。
}
//取得文件的長(zhǎng)度(即:文件的大小)
int nMaterialFileSize;
fseek(materialFile, 0, SEEK_END);
nMaterialFileSize = ftell(materialFile);
std::cout << std::endl << nMaterialFileSize << std::endl;
getchar();
fclose(materialFile);
return 0;
}