PDA

View Full Version : Problem flushing IO Stream


singularity2006
03-04-2004, 07:42 AM
I have a program process composed of two functions. The first calls count_all_characters to count the total number of characters in a file. Then it calls a second function to count all the floating point numbers in a file. However, I'm having trouble clearing the IO stream from the first program before it can process the second program. The second portion keeps running into the EOF from count_all_characters(), which makes it not run at all, thus instant EOF and 0 count of floats. Here's what I have for my flush(doesn't seem to want to work though): Ideas?


#define FLUSH while(getchar() != '\n')

char_count = count_all_characters(input, output);
FLUSH;
white_space_count = count_all_spaces(char_count, input, output);
fprintf(output,"The input file has white %d space character(s).\n",white_space_count);
printf("White space count processed and recorded to log.\n");

playmesumch00ns
03-04-2004, 10:20 AM
Not exactly sure what you're trying to do here. If you're doing two passes pver a file you'll need to reset the file pointer to the start of the file before you try the second pass or you will just get EOF all the time.


I guess you should just be able to take a copy of the file pointer you get when opening the file and just come back to that to re-scan the file after you've done your first pass.

schmu_20mol
03-04-2004, 02:41 PM
maybe this sounds just plain stupid - I didn't really get what you're up to ...but you are sure that you're not overloading variables (a lil too much)?

another thing ... is it possible to assert such a situation in c? 'coz with assertions you probably could circle the problem

singularity2006
03-04-2004, 04:27 PM
Originally posted by playmesumch00ns
Not exactly sure what you're trying to do here. If you're doing two passes pver a file you'll need to reset the file pointer to the start of the file before you try the second pass or you will just get EOF all the time.

I guess you should just be able to take a copy of the file pointer you get when opening the file and just come back to that to re-scan the file after you've done your first pass.

How do you reset the file pointer to the start of file? I was trying to figure that out.... hmmm..

And schmu, I'm not too clear about what u mean about overloading a variable. Anyhow, this is a for a lab we're doing for intro to C and the idea is that we have to write a program that counts the number of characters in a text file and then the number of white spaces in a text file. I've broke this process into two functions. The first function counts the total number of characters. We are given that the text file contains only float numbers and white spaces (both tab spaces, new line spaces, and space bar spaces). So what I did was count the total number of characters, and then count the total number of float numbers, take the difference, and that should be the number of white spaces... but ehr, thinking about it just now, I don't think that'll work. -.-"

Because the floats each are a character, if I only subtract 1 for every entire float number, that isn't subtracting enough characters to get the total number of white spaces. Dang, so maybe I need to fscanf integer values until I hit a decimal.... but anyway, I'll figure out the logistics of that later. Any suggestions on getting the input stream to flush?

bla
03-04-2004, 09:57 PM
um, flushing streams just forces whatever data is left in the buffer to be written, flushing input streams as far as i know is meaningless.

the easiest way to reset the file pointer to the begining of a file is rewind();

FILE* your_file;

// open, read, write, whatever...

// return to the start of the file
rewind(your_file);

singularity2006
03-05-2004, 12:10 AM
wh0a, nice! I'll give that a go! Much thanks. =)

singularity2006
03-05-2004, 02:02 AM
Originally posted by bla
um, flushing streams just forces whatever data is left in the buffer to be written, flushing input streams as far as i know is meaningless.

the easiest way to reset the file pointer to the begining of a file is rewind();

FILE* your_file;

// open, read, write, whatever...

// return to the start of the file
rewind(your_file);

oh man, that thing worked PERFECT!! THANKS!! :bowdown:

CGTalk Moderation
01-17-2006, 02:00 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.