#### Archived

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

# Blocks of data in stdio

## Recommended Posts

Hi, Im currently loading a file with stdio, example below, and im trying to write my code so that I can read a block of 15 characters per buffer, now of course I dont need to write each buffer to its own char, I just need to be able to see the next 15, and the next so on and so forth. I was wondering if someone could look at my code and see if I am needing a simple code to get this to work because at the moment im only getting the same 15 characters over and over again:
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>

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

rewind(file);

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

void main() {
FILE *file;
long lSize, bufSize;
int maxSize = 15;
char *buffer;

file=fopen("C:\\test.txt", "r");
lSize = (unsigned long)getfilesize(file);

if( lSize > maxSize ) {
bufSize = (lSize / maxSize);
rewind(file);
for(int i = 0; i < bufSize; i++ ) {
fseek (file, 0, SEEK_SET);
rewind(file);

buffer = (char*) malloc (maxSize - 4);
if (buffer == NULL) exit (2);

cout << buffer;
cout << endl;
}
}
fclose(file);
}

Thanks again. - BlueDev

A few problems.. in the for(int i = 0; i < bufSize; i++ ) loop, the first thing you do is an fseek(), which is setting the file pointer back to the beginning of the file, which is why you''re getting the same section again and again. This is immediately followed by a call to rewind(), which sets the file pointer back to the beginning of the file, which is why you''re getting the same section again and again. Your function getfilesize() leaves the file pointer at the end of the file, so you need fseek() or rewind() once after the call to getfilesize(). Better still, add it to getfilesize() before you return i.

The malloc() is allocating a completely new section of memory every time you go round the loop. I suspect you probably want to do this only once, outside the loop, but remember to free the memory when you''re finished using it. Also, you''re allocating maxSize-4 which works out as 11 bytes, then attempting to read maxSize, or 15 bytes, into that space. You''re overwriting memory that doesn''t belong to you, and you''re lucky it didn''t crash horribly!

##### Share on other sites

[edited by - drekkar on September 1, 2003 9:14:00 AM]

##### Share on other sites
Also, if you''re using cout statments and the like, why not just use an ifstream? And use new instead of malloc. (but remember to delete also!) I think most people on this board would agree that although it''s possible to write C++ progs with C code intermingled in it, it''s better to treat them as two different languages and use the appropriate conventions.

##### Share on other sites
Bah!

/me shakes his fist menacingly at crossposters.

##### Share on other sites
Pootle,

your answer was greatly appreciated, both you and drekkar had a little bit of the same things to say but you both pointed out a different solutions that will work so thanks all!

- BlueDev

• ### Forum Statistics

• Total Topics
628329
• Total Posts
2982105

• 22
• 9
• 9
• 13
• 11