How do i loop functions? Any help? thanks
How do i loop functions? Any help? thanks
never ending loops use while(1) or you can use this: for(...)*or something like that*
where should i loop it? in main? do i loop the calling functions in main? or somewhere else
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.
try a for statement. forgot how to do it though......darn
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:Originally posted by Unregistered
where should i loop it? in main? do i loop the calling functions in main? or somewhere else
Code:int main( void ) { /* This is bad */ for(;;) { function(); } return 0; /* For our friend, the compiler */ }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.Code:void function( void ) { /* This is a little better */ for(;;) { /* whatever you want it to do */ } }
On a side note, infinite loops are dubious and should be avoided.
Jason Deckard
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; }
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!
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.
Your way is not 'wrong', but you could also determine the file size and use that as your test.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.
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: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!
[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
[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.
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;
}
--------------------------------------
You need to separate the opening the file statement from the reading loop.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; }
As it stands, you reopen the file each time you call it, so obviously, you will always get the first record.