# Tìm thư viện mà mở file không có tồn tại thì tạo file

**URL:** https://daynhauhoc.com/t/tim-thu-vien-ma-mo-file-khong-co-ton-tai-thi-tao-file/105680
**Category:** programming
**Tags:** c++
**Created:** [July 1, 2020, 12:02pm UTC](https://daynhauhoc.com/t/tim-thu-vien-ma-mo-file-khong-co-ton-tai-thi-tao-file/105680 "2020-07-01T12:02:00Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Kiet\_Hoang1](https://daynhauhoc.com/user_avatar/daynhauhoc.com/kiet_hoang1/32/50691_2.png) [@Kiet\_Hoang1](https://daynhauhoc.com/u/Kiet_Hoang1)
#### Post date: [July 1, 2020, 12:02pm UTC](https://daynhauhoc.com/t/tim-thu-vien-ma-mo-file-khong-co-ton-tai-thi-tao-file/105680/1 "2020-07-01T12:02:00Z")

</div>

Có thư viện nào mà mở file không có thì tạo file mà không làm tác dụng phụ khác không?

```auto
#include <vector>
#include <iostream>

#include<stdlib.h>
#include<malloc.h>
#include<stdio.h>
#include<fstream>
#include<cstdio>

using namespace std;

typedef vector<int> intArray;

void main() {
#if 0
int i, m, n;
vector<intArray> a;
cout << "Nhap so luong dong, cot: "<<"\n\n";
cout<<"Nhap so luong dong : ";
cin>>m;
cout<<"Nhap so luong cot : ";
cin>>n;
a.resize(m);//resize là thay đổi kích thước mảng
for (i = 0; i < m; i++)
	a[i].resize(n);
#endif
ifstream f;

f.open("D:\hoangkiet1.txt",ios::app);
if (f == NULL)
printf("Khong mo duoc tap tin!\n\n");
else
{
	cout<<"mo duoc tap tin!"<<"\n\n";
}

f.read("D:\input.txt", 10);

//free(p);
system("pause");
}

```

---

<div class="post-metadata">

### Author: ![noname00](https://daynhauhoc.com/user_avatar/daynhauhoc.com/noname00/32/40596_2.png) [@noname00](https://daynhauhoc.com/u/noname00)
#### Post date: [July 1, 2020, 12:12pm UTC](https://daynhauhoc.com/t/tim-thu-vien-ma-mo-file-khong-co-ton-tai-thi-tao-file/105680/2 "2020-07-01T12:12:31Z")

</div>

**Xem lại quy tắc đăng code lên DNH tại [đây](https://daynhauhoc.com/t/112).**

* * *

Một chút if else thôi mà, cần gì tìm thư viện.

```cpp
#include <string>
#include <fstream>
#include <iostream>
 
int main()
{
    std::string filename = "example.123";
 
    std::fstream fs;
 
    fs.open(filename);
 
    if(!fs.is_open())
    {
       fs.clear();
       fs.open(filename, std::ios::out); //Create file.
       fs.close();
       fs.open(filename);
    }
 
    std::cout << std::boolalpha;
    std::cout << "fs.is_open() = " << fs.is_open() << '\n';
    std::cout << "fs.good() = " << fs.good() << '\n';
}

```

[src](https://en.cppreference.com/w/cpp/io/basic_fstream/open)

---

<div class="post-metadata">

### Author: ![Kiet\_Hoang1](https://daynhauhoc.com/user_avatar/daynhauhoc.com/kiet_hoang1/32/50691_2.png) [@Kiet\_Hoang1](https://daynhauhoc.com/u/Kiet_Hoang1)
#### Post date: [July 2, 2020, 1:38am UTC](https://daynhauhoc.com/t/tim-thu-vien-ma-mo-file-khong-co-ton-tai-thi-tao-file/105680/3 "2020-07-02T01:38:18Z")

</div>

ủa ios::out là mở file có sẵn trở đi

---

<div class="post-metadata">

### Author: ![Kiet\_Hoang1](https://daynhauhoc.com/user_avatar/daynhauhoc.com/kiet_hoang1/32/50691_2.png) [@Kiet\_Hoang1](https://daynhauhoc.com/u/Kiet_Hoang1)
#### Post date: [July 2, 2020, 1:45am UTC](https://daynhauhoc.com/t/tim-thu-vien-ma-mo-file-khong-co-ton-tai-thi-tao-file/105680/4 "2020-07-02T01:45:51Z")

</div>

cảm ơn anh 😁 😁 😁 😁 😁 😁 😁 😁 😁 😁

---

<div class="post-metadata">

### Author: ![SITUVN.gcd](https://daynhauhoc.com/user_avatar/daynhauhoc.com/situvn.gcd/32/54493_2.png) [@SITUVN.gcd](https://daynhauhoc.com/u/SITUVN.gcd)
#### Post date: [July 2, 2020, 1:50am UTC](https://daynhauhoc.com/t/tim-thu-vien-ma-mo-file-khong-co-ton-tai-thi-tao-file/105680/5 "2020-07-02T01:50:11Z")

</div>

[https://stackoverflow.com/a/18160837/10702045](https://stackoverflow.com/a/18160837/10702045)

> [@](#):
>
> the correct way would be to first create the file, using either ios::out or ios::trunc
> 
> ```auto
> f.open("file.txt". ios::out) or f.open("file.txt", ios::trunc);
> 
> ```

Muốn biết đúng hay sai thì chạy thử.

---

<div class="post-metadata">

### Author: ![Duong\_Act](https://daynhauhoc.com/user_avatar/daynhauhoc.com/duong_act/32/47432_2.png) [@Duong\_Act](https://daynhauhoc.com/u/Duong_Act)
#### Post date: [July 2, 2020, 3:04am UTC](https://daynhauhoc.com/t/tim-thu-vien-ma-mo-file-khong-co-ton-tai-thi-tao-file/105680/6 "2020-07-02T03:04:14Z")

</div>

Thì dùng lớp FILE, mở không được thì nó luôn có mã lỗi trả về. Nếu không có file thì tạo mới.  
Cái này là cái cơ bản hay muốn ăn sẵn hơn nữa ? 🙂
