Tech Forums > ph.Cyberview

Anybody know C++?

<< < (4/4)


--- Quote from: namida ---btw, what compiler and environment are you using?
--- End quote ---

Dev C++


Here's for the 6. Not really sure bout this one.

void assignment_6()
FILE *pFile;
int i,nLength;
char[] sFilename,sMode,sInput;
char cData;

get_str("Enter File Name:",sFilename,MAX);//Gets filename
get_str("Enter Mode (R/W)",sMode,MAX);//Gets mode

if(strcmp(sMode,"R")==0||strcmp(sMode,"r")==0)//if sMode gets R or r
if((pFile=fopen(sFilename, "rt"))!=NULL)//opens file for Reading and checks if it exists

cData=fgetc(pFile);//gets the character from the file
printf("%c",cData);//prints cData
}while(cData!=EOF);//while it's not yet the end of the file

/*the reason why I chose fgetc over fgets is because of the bug that fgets has with the EOF. I'm not sure if they already fixed the bug though*/

else //if it doesn't exists or something is wrong witht he file
puts("Error Opening File");

else if(strcmp(sMode,"W")==0||strcmp(sMode,"w")==0)//if sMode gets W or w
if((pFile=fopen(sFilename, "wt"))!=NULL)//Opens the filename for writing

nLength=get_str("",sInput,MAX);//gets input from keyboard
for(i=0;i<nLength;i++)//loops the whole string char per char
fputc(pFile,sInput);//enters the whole string char per char except the '\0'
fputc(pFile,'\n');//inserts a line break
}while(strcmp(sInput,EOF)!=0);//loop to detect if the input from keyboard is an EOF

fclose(pFile);//closes the file


else//if file is in use
puts("Error Opening File");


else//if keyboard input is wrong
puts("Wrong input. Must be R or W only");


Wow...  ang lulupet tlga ng mg noypits sa programming (except me!)


[0] Message Index

[*] Previous page

Go to full version