Hello, I happen to be a novice in the realm of C++, and I'm currently confused due to this error when trying to run my application:
"Assertion Failed. Expression 'stream != NULL' ".
I've looked it up, and nothing that comes up can answer why it gives me said error. I believe it has something to do with my code:

#include "stdafx.h"
#include <stdio.h>
#include <string>

using namespace std;
using namespace System;

int extractfiledata(const char oldf[], const char newf[])
{
	char idx[256];
	long size;
	FILE * tmpf, * updf;
	fopen_s(&tmpf, oldf, "w");
	fopen_s(&updf, newf, "r");
	size = ftell(updf);
	if(tmpf != NULL && updf != NULL)
	{
		while(fread(idx, 1, size, updf))
		{
			string tmpstr(idx);
			int pos=0;
			int chk=0;
			do
			{
				if(sizeof(updf) >= 256 && pos >= 256)
				{
					tmpstr.clear();
					pos=0;
					continue;
				}
				if(chk == sizeof(updf))
				{
					chk=0;
					pos=0;
					break;
				}
				fwrite(&idx[pos], 1, sizeof(&idx[pos]), tmpf);
				pos++;
				chk++;
			}
			while(pos<256);
		}
		fclose(updf);
		fclose(tmpf);
	}
	return 0;
}

int main(array<System::String ^> ^args)
{
	char oldfile[] = "DoesThisWork.txt";
	char newfile[] = "DoesThisWork2.txt";
	extractfiledata(oldfile, newfile);
    return 0;
}

I'm basically trying to copy text from one file over to another file via strings. Any help is appreciated, thanks ahead of time.

Recommended Answers

All 3 Replies

read this article Seems to me it would be a whole lot easier to use managed streams. Don't ask me how because I have not done it. Something like this article.

> size = ftell(updf);
You do this BEFORE checking the file was opened successfully.

Besides, you just opened the file for reading, so the answer is trivially going to be zero.

> if(sizeof(updf)
This isn't going to tell you anything useful about the file.

> while(fread(idx, 1, size, updf))
Use fgets() to read a text file.

> fopen_s(&tmpf, oldf, "w");
I would generally advise avoiding non-portable vendor-specific functions.

Thanks for the help, got it to work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.