Ok, I am just starting to play with C++ and have written a class that opens files for practice. Does anyone know why this is not opening the files? (or if it is, why it is not exiting) I am sending it basic input like c:\test.txt (and using a file that i do have in my c:\).
Code:
#include <stdio.h>
#include <stdlib.h>
class myFileLoader
{
public:
void getName();
private:
FILE *itsName;
};
void myFileLoader::getName()
{
FILE *temp;
char str[100];
printf("Please enter the file you wish to load: ");
fgets(str, 99, stdin);
while ((temp = fopen(str, "r")) == NULL )
{
printf("\n******File was not opened, Please try again******");
printf("\nPlease enter the file you wish to load: ");
fgets(str, 99, stdin);
}
printf("\nFile sucessfully loaded!");
itsName = temp;
}
int main()
{
myFileLoader test;
test.getName();
return 0;
}
Also, if I wanted to put the class in a header file and its function in another cpp file and include it all together, would I need to use #IFNDEFINE and call the other <stdio.h> so that they would know what FILE is supposed to be?
P.S. any pointers on the code would be appreciated.