VS2010 giving me Debug Assertion Failure in C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alex T
    New Member
    • Oct 2010
    • 29

    VS2010 giving me Debug Assertion Failure in C++

    Hello, I am having a problem running my program with libcurl,

    What puzzles me is that this program works on Visual Studio 2010 on my computer running Windows XP, however when I try to run the same program on my laptop which runs Windows 7, it gives me the following error message:

    Debug Assertion Failed!

    Program: C:\Project\libc url\VisualStudi o\project1\Debu g\project1.exe
    File: f:\dd\vctools\c rt_bld\self_x86 \crt\src\fwrite .c
    Line: 77

    Expression: (stream != NULL)

    if you need to see my code, here it is:

    main.cpp
    Code:
    #include "txtfiles.h"
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	txtfiles gs;
    	gs.organize();
    
    	return 0;
    }
    dateclass.h
    Code:
    #include "numdate.h"
    #include "numvalue.h"
    
    using namespace std;
    
    class dateclass
    {
    private:
    	ushort day;
    	ushort newday;
    	ushort month;
    	ushort year;
    	string curdate;
    	string srchdate;
    	string weekday;
    	string datestring;
    	time_t rawtime;
    public:
    	
    	void reset(){
    		time(&rawtime);
    		datestring = ctime(&rawtime);
    		curdate.clear();
    		datestring.clear();
    	}
    	
    	dateclass() //date constructor 
    	{
    		time(&rawtime);
    		datestring = ctime(&rawtime);
    
    		curdate = datestring;
    		curdate.erase(0,4); //remove day prefix
    		curdate.erase(7,9); //remove time
    		curdate.insert(6, ",", 1); //insert comma
    
    		numvalue current(curdate);
    		
    		month = current.getmonth();
    		day = current.getday();
    		year = current.getyear();
    
    		weekday = datestring.substr(0,3);
    	};
    
    	const string& getcurdate()
    	{
    		srchdate = curdate;
    		if (day < 10) //change "0x" to "x" in date
    			srchdate = curdate.erase(4,1);
    		return srchdate;
    	};
    
    	string decrement(ushort decrement)
    	{		
    		numdate numeric (month, day, year, weekday);
    		numeric.decrement(decrement);
    
    		srchdate = numeric.revert();
    		numvalue search(srchdate);
    		newday = search.getday();
    
    		if (newday < 10)
    			srchdate = srchdate.erase(4,1);
    
    		return srchdate;
    	};
    };
    exclusion.h
    Code:
    #include <time.h>
    #include <string>
    
    typedef unsigned short ushort;
    using namespace std;
    
    class exclusion
    {
    private:
    	string weekday;
        ushort num;
    	ushort value;
    
    public:
    	exclusion(string weekday)
    	{
    		if(weekday == "Sat") num = 0;
    		else if(weekday == "Sun") num = 1;
    		else if(weekday == "Mon") num = 2;
    		else if(weekday == "Tue") num = 3;
    		else if(weekday == "Wed") num = 4;
    		else if(weekday == "Thu") num = 5;
    		else num = 6;
    	};
    
    	ushort decrement(ushort countdown)
    	{
    		countdown++;
    		value = countdown;
    		
    		if (num <= 2)
    		{
    			value += num;
    			value += 2 * ((countdown - 1)/ 5);
    			return value;
    		} else {
    			if (countdown > num - 2)
    			{
    				value += 2;
    				value += 2 * ((countdown - num + 1) / 5);
    				return value;
    			} else {
    				value = countdown;
    				return value;
    			}
    		}
    	};
    };
    numdate.h
    Code:
    #include "exclusion.h"
    
    using namespace std;
    const ushort Feb = 31;
    
    enum holidays { NewYear = 1, MartinLuther = 18, Washington = 46, GoodFriday = 92, MemorialDay = 151,\
    	IndependenceDay = 187, LaborDay = 249, ThanksgivingDay = 329, Christmas = 358 };
    
    class numdate
    {
    private:
    	ushort reduce;
    	ushort m_month;
    	ushort d_day;
    	ushort y_year;
    	string w_weekday;
    	ushort t_month;
    	ushort t_day;
    	ushort t_year;
    
    	int raw;
    	ushort num_days;
    	
    	ushort Mar;
    	ushort Apr;
    	ushort May;
    	ushort Jun;
    	ushort Jul;
    	ushort Aug;
    	ushort Sep;
    	ushort Oct;
    	ushort Nov;
    	ushort Dec;
    public:
    	numdate(int month, int day, int year, string weekday) : m_month(month), d_day(day), y_year(year), w_weekday(weekday)
    	{
    		
    	}
    	
    	ushort ret_month() {return m_month;};
    	ushort ret_day() {return d_day;};
    	ushort ret_year() {return y_year;};
    
    	void decrement(ushort decrement)
    	{
    		if (y_year % 4 == 0)
    			{Mar = 60; Apr = 91; May = 121; Jun = 152; Jul = 182;
    			Aug = 213; Sep = 244; Oct = 274; Nov = 305; Dec = 335;}
    		else
    			{Mar = 59; Apr = 90; May = 120; Jun = 151; Jul = 181;
    			Aug = 212; Sep = 243; Oct = 273; Nov = 304; Dec = 334;}
    
    		switch(m_month)
    		{
    		case 1: break;
    		case 2:	num_days = d_day + Feb; break;
    		case 3: num_days = d_day + Mar; break;
    		case 4: num_days = d_day + Apr; break;
    		case 5: num_days = d_day + May; break;
    		case 6: num_days = d_day + Jun; break;
    		case 7: num_days = d_day + Jul; break;
    		case 8: num_days = d_day + Aug; break;
    		case 9: num_days = d_day + Sep; break;
    		case 10: num_days = d_day + Oct; break;
    		case 11: num_days = d_day + Nov; break;
    		case 12: num_days = d_day + Dec; break;
    		}
    
    		t_year = y_year;
    		
    		exclusion decrease(w_weekday);
    		reduce = decrease.decrement(decrement);
    		
    		while (num_days < reduce)
    		{
    			t_year = t_year - 1;
    			if (t_year % 4 == 0) {num_days += 366;}
    			else {num_days += 365;}
    		}
    			
    		raw = num_days - reduce;
    
    		if (raw <= NewYear && num_days >= NewYear)
    			decrement++;
    		if (raw <= MartinLuther && num_days >= MartinLuther)
    			decrement++;
    		if (raw <= Washington && num_days >= Washington)
    			decrement++;
    		if (raw <= GoodFriday && num_days >= GoodFriday)
    			decrement++;
    		if (raw <= MemorialDay && num_days >= MemorialDay)
    			decrement++;
    		if (raw <= IndependenceDay && num_days > IndependenceDay)
    			decrement++;
    		if (raw <= LaborDay && num_days >= LaborDay)
    			decrement++;
    		if (raw <= ThanksgivingDay && num_days >= ThanksgivingDay)
    			decrement++;
    		if (raw <= Christmas && num_days >= Christmas)
    			decrement++;
    
    		reduce = decrease.decrement(decrement);
    		t_day = num_days - reduce;
    
    		if (t_day <= 0)
    		{
    			t_year = t_year - 1;
    			if (t_year % 4 == 0) {t_day += 366;}
    			else {t_day += 365;}
    		}
    
    		if (t_day > Dec) {t_day = t_day - Dec; t_month = 12;}
    		else if (t_day > Nov) {t_day = t_day - Nov; t_month = 11;}
    		else if (t_day > Oct) {t_day = t_day - Oct; t_month = 10;}
    		else if (t_day > Sep) {t_day = t_day - Sep; t_month = 9;}
    		else if (t_day > Aug) {t_day = t_day - Aug; t_month = 8;}
    		else if (t_day > Jul) {t_day = t_day - Jul; t_month = 7;}
    		else if (t_day > Jun) {t_day = t_day - Jun; t_month = 6;}
    		else if (t_day > May) {t_day = t_day - May; t_month = 5;}
    		else if (t_day > Apr) {t_day = t_day - Apr; t_month = 4;}
    		else if (t_day > Mar) {t_day = t_day - Mar; t_month = 3;}
    		else if (t_day > Feb) {t_day = t_day - Feb; t_month = 2;}
    		else {t_month = 1;}
    
    	};
    
    	string revert()
    	{
    		string vdate;
    		
    		switch (t_month)
    		{
    		case 1: vdate += "Jan "; break;
    		case 2: vdate += "Feb "; break;
    		case 3: vdate += "Mar "; break;
    		case 4: vdate += "Apr "; break;
    		case 5: vdate += "May "; break;
    		case 6: vdate += "Jun "; break;
    		case 7: vdate += "Jul "; break;
    		case 8: vdate += "Aug "; break;
    		case 9: vdate += "Sep "; break;
    		case 10: vdate += "Oct "; break;
    		case 11: vdate += "Nov "; break;
    		case 12: vdate += "Dec "; break;
    		}
    
    		vdate += ((char) ((t_day / 10) + 48));
    		vdate += ((char) ((t_day % 10) + 48));
    
    		vdate += ", ";
    
    		vdate += ((char) ((t_year / 1000) + 48));
    		vdate += ((char) (((t_year % 1000) / 100) + 48));
    		vdate += ((char) ((((t_year % 1000) % 100) / 10) + 48));
    		vdate += ((char) ((((t_year % 1000) % 100) % 10) + 48));
    
    		return vdate;
    	};
    };
    numvalue.h
    Code:
    #include <string>
    
    typedef unsigned short ushort;
    using namespace std;
    
    class numvalue
    {
    private:
    	ushort day;
    	ushort month;
    	ushort year;
    	char number;
    	string str_month;
    	string date;
    
    public:
    	numvalue(string input) : date(input){};
    	
    	ushort getmonth()
     	{
    		str_month = date.substr(0,3); //get first 3 chars
    		
    		if (str_month == "Jan") month = 1; //get month values
    		else if (str_month == "Feb") month = 2;
    		else if (str_month == "Mar") month = 3;
    		else if (str_month == "Apr") month = 4;
    		else if (str_month == "May") month = 5;
    		else if (str_month == "Jun") month = 6;
    		else if (str_month == "Jul") month = 7;
    		else if (str_month == "Aug") month = 8;
    		else if (str_month == "Sep") month = 9;
    		else if (str_month == "Oct") month = 10;
    		else if (str_month == "Nov") month = 11;
    		else month = 12;
    
    		return month;
    	};
    
    	ushort getday()
    	{
    		number = date.at(5);
    		day = (int) number - 48;
    		number = date.at(4);
    		day += 10 * ((int) number - 48);
    
    		return day;
    	};
    
    	ushort getyear()
    	{
    		number = date.at(11);
    		year = (int) number - 48;
    		number = date.at(10);
    		year += 10 * ((int) number - 48);
    		number = date.at(9);
    		year += 100 * ((int) number - 48);
    		number = date.at(8);
    		year += 1000 * ((int) number - 48);
    
    		return year;
    	};
    };
    sourcetxt.h
    Code:
    #include <string>
    #include "curl/curl.h"
    #include "curl/easy.h"
    
    using namespace std;
    
    CURLcode copy(const string& url, const string& filepath);
    
    size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) 
    {
    	size_t written;
    	written = fwrite(ptr, size, nmemb, stream);
    	return written;
    }
    
    CURLcode copy(const string& url, const string& filepath)
    {
    
    	struct cleaner
    	{	
    		FILE *fp;
    		~cleaner(){fclose(fp);}
    	}cleanfile;
    	
    	CURLcode res;
    		
    	CURL *curl = curl_easy_init();
    	
    	if(curl) {
    		cleanfile.fp = fopen(filepath.c_str(),"wb");
    		curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
    		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); 
    		curl_easy_setopt(curl, CURLOPT_WRITEDATA, cleanfile.fp);
    		res = curl_easy_perform(curl);
    		curl_easy_cleanup(curl);
    	}
    
    	return res;
    }
    txtfiles.h
    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <string>
    #include "sourcetxt.h"
    #include "dateclass.h"
    
    using namespace std;
    
    const string prntpath = "C:\\data1.txt";
    
    class txtfiles
    {
    private:
    	CURLcode crl;
    	dateclass c_date;
    	size_t j;
    	string today, date, open, high, low, close, volume, search;
    	ifstream infile;
    	ofstream outfile;
    	ushort i;
    public:
    	txtfiles()
    	{
    		today = c_date.getcurdate();
    		crl = copy("http://www.google.com/finance/historical?/q=NYSE:GS", prntpath);
    	};
    
    	const int& organize()
    	{
    		i = 0;
    		infile.open(prntpath);
    		outfile.open("C:\\data.txt");
    
    		if (infile.fail())
    		{
    			cout << "Error: Could Not Open Input File!";
    			return 1;
    		}
    
    		cout << "Successfully Opened Input File." << endl;
    		
    		while (!infile.eof())
    		{
    			getline(infile, date);
    			search = ", 20";
    
    			while ((j = date.find(", 20")) != string::npos)
    			{
    				date.replace(j, 4, "abcd");
    				date.erase(0, 15);
    			}
    			while ((j = date.find("abcd")) != string::npos)
    			{
    				i++;
    				date.replace(j, 4, ", 20");
    				if(i <= 5) continue;
    				getline(infile, open);
    				open.erase(0, 16);
    				getline(infile, high);
    				high.erase(0, 16);
    				getline(infile, low);
    				low.erase(0, 16);
    				getline(infile, close);
    				close.erase(0, 16);
    				getline(infile, volume);
    				volume.erase(0, 19);
    				outfile << "[" << date << "]";
    				outfile	<< "[" << open << "]"; 
    				outfile	<< "[" << high << "]"; 
    				outfile	<< "[" << low << "]"; 
    				outfile << "[" << close << "]"; 
    				outfile << "[" << volume << "]" << endl;
    			}
    		}
    		infile.close();
    		outfile.close();
    		i = remove("C:\\data1.txt");
    		if (i == 0)
    		{
    			cout << " - Successfully Adapted Text File." << endl;
    			return 0;
    		}
    		else
    		{
    			cout << " - Error: Could Not Adapt File!" << endl;
    			return 1;
    		}
    	};
    
    	const string& getvalue(ushort offset, char choice)
    	{
    		date = c_date.decrement(offset);
    		return date;
    	};
    };
    The program runs through main.cpp, sourcetxt.h, and txtfiles.h, but gives me the error message at line 12 of sourcetxt.h

    I notice that ptr, cnt, base, flag, file, charbuf, bufsiz, tmpfname give the error:

    CXX0030: Error: expression cannot be evaluated

    If anyone can help me out, or if I am completely in the wrong section, please let me know. Thank you.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Line 30, sourcetxt.h you need to check that fopen does not return NULL

    Comment

    • newb16
      Contributor
      • Jul 2008
      • 687

      #3
      Windows 7 will not allow you (a non-elevated app) to create file in the root of the disk.

      Comment

      • Alex T
        New Member
        • Oct 2010
        • 29

        #4
        How would I do that though? I don't understand how to do this.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          fopen just returns a pointer to file, that you store in cleaner.fp, just compare it to NULL (or 0) using == as you would with any other pointer (or other variable type).

          Comment

          • Alex T
            New Member
            • Oct 2010
            • 29

            #6
            The program now finishes running, but the text file returned is empty. Why does this happen?

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              How do you know the text file is empty?

              For example do you open it in Notepad or do you pull up the file properties and see it has a size of 0?

              If you have just opened it in notepad then do pull up the file properties and look at the actual file size.

              I ask this because fwrite writes binary data and given that write_data takes an object size and a number of objects it seems to me that there is nothing in the code that suggests that the output would be text.

              Comment

              • Alex T
                New Member
                • Oct 2010
                • 29

                #8
                the size

                The size is 0 bytes, I just checked. I don't understand how this is.

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  Perhaps write_data is never called. That would be easy enough to check, In fact you could record how many bytes of data should have been written by adding a global (not best practice but this is debug code) integer variable and updating it in write_data with the amount of data that should have been written.

                  Comment

                  Working...