文件I/O

文件I/O 首页 / 函数式入门教程 / 文件I/O

当程序终止时,我们需要文件来存储程序的输出,使用文件File,我们可以使用不同语言的各种命令来访问相关信息。

写入文件

要将内容写入文件,我们首先需要打开所需的文件。如果指定的文件不存在,则将创建一个新文件。

让我们看看如何使用C++将内容写入文件。

#include <iostream> 
#include <fstream> 
using namespace std;  

int main () {   
   ofstream myfile; 
   myfile.open ("Tempfile.txt", ios::out); 
   myfile << "Writing Contents to file.\n"; 
   cout << "Data inserted into file"; 
   myfile.close(); 
   return 0; 
} 

注意-

  • fstream    - 是用于控制文件读/写操作的Stream流。

  • ofstream  - 是用于将内容写入文件的Stream流。

让我们看看如何使用Erlang(一种函数性编程语言)将内容写入文件。

-module(helloworld).  
-export([start/0]).   

start() ->
   {ok, File1} = file:open("Tempfile.txt", [write]),  
   file:write(File1,"Writting contents to file"), 
   io:fwrite("Hello learnfk.com\n"). 

注意-

输出  -  当我们运行此代码时,"将内容写入文件"将被写入文件 Tempfile.txt ,如果文件具有任何现有内容,则它将被覆盖。

从文件读取

要读取文件,首先我们必须以读取模式打开指定的文件,如果文件不存在,则其各自的方法将返回NULL。

以下程序显示了如何在 C ++ 中读取文件的内容-

#include <iostream> 
#include <fstream> 
#include <string> 
using namespace std;  

int main () {
   string readfile; 
   ifstream myfile ("Tempfile.txt",ios::in); 
   
   if (myfile.is_open()) {     
      while ( getline (myfile,readfile) ) {       
         cout << readfile << '\n'; 
      } 
      myfile.close(); 
   } else  
      cout << "file doesn't exist";  
   return 0; 
} 

它将产生以下输出-

Hello learnfk.com

注意  - 在此程序中,我们使用“ ios::in”以读取模式打开了文本文件,然后在屏幕上打印其内容。我们使用while循环通过“ getline”方法逐行读取文件内容。

以下程序显示了如何使用 Erlang 执行相同的操作,在这里,我们将使用 read_file(filename)方法从指定文件中读取所有内容。

-module(helloworld).  
-export([start/0]).   

start() ->  
   rdfile = file:read_file("Tempfile.txt"),  
   io:fwrite("~p~n",[rdfile]). 

它将产生以下输出-

ok, L contents to file 

删除现有文件

我们可以使用文件操作删除现有文件,以下程序显示了如何使用C ++删除现有文件 -

#include <stdio.h> 

int main () {   
   if(remove( "Tempfile.txt" ) != 0 ) 
      perror( "File doesn't exist, can't delete" ); 
   else 
      puts( "file deleted successfully " ); 
   return 0; 
}   

它将产生以下输出-

file deleted successfully 

以下程序显示了如何在 Erlang 中执行相同的操作,在这里,我们将使用方法 delete(filename)删除现有文件。

-module(helloworld).  
-export([start/0]).   

start() ->  
   file:delete("Tempfile.txt"). 

输出-如果文件" Tempfile.txt"存在,则将其删除。

确定文件大小

以下程序显示了如何使用C ++确定文件的大小。在这里,函数 fseek 将与流关联的位置指示符设置为新位置,而 ftell 返回Stream流中的当前位置。

#include <stdio.h> 

int main () {  
   FILE * checkfile; 
   long size; 
   checkfile = fopen ("Tempfile.txt","rb"); 
   
   if (checkfile == NULL)  
      perror ("file can't open"); 
   else {   
      fseek (checkfile, 0, SEEK_END);    //non-portable 
      size = ftell (checkfile); 
      fclose (checkfile); 
      printf ("Size of Tempfile.txt: %ld bytes.\n",size); 
   } 
   return 0; 
}    

输出-如果文件" Tempfile.txt"存在,它将以字节为单位显示其大小。

以下程序显示了如何在Erlang中执行相同的操作。在这里,我们将使用方法 file_size(filename)确定文件的大小。

-module(helloworld).  
-export([start/0]).   

start() ->  
   io:fwrite("~w~n",[filelib:file_size("Tempfile.txt")]). 

输出-如果文件" Tempfile.txt"存在,它将以字节为单位显示其大小。

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

零基础学Python -〔尹会生〕

算法面试通关40讲 -〔覃超〕

DevOps实战笔记 -〔石雪峰〕

互联网人的英语私教课 -〔陈亦峰〕

Flink核心技术与实战 -〔张利兵〕

Spark性能调优实战 -〔吴磊〕

说透5G -〔杨四昌〕

运维监控系统实战笔记 -〔秦晓辉〕

结构执行力 -〔李忠秋〕

好记忆不如烂笔头。留下您的足迹吧 :)