1. Don't use feof in the control of a loop - i.e. That while loop.

Here is another FAQ explaining why...
Question 12.2

my problem is this:
I want to read the data from txt file line by line which that data is like this pattern:
when the length of the data line of the txt file is three numbers as passing those three numbers(integers) to fun1 .
when the length of the data line of the txt file is two numbers as passing those two numbers (integers) to func2.
when the length of the data of the line of txt file is one number as passing one number to fun3.

for example if i have txt file like this:

1 2 3
3 4 5
3 4
2
4 5 6
then I want like this to do:
first line goes with its parameters to func1(int x=1,int y=2,int z=3)
second line goes with its parameters to func1(3,4,5)
third line goes with its parameters to func2(3,4)
fourth line goes with its parameters to func3(2)
fifth line goes with its parameters to func1(4,5,6);

any clue how can I start? I tried the code that already posted here but didn't work well.
there's no specified pattern for the number of integers in line, it might be one integer in line or two integer in line or three integers in line. (max three integer in line)

I tried also to pick up line by line as string and split it up but didn't work!

thanks alot

3. How's this?
Code:
```char buff[BUFSIZ];
while ( fgets(buff,BUFSIZ,filep) ) {
int v1,v2,v3;
if ( sscanf(buff,"%d %d %d", &v1, &v2, &v3) == 3 ) {
func1(v1,v2,v3);
}
else if ( sscanf(buff,"%d %d", &v1, &v2) == 2 ) {
func2(v1,v2);
}
else if ( sscanf(buff,"%d", &v1) == 1 ) {
func3(v1);
} else {
// it's borked
}
}```

4. Originally Posted by Salem
How's this?
Code:
```char buff[BUFSIZ];
while ( fgets(buff,BUFSIZ,filep) ) {
int v1,v2,v3;
if ( sscanf(buff,"%d %d %d", &v1, &v2, &v3) == 3 ) {
func1(v1,v2,v3);
}
else if ( sscanf(buff,"%d %d", &v1, &v2) == 2 ) {
func2(v1,v2);
}
else if ( sscanf(buff,"%d", &v1) == 1 ) {
func3(v1);
} else {
// it's borked
}
}```
A minor optimization:
Code:
```char buff[BUFSIZ];

while ( fgets(buff,BUFSIZ,filep) ) {
int v1,v2,v3,count;

count = sscanf("%d %d %d", &v1, &v2, &v3 );
switch ( count )
{
case 1: func1(v1); break;
case 2: func2(v1,v2); break;
case 3: func3(v1,v2,v3); break;
default: // error handlning...
...
}
}```
Notice sscanf() will return the number of consecutive conversions made.

OOOOppps! @laserlight caught an error here... fgets() will return NULL on error or EOF.
Sorry...

5. Originally Posted by flp1969
Notice the test against EOF
Isn't that wrong, though? fgets returns a pointer to char, not an int, hence Salem's original implicit test for a non-null pointer is correct.

6. Originally Posted by laserlight
Isn't that wrong, though? fgets returns a pointer to char, not an int, hence Salem's original implicit test for a non-null pointer is correct.
You are absolutely RIGHT! Sorry...
The snippet was edited....

7. Originally Posted by laserlight
Isn't that wrong, though? fgets returns a pointer to char, not an int, hence Salem's original implicit test for a non-null pointer is correct.
Hi laser, I appreciate your help, and thanks for clarifications. also thanks for flp to make an effort.

I'm still stuck in and I tried many methods like getting line by line as string and splitting it to get every "number" and afterwards converting it by atoi function .. sounds horrible but didn't help me because I always arrive to null pointer error and have many errors in that method.
I tried the method that I declared here before but didn't help.

May you help me please how can I solve the problem? thanks alot.

8. Stop saying "you're stuck" and post the latest code.

9. Hi salem; this is my final code but it's not printing at all the parameters!!

for example I just used printf function for func1, func2,func3 ..

