Jump to content
  • Advertisement


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

Problem with reading file in stdio

This topic is 5375 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;


	while (!feof(sFile)) {
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);

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


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

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

That's unneccessary.

buffer = new char[maxSize];


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.


[ 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
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
why do you get the filesize with this fgetc loop???

int filelen=ftell(sFile);

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

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

(i''m coder not artist )

Share this post

Link to post
Share on other sites
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
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?


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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!