1. 程式人生 > >c++中讀取檔案內容存入到另一個檔案

c++中讀取檔案內容存入到另一個檔案

使用fstream庫中函式讀取檔案內容並存入到另一個檔案中的demo。

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <iostream>
#include <unistd.h>
#include <fstream>
#define MASK_FILE_PATH "./i2clog.csv"
#define OUTPUT_FILE_PATH "./i2cnew.csv"
using namespace
std; //讀取mask string cfgtemp; fstream cfgfile; ofstream i2clog; int main() { cfgfile.open(MASK_FILE_PATH,ios::in); if (!cfgfile.is_open()) { cout << "Can not find mask file,system will work as No Mask Mode." << endl; return 0; } i2clog.open(OUTPUT_FILE_PATH,ios::app); //讀取mask
int i; while (!cfgfile.eof() ) { cfgtemp = "";//清空快取 /* for(i = 0; i < 1300 ; i++) { //使用這個/* */中是讓其每1300行讀取一次並賦值 cfgfile >> cfgtemp; }*/ cfgfile >> cfgtemp;//檔案中取一行資料 i2clog << cfgtemp << '\n';//寫入一行資料到檔案 } i2clog.close(); cfgfile.close(); return
0; }