Link to home
Start Free TrialLog in
Avatar of shaolinfunk
shaolinfunkFlag for United States of America

asked on

What is wrong with my bool function?

I am trying to determine if a file exists or not (see function FileExist).  My code is attached.  Why do I get errors when I compile?
#include <time.h>
#include <stdio.h>
//#include <afx.h>
#include <string>

using namespace std;

unsigned short GetTodaysDate(unsigned short &year, unsigned short &month, unsigned short &day)
{
	time_t rawtime;        //rawtime is variable of data type time_t, related to time.h
    struct tm * timeinfo; //timeinfo points to a struct of type tm, related to time.h
    char str[60];

    rawtime = time (NULL); //retrieves number of seconds from 1/1/70, in GMT, stores in address of rawtime
    timeinfo = localtime ( &rawtime ); //localtime takes rawtime, and creates a "tm" struct

   strftime(str, sizeof(str) , "%Y %m %d", timeinfo);  //formats the timeinfo struct into a string
//   printf(str);                                                       //outputs string to let me see if date is right
   
   year = atoi(&str[0]);   
   month = atoi(&str[5]);
   day = atoi(&str[8]);

   return (year, month, day);
}


string FindOneDayEarlier(unsigned short year, unsigned short month, unsigned short day)
{
//months with 31 days: 1, 3, 5, 7, 8, 10, 12
//months with 30 days: 4, 6, 9, 11
//months with 28/29 days: 2

	day = day - 1; //decrement the day

    if (day == 0) //if day = 0, today must be 1st of the month
	{    
			if (month == 2 || month == 4 || month == 6 || month == 8 || month == 9 || month == 11 || month == 1)
			{
				day = 31;
			}

			if (month == 5 || month == 7 || month == 10 || month == 12)
			{
				day = 30;
			}
			if (month == 3)  //if not a leap year, who cares, just search, find nothing, skip, and decrement to 28
			{
				day = 29;
			}


			month = month - 1;
			if (month == 0)  //if this month is Jan, prior month will be Dec.
			{
				month = 12;  //if we are in Dec., we must be in prior year...so...
				year = year - 1; //we will never count back before year 2000, so it's always positive
			}
    }

   char buffer[40];
   sprintf( buffer, "C:\\\\%d\\\\%d\\\\%d\\", year, month, day);

   return string(buffer);
}

bool FileExist( string strFilepath )
{
     
    // ifstream is used for reading files
    ifstream inf( strFilepath );

    // If we couldn't open the output file stream for reading
    if (!inf)
    {
        return false;
    }
	else
		return true;
	    
}
/* only works with MFC...must remember to "#include <afx.h>"
bool FileExist2( string strPath )
{
        CFileFind ff;

        return FALSE != ff.FindFile( strPath ); //return false 
}
*/

int main()
{
// Get TODAY's date  
   unsigned short year = 0;
   unsigned short month = 0;
   unsigned short day = 0;

	GetTodaysDate (year, month, day);
   
// Given today's date, get YESTERDAY's date in string form to be used in a file path
	string strFilepath;
	strFilepath = FindOneDayEarlier(year, month, day);

//  Given the FilePath for yesterday's date, find out if the Example.txt file exists
	FileExist(strFilepath);

	/* this one only works with MFC
	FileExist2(strFilePath); */

    return 0;
}

Open in new window

Avatar of shaolinfunk
shaolinfunk
Flag of United States of America image

ASKER

Forgot to add the error log...attached here.
1>------ Build started: Project: junk, Configuration: Debug Win32 ------
1>Compiling...
1>Junk.cpp
1>c:\documents and settings\administrator\desktop\junk\junk\junk.cpp(71) : error C2079: 'inf' uses undefined class 'std::basic_ifstream<_Elem,_Traits>'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>c:\documents and settings\administrator\desktop\junk\junk\junk.cpp(71) : error C2440: 'initializing' : cannot convert from 'std::string' to 'int'
1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>Build log was saved at "file://c:\Documents and Settings\Administrator\Desktop\junk\junk\Debug\BuildLog.htm"
1>junk - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
add this include :)

#include <fstream>
Hah. Too late, and less complete lol.
Ok, thanks.

Can you explain to me what that added ".c_str()" means?  
BTW, you could also use '_stat()' to see if a file exists, e.g.

#include <sys/types.h>
#include <sys/stat.h>

bool FileExists(const std::string& s) {

   struct _stat buf;
   int result;

   result = _stat( s.c_str(), &buf );

   return !(result == -1);
}

Open in new window

'string::c_str()' obtains the contents of a std::string as a 'const char*', which is required as the argument to a constructor of a stream (they don't take strings). See also http://www.cplusplus.com/reference/string/string/c_str/ 
Avatar of phoffric
phoffric

Back for a minute. Noticed that you are not testing for the result of FileExist. You should test with an if statement.
#include <fstream>

ifstream inf( strFilepath.c_str() );

Open in new window

>>Too late, and less complete lol.

Will take this as a compliment, since I'm commenting while preparing dinner at the same time ;o)
haha, phoffric you are ahead of me...i am in the middle of composing my very next 500 point question to try and integrate everything i am learning from you, infinity, and jkr.
dinner already?  what time zone are you in, where are you from?  it's only 4pm here EST.
@jkr,
 one hand on the stirring ladel, and one hand on the iphone?
>> Will take this as a compliment, since I'm commenting while preparing dinner at the same time ;o)

Oh, it WAS meant as a compliment :) But you don't have to rub it in lol

I'll have a nice cold beer instead then ;)
This is CEDT at the moment (aka GMT+1, ~10pm), and no, I don't have an iPhone ;o)
I8, you just reminded me of something xD
hehe ... Cheers !!!
Ok guys...I ran into a roadblock again....I don't know how to accomplish what I want to do here in this question:

https://www.experts-exchange.com/questions/25666205/How-do-I-store-a-valid-filepath-so-that-I-can-retrieve-it-later.html

Any help/thought/ideas/code would be much appreciated!