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 =]

Recommended Answers

All 6 Replies

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.

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.

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?

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

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.

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.