// 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;
}