Don't use feof in the control of a loop - i.e. That while loop.
Here is another FAQ explaining why...
Question 12.2
Don't use feof in the control of a loop - i.e. That while loop.
Here is another FAQ explaining why...
Question 12.2
Hi I appreciate your guys your effort to help me.
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
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 } }
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
A minor optimization:
Notice sscanf() will return the number of consecutive conversions made.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... ... } }
OOOOppps! @laserlight caught an error here... fgets() will return NULL on error or EOF.
Sorry...
Last edited by flp1969; 05-05-2019 at 06:00 AM.
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.Originally Posted by flp1969
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
Stop saying "you're stuck" and post the latest code.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
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 ..
FILE TXT is the same as: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; }
60
4 1 4
607
4 5 6
3 1
4 2
Last edited by RyanC; 05-05-2019 at 09:56 AM.
> char line[100000000];
What are you smoking!?
Pick a reasonable size, like BUFSIZ
100MB FFS
Sheesh!
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
[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; }
Stop being an idiot, and make a decent buffer size.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
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 !
Nothing at all?
Not even your error message?
Works for me.
Yes, your 2nd printf was broken.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
Are compiler messages preventing you from making a valid executable?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.