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

Discussion in 'C Programming' started by Blankdraw, Aug 20, 2003.

  1. Blankdraw

    Blankdraw Guest

    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]);
    }
    }
     
    Blankdraw, Aug 20, 2003
    #1
    1. Advertising

  2. Blankdraw

    Al Bowers Guest

    Blankdraw wrote:
    > 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;
    >


    --
    Al Bowers
    Tampa, Fl USA
    mailto: (remove the x)
    http://www.geocities.com/abowers822/
     
    Al Bowers, Aug 20, 2003
    #2
    1. Advertising

  3. Blankdraw

    Nick Austin Guest

    On 19 Aug 2003 20:02:16 -0700, (Blankdraw) wrote:

    >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.
     
    Nick Austin, Aug 20, 2003
    #3
  4. On 19 Aug 2003, Blankdraw wrote:

    > 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]);
    > }
    > }
    >


    --
    main(){int j=1234;char t[]=":mad:abcdefghijklmnopqrstuvwxyz.\n",*i=
    "iqgbgxmdbjlgdv.lksrqek.n";char *strchr(const char *,int);while(
    *i){j+=strchr(t,*i++)-t;j%=sizeof t-1;putchar(t[j]);} return 0;}
     
    Darrell Grainger, Aug 20, 2003
    #4
  5. Blankdraw

    Fao, Sean Guest

    "Vijay Kumar Zanvar" <> wrote in message
    news:bhve6s$37ioa$-berlin.de...
    >
    >
    >
    > olaf wrote in message ...
    > >Hello
    > >
    > >The only thing I can get up is did you open the file. you close it,
    > >but where do you open it.
    > >
    > >Greetings Olaf
    > >
    > >int dump(int **current)
    > >{
    > >FILE *fileptr=NULL;
    > > fileptr = fopen("c://file.txt","w");

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


    And you should *always* check the return type.
     
    Fao, Sean, Aug 20, 2003
    #5
  6. On 19 Aug 2003 20:02:16 -0700, in comp.lang.c ,
    (Blankdraw) wrote:

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


    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
     
    Mark McIntyre, Aug 20, 2003
    #6
  7. Blankdraw

    Randy Howard Guest

    In article <qSO0b.11$>, -
    WANT-NO-SPAM says...
    > > > fileptr = fopen("c://file.txt","w");

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

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


    type? :)
     
    Randy Howard, Aug 21, 2003
    #7
  8. Blankdraw

    Alex Guest

    Fao, Sean <-want-no-spam> wrote:

    > "Vijay Kumar Zanvar" <> wrote in message
    > news:bhve6s$37ioa$-berlin.de...
    >>
    >>
    >>
    >> olaf wrote in message ...
    >> >Hello
    >> >
    >> >The only thing I can get up is did you open the file. you close it,
    >> >but where do you open it.
    >> >
    >> >Greetings Olaf
    >> >
    >> >int dump(int **current)
    >> >{
    >> >FILE *fileptr=NULL;
    >> > fileptr = fopen("c://file.txt","w");

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


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


    ITYM: value

    Alex
     
    Alex, Aug 21, 2003
    #8
  9. Blankdraw

    Fao, Sean Guest

    Randy Howard <> wrote in message news:<>...
    > In article <qSO0b.11$>, -
    > WANT-NO-SPAM says...
    > > > > fileptr = fopen("c://file.txt","w");
    > > >
    > > > This should be fileptr = fopen ( "c:\\file.txt", "w" )

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

    >
    > type? :)


    Oops, sorry about that! I mean value.
     
    Fao, Sean, Aug 22, 2003
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. craig dicker
    Replies:
    9
    Views:
    704
    Juan T. Llibre
    Jul 7, 2005
  2. Stan Goodman

    Even older fart, even newer newbie

    Stan Goodman, Jul 3, 2003, in forum: Java
    Replies:
    11
    Views:
    722
    Stan Goodman
    Jul 4, 2003
  3. Xeno Campanoli
    Replies:
    1
    Views:
    364
    James Britt
    Jul 1, 2005
  4. Schklerg
    Replies:
    7
    Views:
    135
    Mike Scirocco
    Nov 30, 2003
  5. Replies:
    2
    Views:
    102
    Dr John Stockton
    Jun 27, 2006
Loading...

Share This Page