c58.cpp

来自「此文件可以能帮你求体积」· C++ 代码 · 共 89 行

CPP
89
字号
// c58.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>

void Out();
void InWord();
void InLine();
void InChar();

int main(int argc, char* argv[])
{
	Out();
	InWord();
	InLine();
	InChar();

	return 0;
}

void Out()
{
	ofstream outFile( "text.txt" );
	if ( !outFile )
	{
		cout << "error opening text.txt\n";
		exit(1);
	}

	outFile << "How are you!\n";
	outFile << "Find.\n";
}

void InWord()
{
	ifstream inFile( "text.txt" );
	if ( !inFile )
	{
		cout << "error opening text.txt\n";
		exit(1);
	}
	char one[64] = "";
	cout << "以单词形式显示:\n";
	while ( !inFile.eof() )
	{
		inFile >> one;
		cout << one << endl;
	}
}

void InLine()
{
	cout << "以行的形式显示:\n";
	ifstream inFile( "text.txt" );
	if ( !inFile )
	{
		cout << "error opening text.txt\n";
		exit(1);
	}

	char one[80] = "";
	while ( !inFile.eof() )
	{
		inFile.getline( one, sizeof(one) );
		cout << one << endl;
	}
}

void InChar()
{
	cout << "以字符形式显示:\n";
	ifstream inFile( "text.txt" );
	if ( !inFile )
	{
		cout << "error opening text.txt\n";
		exit(1);
	}
	char letter;
	while ( !inFile.eof() )
	{
		inFile.get( letter );
		cout << letter << " ";
	}
	cout << endl;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?