C
FILE* fp;
fp = fopen("파일 이름", "r");
C++
ifstream fp;
fp.open("파일 이름");
접기
#include <iostream> #include <fstream> using namespace std;
int main() { char filename[50]; char str[500]; char temp[20]; char* p; int ch = 0; int word = 0; int line = 0; ifstream fp; cout << "Input the filename : "; cin.getline(filename, 50);
fp.open(filename);
if(fp.fail()) { cout<<"Not Found File"; return 0; }
while(fp.eof()==0) { fp.getline(str,500); p = strtok(str," "); if(p) { strcpy(temp,p); for(int i=0; temp[i] != '\0'; i++) { ch++; } word++; } while(p) { p = strtok(NULL," "); if(p) { strcpy(temp,p); for(int i=0; temp[i] != '\0'; i++) { ch++; } word++; } } line++; } cout<<"charater : "<<ch<<endl; cout<<"word : "<<word<<endl; cout<<"line : "<<line<<endl;
fp.close();
return 0; }
접기
접기
#include <iostream> #include <fstream> #include <string.h> using namespace std;
void del(char *arr, char ch);
int main() { char ch; char a[500]; char *ptr; char em_str[50]; int i = 0; ifstream _in; ofstream _out; _in.open("article_in.txt"); _out.open("article_out.txt");
if (_in.fail()) { cout << "Error!" << endl; return 0; } cout<<"input char : "; cin>>ch; while(_in.eof()==0) { _in.getline(a,500); ptr=strtok(a," "); if(ptr!=NULL) { strcpy(em_str,ptr); del(em_str,ch); _out<<em_str<<" ";
while(1) { ptr = strtok(NULL," "); if(ptr==NULL) break;
strcpy(em_str,ptr); del(em_str,ch); _out<<em_str<<" "; }
} _out<<em_str<<"\n";
} _in.close(); _out.close();
return 0; }
void del(char *arr, char ch) { int i;
if(arr[0]==ch) { for(i=0; arr[i] != '\0'; i++) { arr[i]=' '; } } }
접기