Question:
Trouble with Feof function in while loop?
RBerndt
2010-12-09 11:38:53 UTC
Hello, I'm a very new 'computer programmer' who is having trouble getting a while loop to work without a trailer signal. Im trying to read an unknown amount of unformated data from a file and convert it into the format shown below.

A buddy of mine suggested using the Feof function, but I have no idea how it works. My code is below, currently it just reads the same line over and over.

If someone could give me some pointers on how to properly use a while loop feof combination id be extremely grateful


The data file contains 50+ lines of unsorted data...(top 5 lines as an example)

500001012803102008108305200010
100000010103312008110004000020
610000012301112007304303000025
500002020404232007218155200010

Im supposed to convert the data into this format:

5000010128,03/10/2008,1,08:30,f5.2,000


---------------------------------Here is my Code------------------------------------…

#include
#include
#include

#define FILENAME "machine_samples_1.txt"

int main ()
{
/* Declare and Initialize variables */
int data_pts=0, Identifier, Day, Month, Year, Shift, Hour, Minute, Format_Width, Format_Fractional_Part, Format_Fract_Part, Number_Data_Pts;

FILE* machine_samples;

/* Open file and read first data points*/
machine_samples = fopen(FILENAME,"r");
fscanf(machine_samples,"%10d %02d %2d %4d %1d %02d %02d %1d %1d %05d",&Identifier,&Day,&Month,&Year,&Shi…

while (!feof(machine_samples))
{
printf("%10d," "%02d/" "%2d/" "%4d," "%1d," "%02d:" "%02d," "f%1d" ".%1d," "%05d" ,Identifier,Day,Month,Year,Shift, Hour, Minute, Format_Width, Format_Fract_Part, Format_Fract_Part, Number_Data_Pts);
}

return EXIT_SUCCESS;

}
Three answers:
JoelKatz
2010-12-09 11:44:41 UTC
Put the 'fscanf' inside the loop. A second copy of the 'fscanf' line before the close brace the ends the 'while' loop should do it.
2010-12-09 11:48:29 UTC
while (!feof(machine_samples))



You have to read the file in here to trigger eof. If you haven't reached eof when you hit the while, you'll be in the loop forever. If you have you'll never enter the loop.



Also - you're not formatting anything. You're reading 10 decimal variables:



5000010128 03 10 2008 1 08 30 5 2 00010
riveros
2016-12-12 21:55:24 UTC
there are various sorts of at the same time as loop purposes, as you've were given customary via now, at the same time as()...,do..at the same time as etc. As on your project, an undemanding at the same time as loop do the job.at the same time as loop purposes like this: int i=a million; at the same time as(i<10) { i++; } The loop runs so long because the cost of variable 'i' is decrease than 10(ie i<10) and when you consider that the cost of i has been higher repeated via one in each and each loop, the loop runs for 10 cases, and so does the alongside with it. on your particular celebration that has similarities: int i; scanf("%d",&i); at the same time as(i!=10) { printf("regardless of"); scanf("%d",&i); } right here, the loop keeps on operating so long because the person would not enter the cost of i as 10.If the person inputs 10, the loop breaks.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...