• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 269
  • Last Modified:

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

0
shaolinfunk
Asked:
shaolinfunk
  • 6
  • 5
  • 4
  • +1
1 Solution
 
shaolinfunkAuthor Commented:
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

0
 
jkrCommented:
Two things: You forgot to

#include <fstream>

and on line 72, you need to use

    ifstream inf( strFilepath.c_str() );

E.g.
#include <time.h>
#include <stdio.h>
//#include <afx.h>
#include <string>
#include <fstream>

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.c_str() );

    // 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

0
 
Infinity08Commented:
add this include :)

#include <fstream>
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Infinity08Commented:
Hah. Too late, and less complete lol.
0
 
shaolinfunkAuthor Commented:
Ok, thanks.

Can you explain to me what that added ".c_str()" means?  
0
 
jkrCommented:
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

0
 
jkrCommented:
'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/ 
0
 
phoffricCommented:
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

0
 
jkrCommented:
>>Too late, and less complete lol.

Will take this as a compliment, since I'm commenting while preparing dinner at the same time ;o)
0
 
shaolinfunkAuthor Commented:
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.
0
 
shaolinfunkAuthor Commented:
dinner already?  what time zone are you in, where are you from?  it's only 4pm here EST.
0
 
phoffricCommented:
@jkr,
 one hand on the stirring ladel, and one hand on the iphone?
0
 
Infinity08Commented:
>> 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 ;)
0
 
jkrCommented:
This is CEDT at the moment (aka GMT+1, ~10pm), and no, I don't have an iPhone ;o)
0
 
jkrCommented:
I8, you just reminded me of something xD
0
 
Infinity08Commented:
hehe ... Cheers !!!
0
 
shaolinfunkAuthor Commented:
Ok guys...I ran into a roadblock again....I don't know how to accomplish what I want to do here in this question:

http://www.experts-exchange.com/Programming/Languages/CPP/Q_25666205.html

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now