Thursday, September 25, 2008

Reading Data & Inputting Data from a file

.cpp file
text file --- color_file_in.txt (input file)
Problem:
R or r = Red
B or B = Black
G or g = Green
W or w = White
= OUT OF THE LIST
-1 = Terminator/stops the file processing

#include < conio.h >
#include < iostream >
using namespace std;
int main()
{
FILE *ptr;
char mik;
int num;
ptr=fopen("color_file.in.txt","r");
if(ptr==NULL)
{
cout<<"File could not be opened!";
}
else
{
while(!feof(ptr))
{
fscanf(ptr,"%c%d\n",&mik,&num);
if(mik!=-1)
{
if(mik=='R' || mik=='r'){cout<<"Red\n";}
else if(mik=='B' || mik=='b'){cout<<"Black\n";}
else if(mik=='G' || mik=='g'){cout<<"Green\n";}
else if(mik=='W' || mik=='w'){cout<<"White\n";}
else cout<<"OUT OF THE LIST\n";
}



}

fclose(ptr);
}
getch();
}

No comments: