파일입출력 ㅠㅠ
#include
#include
void Fileoutput();
void Fileread();
void Showresult();
int main()
{
Fileoutput();
Fileread();
Showresult();
return 0;
}
void Fileoutput()
{
FILE * fp=fopen("text.txt", "rt");
char read[20];
if(fp==NULL)
puts("파일오픈 실패!");
while(feof(fp))
{
while(fgets(read, sizeof(read), fp)!=NULL)
{
fgets(read,sizeof(read),fp);
printf("%s\n", read);
}
}
fclose(fp);
}
void Fileread()
{
FILE *src=fopen("text.txt", "rt");
FILE *des=fopen("output.txt", "wt");
char read[500];
if(src==NULL || des==NULL)
puts("파일오픈 실패!");
while(fgets(read, sizeof(read), src)!=NULL)
fputs(read,des);
}
void Showresult()
{
FILE * fp=fopen("output.txt", "rt");
char read[500];
double result[10];
int i;
int resultcnt=0;
int resultnum=0;
int len=0;
if(fp==NULL)
{
puts("파일오픈 실패!");
}
while(feof(fp))
{
fgets(read,sizeof(read), fp);
for(i=0;read[i]!=NULL;i++)
{
if(read[i]=='#')
resultnum++;
}
len=strlen(read);
result[resultcnt]=len-resultnum/100.00;
resultnum=0;
resultcnt++;
}
resultcnt=0;
while(result[resultcnt]!='\0');
{
printf("%d.Efficiency ratio is %.2f % \n", (resultcnt+1), result[resultcnt]);
resultcnt++;
}
fclose(fp);
}
소스가 이런데요
실행하면 아무것도 뜨지 않네요..
첫번째 함수는 파일을 열고 문자열 단위로 출력하는 겁니다
두번째 함수는 파일을 복사하는 것이고요
세번째 함수는 복사한 파일을 읽어서 문자열의 길이를 알고 그 문자열 안에서 #의 갯수를 알아내어 퍼센트 게이지를 얻는 함수입니다..
어디를 고치고 추가해야 할지 도와주세요


이거 참고 해보세요.
//http://www.acm.uiuc.edu/webmonkeys/book/c_guide/ #include <windows.h> #include <stdio.h> void Fileoutput (); void Fileread (); void Showresult (); int main () { Fileoutput (); Fileread (); Showresult (); system("pause"); return 0; } //첫번째 함수는 파일을 열고 문자열 단위로 출력하는 겁니다 void Fileoutput () { char read[20]; char* pchar = NULL; //FILE *fopen(const char *filename, const char *mode); //On success a pointer to the file stream is returned. On failure a null pointer is returned. FILE * fp = fopen ("text.txt", "rt"); if (fp == NULL) { puts ("파일오픈 실패!"); return ; } while (1) { //http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.12.html#feof //int feof(FILE *stream); //Tests the end-of-file indicator for the given stream. //If the stream is at the end-of-file, then it returns a nonzero value. //If it is not at the end of the file, then it returns zero. if(feof (fp) != 0) break; // memset(read, 0x00, sizeof(read)); //char *fgets(char *str, int n, FILE *stream); //Reads a line from the specified stream and stores it into the string pointed to by str. //It stops when either (n-1) characters are read, the newline character is read, //or the end-of-file is reached, whichever comes first. //The newline character is copied to the string. //A null character is appended to the end of the string. //On success a pointer to the string is returned. On error a null pointer is returned. //If the end-of-file occurs before any characters have been read, the string remains unchanged. pchar = fgets (read, sizeof (read), fp); if (pchar == NULL) { fclose (fp); return ; } printf ("%s", read); } fclose (fp); } //두번째 함수는 파일을 복사하는 것이고요 void Fileread () { char read[500]; char* pchar = NULL; FILE * fp_src = fopen ("text.txt", "rt"); FILE * fp_des = fopen ("output.txt", "wt"); if (fp_src == NULL) { puts ("src 읽기 파일 열기 실패!"); return ; } if (fp_des == NULL) { fclose (fp_src); puts ("des 쓰기 파일 열기 실패!"); return ; } while (1) { if(feof (fp_src) != 0) break; // memset(read, 0x00, sizeof(read)); // pchar = fgets (read, sizeof (read), fp_src); if (pchar == NULL) { fclose (fp_src); fclose (fp_des); return ; } fputs (read, fp_des); printf ("%s", read); } fclose (fp_src); fclose (fp_des); } //세번째 함수는 복사한 파일을 읽어서 문자열의 길이를 알고 그 문자열 안에서 #의 갯수를 알아내어 퍼센트 게이지를 얻는 함수입니다.. void Showresult () { int i = 0; int len = 0; int resultcnt = 0; int resultnum = 0; double value = 0.0; double result[10]; char read[500]; char* pchar = NULL; FILE * fp = fopen ("output.txt", "rt"); if (fp == NULL) { puts ("파일오픈 실패!"); return ; } while (1) { if(feof (fp) != 0) break; // pchar = fgets (read, sizeof (read), fp); if (pchar == NULL) { fclose (fp); return ; } // for (i = 0; read[i] != NULL; i++) { if (read[i] == '#') resultnum++; } len = strlen (read); value = (double)(len - resultnum); if(value == 0.0) { result[resultcnt] = 0.0; } else { result[resultcnt] = value / 100.00; } resultnum = 0; resultcnt++; } resultcnt = 0; while (result[resultcnt] != '\0'); { printf ("%d.Efficiency ratio is %.2f % \n", (resultcnt + 1), result[resultcnt]); resultcnt++; } fclose (fp); }----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.
매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.
각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com
댓글 달기