E D R , A S I H C RSS

File Input Output

in C++

~cpp 
#include <fstream>
using namespace std;

int main()
{
	ifstream fin("input.txt"); // fin๊ณผ input.txt๋ฅผ ์—ฐ๊ฒฐ 
	ofstream fout("output.txt"); // fout๊ณผ output.txt๋ฅผ ์—ฐ๊ฒฐ

	int a,b;
	fin >> a >> b; // cin์œผ๋กœ ํ™”๋ฉด์—์„œ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค๋ฉด, fin์€ ์—ฐ๊ฒฐ๋œ ํŒŒ์ผ๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.
	fout << a+b << endl; // cout์œผ๋กœ ํ™”๋ฉด์œผ๋กœ ์ถœ๋ ฅํ•œ๋‹ค๋ฉด, fout์€ ์—ฐ๊ฒฐ๋œ output.txt์— a+b๋ฅผ ์ถœ๋ ฅ
	return 0;
}

input.txt
~cpp 
3 8

์ € ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜๋ฉด output.txt๊ฐ€ ์ƒ๊ธฐ๋ฉด์„œ 11์ด ๊ทธ ์•ˆ์— ์จ์ง„๋‹ค.

Python

~cpp 
fin = file('input.txt')
fout = file('output.txt.','w')

a,b=[int(i) for i in fin.read().split()]
print >> fout,(a+b) #ํ˜น์€ fout.writeline( str(a+b)+'n' )

fin.close()
fout.close()

input.txt
~cpp 
3 8

์ € ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜๋ฉด output.txt๊ฐ€ ์ƒ๊ธฐ๋ฉด์„œ 11์ด ๊ทธ ์•ˆ์— ์จ์ง„๋‹ค.

Java

~cpp 
try { 
String inputString; 
InputStreamReader isr = new InputStreamReader(new FileInputStream(fileName)); 
BufferedReader br = new BufferedReader(isr); 
while((inputString = br.readLine()) != null) { 
      buf = buf + inputString ; 
      buf = buf + "r" ; 
      buf = buf + "n" ; 
       {} br.close(); 
} 
catch (IOException e) 
 System.out.println("Error : "+ e.toString()); {}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:16
Processing time 0.2335 sec