How to loop a function

This is a discussion on How to loop a function within the C Programming forums, part of the General Programming Boards category; How do i loop functions? Any help? thanks...

  1. #1
    Unregistered
    Guest

    How to loop a function

    How do i loop functions? Any help? thanks

  2. #2
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    never ending loops use while(1) or you can use this: for(...)*or something like that*

  3. #3
    Unregistered
    Guest
    where should i loop it? in main? do i loop the calling functions in main? or somewhere else

  4. #4
    Unregistered
    Guest
    i created a function that reads a line of data in a text of data until EOF. i also created a print function that will print the data, but all i get is just one line of data.

  5. #5
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    try a for statement. forgot how to do it though......darn

  6. #6
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Originally posted by Unregistered
    where should i loop it? in main? do i loop the calling functions in main? or somewhere else
    If you want the function to loop indefinitely, then include your while(1) or for(;;) in the function itself. Avoid creating an infinite loop that continuously calls the function:
    Code:
    int main( void )
    {
      /* This is bad */
    
      for(;;)
      {
        function();
      }
    
      return 0; /* For our friend, the compiler */
    }
    Code:
    void function( void )
    {
      /* This is a little better */
    
      for(;;)
      {
        /* whatever you want it to do */
      }
    }
    The difference is you are not repeatedly pushing and popping the function on/off the stack in the second example, which makes it more efficient.

    On a side note, infinite loops are dubious and should be avoided.
    Jason Deckard

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Do not test for EOF!!!!!!

    Some files contain values equiv to this value and so results are unpredictable!

    Always test the file pointer with feof(), as in:

    while(!feof(fp))
    //...copy data...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Unregistered
    Guest
    so i created a while loop in the function readData to read the whole notepad text. The function readData reads the text file line by line until end of file because for each of the line, i need to test for something. I have another function called printData to print the data. So i get the data with readData and pass by reference to main, but the printData function only prints the first line of the data. Unfortunately, the readData only passes the first line of data to main. I want the readData function to pass all lines of the data by reference to main or to printData function, how would i do that? thanks!

  9. #9
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    I agree with Salem, however, loading the file line by line is a waste of time. Do a block load to a buffer and then operate or scan through the buffer. Get the disk access part done quickly and then alter the image in memory just as though you were accessing an array. Some people say to use objects with text files, but this only complicates an extremely simple process. Read the data into a linear array and work from there. When done with the changes, write the new array to the file thus changing the files contents.

    C provides a multitude of disk i/o functions to use.

  10. #10
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Originally posted by Unregistered
    The function readData reads the text file line by line until end of file because for each of the line, i need to test for something.
    Your way is not 'wrong', but you could also determine the file size and use that as your test.

    Originally posted by Unregistered
    I have another function called printData to print the data. So i get the data with readData and pass by reference to main, but the printData function only prints the first line of the data. Unfortunately, the readData only passes the first line of data to main. I want the readData function to pass all lines of the data by reference to main or to printData function, how would i do that? thanks!
    Salem has a point; code would be nice. If you knew the file's size, you could allocate memory to hold the entire file (assuming the box has the memory). Something along these lines may work for you:
    [list=1][*]Open file and determine size of target file.[*]Allocate memory to hold the contents of the file.[*]Pass the file's descriptor, size, and a pointer to the buffer as arguments to your readData() function.[*]readData() from the file into the buffer. Since it knows the file size, it should know when to stop.[*]Let readData() return and close your file.[*]Call printData() with the pointer to the buffer and the file size (which is also the length of the buffer).[/list=1]
    Jason Deckard

  11. #11
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    [quote]
    Do not test for EOF!!!!!!

    Some files contain values equiv to this value and so results are unpredictable!

    Always test the file pointer with feof(), as in:

    while(!feof(fp))
    //...copy data...
    [quote]

    Simply not true. It is true that the EOC mark or end of cluster mark can be mis-interpreted since in FAT32 the EOC mark could also actually refer to a valid cluster (this cannot happen in FAT 12/16). However, MS states that at no time will the DOS or Windows operating system allow a cluster number equal to EOC in FAT32 be used or referenced in the FAT. Other FAT drivers that allow this are not in compliance with the MS FAT standard and should not be used.

    It is possible to truncate a cluster chain midway by marking the cluster as EOC in the FAT. However, this just wastes disk space since the last half of the file can never be accessed. Also, FAT file drivers will not use the remainder of the cluster chain after the EOC because the cluster values are not 0 or empty. It will not write to used clusters - this would create a mess of crosslinked clusters and would destroy data and possible necessitate a reformat to restore the disk file system to working order.

    All of this info only applies to FAT family of file systems. NTFS and others use different storage systems.


    EOC - value in FAT
    0 - Cluster is free
    FAT 12 - 0FF8h-0FFFh
    FAT 16 - FFF8h-FFFFh
    FAT 32 - FFFFFFF8h-FFFFFFFFh

    Since DOS/Windows FAT drivers will effectively skip cluster FFFFFFF8h-FFFFFFFFh, then EOC can never point to a valid cluster.

    So if you mess around with the FAT and truncate your files midway, you will waste

    ((SecPerCluster*BytesPerSector)*ClustersLeftInChai n) bytes.

    BytesPerSector is normally 512, but MS FAT drivers will work with larger values although they are not recommended.

    Not a good idea. If you for some odd reason want to try this, make sure and zero out all remaining clusters after you place the EOC mark to ensure that they will be used by the FAT driver later.

  12. #12
    Unregistered
    Guest
    Hi everybody, this is my code. I am a beginner, so i don't know what u guys were talking about something buffer. but thanks for the help, but i still need some more help. any more help i would really appreciate. thanks
    -------------------------------------------------------
    #include <stdio.h>
    #include <stdlib.h>

    void readInventory (int *partNum, float *price, int *quantity, int *reorder, int *minOrder);
    int calcOrderAmount (int quantity, int reorder, int minOrder);
    void printInventory (int partNum, float price, int quantity, int reorder, int minOrder, int orderAmount);

    int main()
    {

    int partNum, quantity, reorder, minOrder;
    float price;
    int orderA;


    readInventory(&partNum, &price, &quantity, &reorder, &minOrder);
    orderA = calcOrderAmount(quantity, reorder, minOrder);
    printInventory(partNum, price, quantity, reorder, minOrder, orderA);

    system("PAUSE");
    return 0;
    }
    void readInventory (int *partNum, float *price, int *quantity, int *reorder, int *minOrder)
    {
    FILE *fpText;

    fpText = fopen("inventory.txt", "r");

    while ((fscanf(fpText, "%d %f %d %d %d", partNum, price, quantity, reorder, minOrder)) != EOF)

    return;
    }
    int calcOrderAmount (int quantity, int reorder, int minOrder)
    {
    int orderAmount;
    if (quantity < reorder)
    {
    orderAmount = (reorder + minOrder) - quantity;
    return orderAmount;
    }
    else
    return 0;
    }

    void printInventory (int partNum, float price, int quantity, int reorder, int minOrder, int orderA)
    {

    printf("\tINVENTORY REPORT\n");
    printf("PartNo. Price Quantity Reorder Min.Order OrderAmount\n");
    printf("%04d %7.2f %6d %7d %7d %9d\n", partNum, price, quantity, reorder, minOrder, orderA);

    return;
    }
    --------------------------------------

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    Code:
    void readInventory (int *partNum, float *price, int *quantity, int *reorder, int *minOrder) 
    { 
    FILE *fpText; 
    
    fpText = fopen("inventory.txt", "r"); 
    
    while ((fscanf(fpText, "%d %f %d %d %d", partNum, price, quantity, reorder, minOrder)) != EOF) 
    
    return; 
    }
    You need to separate the opening the file statement from the reading loop.
    As it stands, you reopen the file each time you call it, so obviously, you will always get the first record.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function (modules) and loop help.
    By GCNDoug in forum C Programming
    Replies: 5
    Last Post: 04-07-2007, 01:43 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  4. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 10:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21