Can't get "fgets()" to compile - Page 1
Forum section: electronics / beginners

author: Moondeck
author_rank: Regular Contributor
timestamp: July 31, 2015, 04:00:25 pm
content: Hello, i am doing a parallel port memory programmer project (too many P's) , and i am writing software for it, but i cannot get it to compile, take a look (Its unfinished):Code:#include <stdio.h>#include <sys/io.h>void main(){FILE * pFileconst char fmode = "r";char strpatch;char buf;int chnum = 3;printf("Welcome to Novamoon ParProg Programming utility v0.1a \n");printf("Please specify a .mprog file: ");scanf("%s",strpatch);printf("\nTrying to flash file at:");printf(strpatch,"\n");pFile = fopen (strpatch , "r");if (pFile == NULL) perror ("Error opening file");else{fgets(buf,chnum,pFile);printf(buf,"\n");}}And the error code:Code:moondeck@Thorium:~/Pulpit$ gcc ParProgmain.cParProgmain.c: In function ‘main’:ParProgmain.c:8:8: error: expected identifier or ‘(’ before ‘char’const char fmode = "r";^ParProgmain.c:17:9: error: expected expression before ‘=’ tokenpFile = fopen (strpatch , "r");^ParProgmain.c:18:13: error: expected expression before ‘==’ tokenif (pFile == NULL) perror ("Error opening file");^ParProgmain.c:21:24: error: expected expression before ‘)’ tokenfgets(buf,chnum,pFile);^

author: DmitryL
author_rank: Regular Contributor
timestamp: July 31, 2015, 04:05:45 pm
content: I can advise to post your question to StackOverflow and like forums..They will gladly help you to place missing semicolons in right positions in your souce code.

author: Moondeck
author_rank: Regular Contributor
timestamp: July 31, 2015, 04:08:07 pm
content: I've posted on this forum, because i have always got help here, and nobody has ever been a dickhead towards a noob like me.

author: WaRc3L
author_rank: Contributor
timestamp: July 31, 2015, 04:10:01 pm
content: Quote from: Moondeck on July 31, 2015, 04:00:25 pmI guess is FILE* pFile;.I copy&paste the code, and I compile it, but makes me an error that library sys/io isn't found. I suppose that you have it.Regards, I hope that works.Marcel

author: Moondeck
author_rank: Regular Contributor
timestamp: July 31, 2015, 04:14:26 pm
content: LMAO, that fixed it. Cant belive it was such a lame problem. Anyway, not closing the thread yet, in case i would have other problems.

author: IanB
author_rank: Super Contributor
timestamp: July 31, 2015, 04:47:53 pm
content: Quote from: Moondeck on July 31, 2015, 04:14:26 pmYes, when you get a string of weird errors like that always suspect something wrong or missing right before the place where the first error is reported. After a while you get a feel for these things.

author: Moondeck
author_rank: Regular Contributor
timestamp: July 31, 2015, 05:27:43 pm
content: Yeah, its actually my first computer C program (other than hello world and shit), i have been only using microcontrollers before, so it was more about registers not actual C stuffs.

author: Moondeck
author_rank: Regular Contributor
timestamp: July 31, 2015, 05:34:32 pm
content: Now, how can i each time read next line? so like, take line 1, print it, go to line 2, print it, etc.Code here:Code:#include <stdio.h>#include <sys/io.h>void main(){FILE * pFile;const char fmode = "r";char strpatch;char buf;char character;int chnum = 3;printf("Welcome to Novamoon ParProg Programming utility v0.1a \n");printf("Please specify a .mprog file: ");scanf("%s",strpatch);printf("\nTrying to flash file at:\n");printf(strpatch,"\n");pFile = fopen (strpatch , "r");if (pFile == NULL) printf("Error opening file, press Ctrl+C to terminate\n");else{fgets(buf,chnum,pFile);character = buf;if((character != "EOF")){printf(buf);printf(" \n");}else{printf("End of File, Programming success!\n");printf("Press Ctrl+C to terminate");while(1);}}}

author: WaRc3L
author_rank: Contributor
timestamp: July 31, 2015, 05:56:56 pm
content: Quote from: Moondeck on July 31, 2015, 05:34:32 pmpFile is a pointer into the file, that means pFile is the responsable to move the "cursor" to read the lines. You can get line by line after call fgets function, because, if my memory serves me, fgets reads until a new line.Why you use character=buff and then compare it with EOF?. Exist a function, feof() that returnsTRUE1 (in C doesn't exist booleans types)if is end of file. Furthermore, in C, is recommended to use function like strcmp() to manipulate strings.And, always you open a file, you must close it with fclose(). In addition, if the file has structure, I recommend you use a scanf function instead a fgets.Look this example :http://www.cplusplus.com/reference/cstdio/FILE/.Regards,Marcel

author: Moondeck
author_rank: Regular Contributor
timestamp: July 31, 2015, 06:01:50 pm
content: So, if i have a known number of characters in each line, say 3, i just increment the pointer by 3 each time?

author: WaRc3L
author_rank: Contributor
timestamp: July 31, 2015, 06:14:34 pm
content: Quote from: Moondeck on July 31, 2015, 06:01:50 pmNo. If you want the entire line, use fgets only. If the three numbers are important (the file has got a structure), use fscanf.Look the file that I attached.This file has not got any structure. We can use fgets like this :Code:#include <stdio.h>int main(void){FILE* pFile;char hello;pFile = fopen("file.txt", "r");int i=1;while(!feof(pFile)){fgets(hello, 100, pFile);printf("Line %d : %s", i,hello);i++;//system("pause");}fclose(pFile);}system("pause"), if you use Windows, is equivalent you wrote on the final.I hope that I explained well with the code. I attached the output as well.pD: Your code doesn't work because fgets is not into any bucle. You only read one line, and then the program finishs. while(!feof(X)) is equivalent to while(feof(X)!=1) and while(feof(X)==0). ! is the not operator.Regards,Marcel

author: WaRc3L
author_rank: Contributor
timestamp: July 31, 2015, 06:21:58 pm
content: Another case,If your file has got a structure, like 3 numbers each lines.3 2 15 100 555 9900 100You can take number by number, with fscanf :Code:#include <stdio.h>int main(void){FILE* pFile;char hello;int first, second, third;pFile = fopen("file.txt", "r");int i=1;while(!feof(pFile)){fscanf(pFile, "%d %d %d", &first, &second, &third );printf("Line %d : %d, %d, %d\n", i, first, second, third);i++;//system("pause");}fclose(pFile);}

author: Moondeck
author_rank: Regular Contributor
timestamp: July 31, 2015, 07:09:49 pm
content: Now, its working BUT it does NOT take the next line, how do i do this? Tried incrementing pFile, does not work.Code:#include <stdio.h>#include <sys/io.h>/////////////////////////////////////Novamoon ParProg Utility ////Written by Moondeck ///////////////////////////////////////WTFPL License /////////////////////////////////////void main(){FILE * pFile;const char fmode = "r";char strpatch;char buf;char character;int chnum = 3;int i = 1;printf("Welcome to Novamoon ParProg Programming utility v0.1a \n");printf("Please specify a .mprog file: ");scanf("%s",strpatch);printf("\nTrying to flash file at:\n");printf(strpatch,"\n");pFile = fopen (strpatch , "r");if (pFile == NULL){printf("\nError opening file, press Ctrl+C to terminate\n");}else{fscanf(pFile, "%d", &buf,"\n");while(feof(pFile) != 1){printf("\nLine %d : %d \n",i,buf);i++;}printf("\nEnd of File, Programming success!\n");printf("Press Ctrl+C to terminate");while(1);}}

author: WaRc3L
author_rank: Contributor
timestamp: July 31, 2015, 07:40:49 pm
content: It waits into the infinite loop while(feof(pFile) != 1). The function fscanf, and fgets, increments the pFile for you. You don't need to do nothing, but, if you want to read more than one line, you need to put that sentence into any loop.Put fscanf into while(feof(pFile)!=1) and execute then. You need to close the file (is a good practice ever that your program works). Why is \n there?, \n is not sense there..., fscanf reads a line for you.And, %d, is for "integers", not for buffers. You need to change it to "%s", or "%c" to characters. Buf is a char, not a string. Can you post a the file that you want to read? (is one number for each line?)Code:#include <stdio.h>#include <sys/io.h>/////////////////////////////////////Novamoon ParProg Utility ////Written by Moondeck ///////////////////////////////////////WTFPL License /////////////////////////////////////void main(){FILE * pFile;const char fmode = "r";char strpatch;char buf;char character;int chnum = 3;int i = 1;printf("Welcome to Novamoon ParProg Programming utility v0.1a \n");printf("Please specify a .mprog file: ");scanf("%s",strpatch);printf("\nTrying to flash file at:\n");printf(strpatch,"\n");pFile = fopen (strpatch , "r");if (pFile == NULL){printf("\nError opening file, press Ctrl+C to terminate\n");}else{while(feof(pFile) != 1){printf("\nLine %d : %c \n",i,buf);fscanf(pFile, "%c", &buf); // You need to fscanf every iterate in the loop. And, why \n here?i++;}fclose(pFile); // !printf("\nEnd of File, Programming success!\n");printf("Press Ctrl+C to terminate");while(1);}}Regards,Marcel

author: Moondeck
author_rank: Regular Contributor
timestamp: July 31, 2015, 07:46:14 pm
content: THANKS! IT WORKS NOW! Now, i need to do the actual parallel port driving, but from what i can see, its going to be pretty simple. Thanks for your help. the file structure is:Code:023042000013255etc.

author: WaRc3L
author_rank: Contributor
timestamp: July 31, 2015, 07:50:42 pm
content: Quote from: Moondeck on July 31, 2015, 07:46:14 pmI forget to close the file into code. Close it before while(1);.Ok, if the file is only numbers, I recommend you to change char to int the declaration of the buf variable, and then, change "%c", that I wrote, to "%d" (what you already have).Regards,Marcel