File Read Write

Reads information from the file and outputs it onto the screen

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

int main ()
{
   char data[80];
   ofstream outfile;
   outfile.open("file.dat");
   cout << "Writing to the file" << endl;
   cout << "Enter your name: "; 
   cin.getline(data, 80);
   outfile << data << endl;
   cout << "Enter your id: "; 
   cin >> data;
   cin.ignore();
   outfile << data << endl;
   outfile.close();
   ifstream infile; 
   cout << "Reading from the file" << endl; 

   infile.open("file.dat"); 
   infile >> data; 
   cout << data << endl; 
   infile >> data; 
   cout << data << endl; 
   infile.close();
   return 0;
}
Another example of read() and write() and illustrates the use of gcount( )
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
  double doubleNumberArray[4] = {99.75, -34.4, 1776.0, 200.1};
  int i;
  ofstream out("numbers", ios::out | ios::binary);
  if(!out) {
    cout << "Cannot open file.";
    return 1;
   }
  out.write((char *) &doubleNumberArray, sizeof doubleNumberArray);
  out.close();
  for(i=0; i<4; i++) // clear array
    doubleNumberArray[i] = 0.0;

  ifstream in("numbers", ios::in | ios::binary);
  in.read((char *) &doubleNumberArray, sizeof doubleNumberArray);
  
  cout << in.gcount() << " bytes read\n"; // see how many bytes have been read
  for(i=0; i<4; i++) // show values read from file
     cout << doubleNumberArray[i] << " ";
  in.close();
  return 0;
}

Opening Files for Read and Write

#include <fstream>
#include <iostream>


using namespace std;
int main()
{
   char buffer[255];    

   ofstream fout("text.txt");  
   fout << "This line written directly to the file...\n";
   cout << "Enter text for the file: ";
   cin.ignore(1,'\n');
   cin.getline(buffer,255);

   fout << buffer << "\n";
   fout.close();         

   ifstream fin("text.txt");    
   char ch;
   while (fin.get(ch))
      cout << ch;

   fin.close();
   return 0;
}
  
    

Use get() and getline() to read characters.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
  char ch;
  char str[256];

  ofstream fout("test.dat");
  if(!fout) {
    cout << "Cannot open file for output.\n";
    return 1;
  }

  fout << "This is a line of text.\n";
  fout << "This is another line of text.\n";
  fout << "This is the last line of text.\n";

  fout.close();
  if(!fout.good()) {
    cout << "An error occurred when writing to the file.\n";
    return 1;
  }

  ifstream fin("test.dat", ios::in);
  if(!fin) {
    cout << "Cannot open file for input.\n";
    return 1;
  }

  cout << "Use get():\n";

  cout << "Here are the first three characters: ";
  for(int i=0; i < 3; ++i) {
    fin.get(ch);
    cout << ch;
  }
  cout << endl;

  fin.get(str, 255);
  cout << "Here is the rest of the first line: ";
  cout << str << endl;

  fin.get(ch);

  cout << "\nNow use getline():\n";

  fin.getline(str, 255);
  cout << str << endl;
  fin.getline(str, 255);
  cout << str;

  fin.close();
  if(!fin.good()) {
    cout << "Error occurred while reading or closing the file.\n";
    return 1;
  }

  return 0;
}
  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
// zuoye07.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "zuoye07.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif DWORD BufferSize=1024; char buf[1024]; / // The one and only application object CWinApp theApp; using namespace std; void FileReadWrite_NoBuffer(char*source,char*destination); int _tmain(int argc,TCHAR*angv[],TCHAR*envp[]) { int nRetCode=0; printf("Call FileReadWrite_NoBuffer!\n"); //调用FileReadWrite_NoBuffer(char*source,char*destination)函数 FileReadWrite_NoBuffer("source.txt","nobuffer.txt"); return nRetCode; } void FileReadWrite_NoBuffer(char*source,char*destination) { HANDLE handle_src,handle_dst; DWORD NumberOfByteWrite; bool cycle; char*buffer; buffer=buf; //创建文件source.txt handle_src=CreateFile(source, GENERIC_READ, 0, NULL, OPEN_ALWAYS, FILE_FLAG_NO_BUFFERING, NULL); //创建文件nobuffer.txt handle_dst=CreateFile(destination, GENERIC_WRITE, NULL, NULL, OPEN_ALWAYS, NULL, NULL); //判断文件是否创建失败,若失败打印输出提示信息并退出 if(handle_src==INVALID_HANDLE_VALUE || handle_dst==INVALID_HANDLE_VALUE) { printf("File Create Fail!\n"); exit(1); } cycle=true; while(cycle) { NumberOfByteWrite=BufferSize; //读取文件source.txt if(!ReadFile(handle_src,buffer,NumberOfByteWrite,&NumberOfByteWrite,NULL)) { //读取文件source.txt失败 printf("Read File Error!%d\n",GetLastError()); exit(1); } if(NumberOfByteWrite<BufferSize)cycle=false; //写入文件nobuffer.txt if(!WriteFile(handle_dst,buffer,NumberOfByteWrite,&NumberOfByteWrite,NULL)) { //写入文件nobuffer.txt失败 printf("Write File Error!%d\n",GetLastError()); exit(1); } } //关闭文件句柄(source.txt,nobuffer.txt) CloseHandle(handle_src); CloseHandle(handle_dst); }
最新发布
06-09

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值