///////////
★★★C method 1
///////////
#include <stdio.h>
#include <stdlib.h>

#define INPUT_FILE_1 "hw1_in1"
#define OUTPUT_FILE_1 "hw1_out1"

void ReadFile();
void WriteFile();
void ParseLine(char * pLineBuf);

int main()
{

 printf("\n\n");
 printf("%s : file read\n", INPUT_FILE_1);
 printf("====================================\n");
 
 ReadFile();
 
 printf("\n\n");
 printf("%s : file write\n", OUTPUT_FILE_1);
 printf("====================================\n");
 
 WriteFile();

 printf("\n\n");
 return 0;
}

void ReadFile()
{
 char buf[256];
 FILE * fp;

 fp = fopen(INPUT_FILE_1, "r");
 
 if (fp == NULL)
 {
  printf("%s file open error\n", INPUT_FILE_1);
  exit(0);
 }

 while(!feof(fp))
 {
  fgets(buf, sizeof(buf), fp);

  ParseLine(buf);
 }
 
 fclose(fp);
}

void WriteFile()
{
 int i;
 float f;
 char buf[256];
 FILE * fp;

 fp = fopen(OUTPUT_FILE_1, "w");
 
 if (fp == NULL)
 {
  printf("%s file open error\n", INPUT_FILE_1);
  exit(0);
 }

 fprintf(fp, "---------------------------------\n");
 fprintf(fp, " %s file write test\n", OUTPUT_FILE_1);
 fprintf(fp, "---------------------------------\n");
 
 f = 0.0;
 
 for (i = 0; i < 5; i++)
 {
  fprintf(fp, " %.2f\t%.2f\t%.2f\n", f + 1, f + 1, f + 1);
  f++;
 }
 
 fclose(fp);
}


// 이 함수에서 파싱을 합니다
void ParseLine(char * pLineBuf)
{
 printf("%s", pLineBuf);

}


C++
http://www.cplusplus.com/reference/iostream/ifstream/open.html

this is reference and easy to use

#include

ofstream output;
output.open("filename" ios::out | ios::trunc);


if(output.is_open())
output << "anything if you wanna";

 

'devel > man & example' 카테고리의 다른 글

scanf에서 fflush(stdin) 사용 안하고 \n 파싱해서 없애기  (2) 2011.11.27
function pointer  (0) 2009.11.24
sprintf int to ascii  (0) 2009.11.08
String Parsing  (0) 2008.05.25
bit shift e.g. , 2dim matrix, Defed Func  (0) 2007.05.01
Posted by 쵸코케키

블로그 이미지
chocokeki
쵸코케키

공지사항

Yesterday
Today
Total

달력

 « |  » 2024.5
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

최근에 올라온 글

최근에 달린 댓글

글 보관함