1. 程式人生 > 其它 >c++讀寫檔案

c++讀寫檔案

一、檔案的輸入輸出
二、從txt檔案中讀取二維陣列(int 以及string)
三、從txt檔案讀取的資料存到struct中

參考部落格:https://blog.csdn.net/u013749068/article/details/78761553

http://www.cnblogs.com/helinsen/archive/2012/07/26/2609251.html

一、檔案的輸入輸出
1.fstream提供了三個類,用來實現c++對檔案的操作。(檔案的建立、讀、寫)。

ifstream :從已有的檔案讀入

ofstream : 向檔案寫內容

fstream : 開啟檔案供讀寫

2.檔案開啟模式:

ios::in 只讀

ios::out 只寫

ios::app 從檔案末尾開始寫,防止丟失檔案中原來就有的內容

ios::binary 二進位制模式

ios::nocreate 開啟一個檔案時,如果檔案不存在,不建立檔案

ios::noreplace 開啟一個檔案時,如果檔案不存在,建立該檔案

ios::trunc 開啟一個檔案,然後清空內容

ios::ate 開啟一個檔案時,將位置移動到檔案尾

3.檔案指標位置的用法

ios::beg 檔案頭

ios::end 檔案尾

ios::cur 當前位置

例子:

file.seekg(0,ios::beg); //讓檔案指標定位到檔案開頭

file.seekg(0,ios::end); //讓檔案指標定位到檔案末尾

file.seekg(10,ios::cur); //讓檔案指標從當前位置向檔案末方向移動10個位元組

file.seekg(-10,ios::cur); //讓檔案指標從當前位置向檔案開始方向移動10個位元組

file.seekg(10,ios::beg); //讓檔案指標定位到離檔案開頭10個位元組的位置

注意:移動的單位是位元組,而不是行。

二、從txt檔案中讀取二維陣列(int以及string)
首先選取“世界女排大獎賽歷屆前三“”資料作為data.txt

其中第1、2列是讀入成int資料,第2~6列資料讀入成string;

由於本人在讀入該資料遇到了些問題,故將data.txt改成如下形式:

檔案由兩部分組成,1~25以及26~50行。

程式碼如下:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
//建立二維陣列分別儲存int及string資料
string str[25][4];
int I[25][2] = {0};
ifstream myfile("data1.txt");
ofstream outfile("out.txt", ios::trunc);

if (!myfile.is_open())
{
cout << "can not open this file" << endl;
return 0;
}
//從data1檔案中讀入int資料
for (int i = 0; i < 25; i++)
{
for (int j = 0; j < 2; j++)
{
myfile >> I[i][j];
}
}
//讀入string資料
for (int i = 25; i < 50; i++)
{
for (int j = 0; j < 4; j++)
{
myfile >> str[i-25][j];
}
}
//將資料輸出至out.txt檔案中
for (int i = 0; i < 25; i++)
{
outfile << I[i][0] << " " << I[i][1] << " "
<< str[i][0] << " " << str[i][1] << " "
<< str[i][2] << " " << str[i][3]<< endl;
};

myfile.close();
outfile.close();
return 0;
}

最終在當前目錄下生成一個out.txt檔案。開啟如下:

三、從txt檔案讀取的資料存到struct中
檔案讀取的過程一樣,多了以下幾步:定義struct Game,定義一個元素 個數為25的結構陣列,寫一個for迴圈初始化結構陣列。

程式碼如下:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

//定義一個結構體
struct Game
{
int num;
int year;
string location;
string first;
string second;
string third;
};

int main()
{
string str[25][4];
int I[25][2] = {0};
ifstream myfile("data1.txt");
ofstream outfile("out.txt", ios::trunc);
//定義一個結構陣列
Game game[25];

//開啟並讀取data1.txt
if (!myfile.is_open())
{
cout << "can not open this file" << endl;
return 0;
}
for (int i = 0; i < 25; i++)
{
for (int j = 0; j < 2; j++)
{
myfile >> I[i][j];
}
}
for (int i = 25; i < 50; i++)
{
for (int j = 0; j < 4; j++)
{
myfile >> str[i-25][j];
}
}

//初始化結構陣列元素
for (int i = 0; i < 25; i++)
{
game[i].num = I[i][0];
game[i].year = I[i][1];
game[i].location = str[i][0];
game[i].first = str[i][1];
game[i].second = str[i][2];
game[i].third = str[i][3];

//寫入outfile物件並控制檯輸出結果
outfile << game[i].num << " " << game[i].year << " " << game[i].location << " " << game[i].first << " "
<< game[i].second << " " << game[i].third << endl;
cout << game[i].num << " " << game[i].year << " " << game[i].location << " " << game[i].first << " "
<<game[i].second<<" "<<game[i].third<<endl;
};

myfile.close();
outfile.close();
return 0;

————————————————
版權宣告:本文為CSDN博主「漸進的冰山」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處連結及本宣告。
原文連結:https://blog.csdn.net/qq_29406323/article/details/81261926