Archived

This topic is now archived and is closed to further replies.

BlueDev

Problem with reading file in stdio

Recommended Posts

BlueDev    126
Hello, I am running into some trouble when trying to read a file that is larger than 1 GB. Now I thought it would be a good idea to slice it in small pieces but when I do it still does not come out correctly. Can anyone look at the code below and tell me why I am not able to read my file. My file byte size is (1162986833) I dont get an error loading the file its just that my buffer is empty when I try to display my information:
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

unsigned long getfilesize(FILE *sFile) {
	unsigned long i = 0;

	rewind(sFile);

	while (!feof(sFile)) {
		fgetc(sFile);
		i++;
	}
return i;
}

void main() {
	FILE *file;
	char *buffer;
	unsigned long lSize;
	unsigned int maxSize = 50000;

	file=fopen("C:\\test.txt", "rb");
		fseek(file, 0, SEEK_END);
		lSize = (unsigned long)getfilesize(file);
		rewind(file);

		buffer = (char*) calloc (maxSize,sizeof(maxSize));
		if (buffer == NULL) exit (2);

		fread(buffer,1,maxSize,file);

		cout << buffer;
	fclose(file);
	free(buffer);
}
Thanks for your help. - BlueDev
BlueDev Net [edited by - BlueDev on September 1, 2003 11:49:33 PM]
KalvinB    102
Why are you using a long? Use an unsigned int.

buffer = (char*) calloc (maxSize,sizeof(maxSize));

That's unneccessary.

buffer = new char[maxSize];

then

delete [] buffer;

cout is a string function. It stops when it hits a zero in the string. If you want to output the whole thing you don't use a string function.

You use a loop and output one character at a time manually.

Ben


[ IcarusIndie.com | recycledrussianbrides.com ]


Will Post For Food

[edited by - KalvinB on September 2, 2003 1:14:12 AM]

Share this post


Link to post
Share on other sites
BlueDev    126
Thanks for the insight, I understand everthing your saying there, but how am I going to display each letter manually in a loop, ive never seen or done anything like that before.

Thanks for your help

- BlueDev

Share this post


Link to post
Share on other sites
LousyPhreak    133
why do you get the filesize with this fgetc loop???

try:
fseek(sFile,0,SEEK_END);
int filelen=ftell(sFile);


and for outputting stuff in a loop:
for(char *c=buffer,c{
//c way:
printf("%c",*c);
//c++ way (not sure about this, never used it but should work)
cout<<*c;
}

also (if you dont like the pointer stuff ):
for(int i=0;i printf("%c",buffer);



http://mitglied.lycos.de/lousyphreak/
(i''m coder not artist )

Share this post


Link to post
Share on other sites
BlueDev    126
Thanks for the help, it seems to be running alot faster ever since I rewrote my code and am using the loop to display each character.

thanks again.

- BlueDev

Share this post


Link to post
Share on other sites
BlueDev    126
Also, is there a way I can write buffer[j] to a char because when I try to write my file it doesnt seem to write all the characters, only if using cout, and a loop. So is there a way to write each and every byte to a file similar to the above loop?

Thanks

edit: nevermind, I figured it out

edit2: well, nevermind, I havent figured it out, and im trying to make something like char *buffer2; buffer2 = buffer[j]; but it doesnt seem to work that easily

- BlueDev

[edited by - BlueDev on September 2, 2003 11:37:29 PM]

Share this post


Link to post
Share on other sites
BlueDev    126
Ok, with some help from Washu I got this part in


buffTemp = (char*) malloc (sizeof(char)*bytesRead+1);
memcpy((void*)buffTemp, (void*)buffer, bytesRead);


Now I still have the same problem and can only read the first two characters but I want to read the whole thing like the loop example, now if I remove the memcpy() line I get all 65536 characters but there all looking like "____________" with 4 "power of 2" symbols at the end, how can I resolve this and simply read the data that is in buffer?

Thank you.

- BlueDev

Share this post


Link to post
Share on other sites
BlueDev    126
Ok, I almost figured it out:


unsigned int j;
for(j = 0; j < MAXSIZE; j++)
cout << buffer[j];
memset (buffTemp,buffer[j],j);
cout << endl;


im not sure how memset is fully supposed to work but it looks like it can do the job, so if you might know how to use memset or something better please let me know

- BlueDev

[edited by - BlueDev on September 2, 2003 12:46:07 AM]

Share this post


Link to post
Share on other sites