Code:
```int main()
{
int v1, v2, v3;
char line[100000000];
FILE *file = fopen("FILE.txt", "r");
if (file != NULL)
{
/* or other suitable maximum line size */
while (fgets(line, sizeof line, file) != NULL)
{
if (sscanf(line, "%d %d %d", &v1, &v2, &v3) == 3)
{
printf("%d %d %d\n", v1, v2, v3);
} else if (sscanf(line, "%d %d", &v1, &v2) == 2) {
printf("%d %d %d\n", v1, v2);
} else if (sscanf(line, "%d", &v1) == 1) {
printf("%d\n", v1);
}
}
}
else
{
printf("error file openning");
}
return 0;
}```
FILE TXT is the same as:
60
4 1 4
607
4 5 6
3 1
4 2

10. > char line[100000000];
What are you smoking!?

Pick a reasonable size, like BUFSIZ

100MB FFS
Sheesh!

11. Originally Posted by Salem
> char line[100000000];
What are you smoking!?

Pick a reasonable size, like BUFSIZ

100MB FFS
Sheesh!
it's not working either!
I changed to 100MB it, it runs and there's not output, it's just running and at the end tell me in command line : Process finished with exit code -1073741571 (0xC00000FD)

12. [code]
Code:
```int main()
{
int v1, v2, v3;
char line[104857600];
FILE *file = fopen("FILE.txt", "r");
if (file != NULL)
{
/* or other suitable maximum line size */
while (fgets(line, sizeof line, file) != NULL)
{
if (sscanf(line, "%d %d %d", &v1, &v2, &v3) == 3)
{
printf("%d %d %d\n", v1, v2, v3);
} else if (sscanf(line, "%d %d", &v1, &v2) == 2) {
printf("%d %d %d\n", v1, v2);
} else if (sscanf(line, "%d", &v1) == 1) {
printf("%d\n", v1);
}
}
}
else
{
printf("error file openning");
}
return 0;
}```

13. Stop being an idiot, and make a decent buffer size.

14. Originally Posted by Salem
Stop being an idiot, and make a decent buffer size.
Hi salem, I've done this code
Code:
```int main()
{
int v1, v2, v3;
char line[10000];
FILE *file = fopen("FILE.txt", "r");
if (file != NULL)
{
/* or other suitable maximum line size */
while (fgets(line, sizeof line, file) != NULL)
{
if (sscanf(line, "%d %d %d", &v1, &v2, &v3) == 3)
{
printf("%d %d %d\n", v1, v2, v3);
} else if (sscanf(line, "%d %d", &v1, &v2) == 2) {
printf("%d %d %d\n", v1, v2);
} else if (sscanf(line, "%d", &v1) == 1) {
printf("%d\n", v1);
}
}
}
else
{
printf("error file openning");
}
return 0;
}
```

it didn't output anything , I'm not kidding !

15. Nothing at all?

Works for me.
Code:
```\$ cat foo.c
#include <stdio.h>
int main()
{
int v1, v2, v3;
char line[BUFSIZ];
FILE *file = fopen("foo.txt", "r");
if (file != NULL)
{
/* or other suitable maximum line size */
while (fgets(line, sizeof line, file) != NULL)
{
if (sscanf(line, "%d %d %d", &v1, &v2, &v3) == 3)
{
printf("V3 = %d %d %d\n", v1, v2, v3);
} else if (sscanf(line, "%d %d", &v1, &v2) == 2) {
printf("V2 = %d %d\n", v1, v2);      //!! WAS foo.c:16:24: warning: format ?%d? expects a matching ?int? argument [-Wformat=]
} else if (sscanf(line, "%d", &v1) == 1) {
printf("V1 = %d\n", v1);
}
}
}
else
{
printf("error file openning");
}
return 0;
}
\$ cat foo.txt
60
4 1 4
607
4 5 6
3 1
4 2
\$ gcc foo.c
\$ ./a.out
V1 = 60
V3 = 4 1 4
V1 = 607
V3 = 4 5 6
V2 = 3 1
V2 = 4 2```
Yes, your 2nd printf was broken.

Are compiler messages preventing you from making a valid executable?