Hello all, I am pretty new to c++ and programing in general. I have a problem with a code I am attempting to write. I am supposed to create an int function that will try to open a file named word.uses, where word is the string parameter. the file will contain a single integer value which represents the number of times that file has been opened, called uses. if the file does not exist, return 0. if it does exist then return the integer value found in the file.
I am having some trouble getting the file to open. I am not sure if the file is opening or not but the integer uses does not get replaced with uses1 when I run the program. Am I going about the right way of reading the file? any help would be greatly appreciated.
To open a stream you create one with the filename as parameter.
One way:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
int get_uses(string word)
{
word = word + ".uses";
ifstream fin(word.c_str());
if(!fin) // file error - !fin is same as !fin.good
{
return 0;
}
int use_count;
fin >> use_count; // read the int
// maybe check if the int was read correctly
// if (!fin) return 0;
// no need to close the stream, will be done by destructor
return use_count;
}