Aaarrrrgh! can't even make printf() work...

B

Blankdraw

The following 2 code segs seem to be the cause of my output infinite
loop.
Can someone tell me if this is wrong? I'd like to post the entire
program, but that seems to distract everyone who migh post from the
actual functional glitch.


for (row = 0; row < 52; row++) /* dump array into text file
*/
{
fprintf (fileptr, "\n");
for (col = 1; col < 121; col++) {
fprintf (fileptr, "%2d ", current[row][col]);
}
}

fclose(fileptr);
return 0;
}

******************************************************************

for (row = 0; row < 52; row++)
{
printf("\n");
for (col = 1; col < 121; col++) {
printf ("%i ", current[row][col]);
}
}
 
A

Al Bowers

Blankdraw said:
The following 2 code segs seem to be the cause of my output infinite
loop.
Can someone tell me if this is wrong? I'd like to post the entire
program, but that seems to distract everyone who migh post from the
actual functional glitch.

What is the declaration of the array.
Is it:
int current[52][120]; ?

If so, you have an error in the for loop.
for (row = 0; row < 52; row++) /* dump array into text file
*/
{
fprintf (fileptr, "\n");
for (col = 1; col < 121; col++) {

for(col 0; col < 120,col++) {
fprintf (fileptr, "%2d ", current[row][col]);
}
}

fclose(fileptr);
return 0;
 
N

Nick Austin

The following 2 code segs seem to be the cause of my output infinite
loop.
Can someone tell me if this is wrong? I'd like to post the entire
program, but that seems to distract everyone who migh post from the
actual functional glitch.

Assuming correct definitions for row, col, current and fileptr and
that <stdio.h> is included there is no problem with the code you
posted.

How about a *minimal* but complete program that exhibits the problem?

Nick.
 
D

Darrell Grainger

The following 2 code segs seem to be the cause of my output infinite
loop.
Can someone tell me if this is wrong? I'd like to post the entire
program, but that seems to distract everyone who migh post from the
actual functional glitch.

Looking at the code below I see nothing that will cause an infinite loop.
It must be something you have not included. You want to create a stand
alone snippet that exhibits the same, incorrect, behaviour. Usually, doing
that will make the problem obvious.

What is missing to make this a stand alone program? You need an #include
<stdio.h>, you need variable declarations, you need a main function. Once
you put that together look at it again and see if the problem still
occurs. If it does not then the trick is figuring out what is in the
original program that is missing from the snippet. Again, be the time you
figure that out the problem should be obvious. If not, post the code
snippet that compiles.
for (row = 0; row < 52; row++) /* dump array into text file
*/
{
fprintf (fileptr, "\n");
for (col = 1; col < 121; col++) {
fprintf (fileptr, "%2d ", current[row][col]);
}
}

fclose(fileptr);
return 0;
}

On a guess, are these the variable declarations?

int row, col, current[52][120];

You might have gotten the indices backwards. Why is col = 1 the start for
the inner loop? Just a curiosity but probably not part of your problem.

Another thought, have you stepped through the code with a debugger? How do
you know it is an infinite loop? The fprintf statement might not write to
the file until the fclose occurs. If you look at the file and see it is
not changing, maybe the changes are buffered. Try adding a fflush(fileptr)
after the fprintf.

Another thought, did the fopen work okay? Did you check the results? Is it
using the correct mode?

Anything else will be wild guessing so I'll stop here.
******************************************************************

for (row = 0; row < 52; row++)
{
printf("\n");
for (col = 1; col < 121; col++) {
printf ("%i ", current[row][col]);
}
}
 
F

Fao, Sean

Vijay Kumar Zanvar said:
olaf wrote in message ...

This should be fileptr = fopen ( "c:\\file.txt", "w" )

And you should *always* check the return type.
 
M

Mark McIntyre

The following 2 code segs seem to be the cause of my output infinite
loop.
Can someone tell me if this is wrong? I'd like to post the entire
program, but that seems to distract everyone who migh post from the
actual functional glitch.


for (row = 0; row < 52; row++) /* dump array into text file

your array has 52 rows
for (col = 1; col < 121; col++) {

You're ignoring the first column on purpose?
Note that your array has to have 121 columns for this to be valid.

fprintf (fileptr, "%2d ", current[row][col]);

as long as current is defined properly, this will be fine. However if
its not, then its possible you're overrunning it, and writing over
either row or col, in which case an infinite loop is quite possible.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top