954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problem with EOF and for loop

Hello,
I've got problem with loop for and EOF in it. I want to write every thing from a file on screen it work with one line but i want to read all lines here is the code:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main ()
{
    int i,j,k,l,o,liczba,tab1[4];
    FILE *f = fopen("b.txt", "r");
    {
    // for (o=1;EOF!=0;o++) problem here
    {  
    for (l=0;l<10;l++)
    {
    for(i=0;i<4;i++)
    {
    tab1[i]=0;
    k=0;
    for (j=3;j>=-1;j--)
    {
    k++;
    fread(&i,1,1,f);
    if (i>=48 & i<=57)
       tab1[j]=i-48;
         else
         break;  
    
}
    k=k-1;
    if (k==1)
    liczba=tab1[3];
    if (k==2)
    liczba=tab1[3]*10+tab1[2];
    if (k==3)
    liczba=tab1[3]*100+tab1[2]*10+tab1[1];
    if (k==4)
    liczba=tab1[3]*1000+tab1[2]*100+tab1[1]*10+tab1[0];
    printf("%i;",liczba);
}
}
}
    
    
    fclose(f);
    system("pause");
}
}


Inside of the b.txt

1;12.12.1234;1;2;3;4;5;1;
2;12.12.1234;1;2;3;4;5;2;
3;12.12.1234;1;2;3;4;5;3;

and i am using Dev++
Thanks for help =]

BoB3R
Newbie Poster
16 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

You have a number of problems here.

1. EOF is a constant, it is equal to -1. It never changes. If you what to know when you hit end of file, watch for the value returned by fread(). It will become 0.

2. Your "j" loop is really strange. Do you realize that you have a chance to write outside of tab1?

3. At line 21 fread modifies the value of "i'", which is also a loop counter.

4. I see other problematic places, but I will not comment on them, since I do not understand what you are trying to do.

nezachem
Posting Shark
903 posts since Dec 2009
Reputation Points: 719
Solved Threads: 194
 

Hello,
So it looks like this Ive got a file named "b.txt" created by me in windows and i want to read from it and print it on screen thats it and i didn't know it would be so complicated.

BoB3R
Newbie Poster
16 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

It's complicated only because you complicate things. For a task you stated, a simple loop

while(fread(s, ...) != 0)
    fwrite(s, ...);

suffices.
Can you explain how did you come up with the code you posted?

nezachem
Posting Shark
903 posts since Dec 2009
Reputation Points: 719
Solved Threads: 194
 

It's complicated only because you complicate things. For a task you stated, a simple loop

while(fread(s, ...) != 0)
    fwrite(s, ...);

suffices. Can you explain how did you come up with the code you posted?

Hello,
Do you think it will be reading the line i posted then print it on screen? Ive got in b.txt 5k lines of numbers and how do program will know it is 22 or 2 2, and with this code my teacher helped me, but nvm i will try do it my self

BoB3R
Newbie Poster
16 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 
So it looks like this Ive got a file named "b.txt" created by me in windows and i want to read from it and print it on screen thats it and i didn't know it would be so complicated.

:shrug:

#include <stdio.h>

int main(void)
{
   FILE *file = fopen("b.txt", "r");
   if ( file )
   {
      char line[BUFSIZ];
      while ( fgets(line, sizeof line, file) )
      {
         fputs(line, stdout);
      }
      fclose(file);
   }
   return 0;
}

[edit]BTW:1. EOF is a constant, it is equal to -1. It never changes.Don't concern yourself with any value defined by an implementation (such as -1). It might be highly likely (certainly at this point) that it is different, but it can be any negative integral value.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

EOF is not a character nor a value that exists at the end or middle of a file. It is a macro defined as an int with a negative value. It is a return value of functions that perform read operations (fgetc() and getchar()).

For example, The prototype of fgets() is

char *fgets(char *s, int n, FILE *stream);


The return code of fgets() is NULL when an error occours. The EOF is considered to be an ERROR!

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: