Author Topic: Can't get "fgets()" to compile  (Read 4648 times)

0 Members and 1 Guest are viewing this topic.

Offline MoondeckTopic starter

  • Regular Contributor
  • *
  • Posts: 142
  • Country: dk
  • i really like compilers
Can't get "fgets()" to compile
« on: July 31, 2015, 04:00:25 pm »
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: [Select]
#include <stdio.h>
#include <sys/io.h>

void main()
{
  FILE * pFile
const char fmode[2] = "r";
char strpatch[40];
char buf[3];
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: [Select]
moondeck@Thorium:~/Pulpit$ gcc ParProgmain.c
ParProgmain.c: In function ‘main’:
ParProgmain.c:8:8: error: expected identifier or ‘(’ before ‘char’
  const char fmode[2] = "r";
        ^
ParProgmain.c:17:9: error: expected expression before ‘=’ token
   pFile = fopen (strpatch , "r");
         ^
ParProgmain.c:18:13: error: expected expression before ‘==’ token
   if (pFile == NULL) perror ("Error opening file");
             ^
ParProgmain.c:21:24: error: expected expression before ‘)’ token
   fgets(buf,chnum,pFile);
                        ^
I'm selling 100ml bottles of free energy, PM me for pricing.
 

Offline DmitryL

  • Regular Contributor
  • *
  • Posts: 243
  • Country: gb
Re: Can't get "fgets()" to compile
« Reply #1 on: July 31, 2015, 04:05:45 pm »
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.
 

Offline MoondeckTopic starter

  • Regular Contributor
  • *
  • Posts: 142
  • Country: dk
  • i really like compilers
Re: Can't get "fgets()" to compile
« Reply #2 on: July 31, 2015, 04:08:07 pm »
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.
I'm selling 100ml bottles of free energy, PM me for pricing.
 

Offline WaRc3L

  • Contributor
  • Posts: 11
  • Country: es
Re: Can't get "fgets()" to compile
« Reply #3 on: July 31, 2015, 04:10:01 pm »
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: [Select]
#include <stdio.h>
#include <sys/io.h>

void main()
{
  FILE * pFile
const char fmode[2] = "r";
char strpatch[40];
char buf[3];
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: [Select]
moondeck@Thorium:~/Pulpit$ gcc ParProgmain.c
ParProgmain.c: In function ‘main’:
ParProgmain.c:8:8: error: expected identifier or ‘(’ before ‘char’
  const char fmode[2] = "r";
        ^
ParProgmain.c:17:9: error: expected expression before ‘=’ token
   pFile = fopen (strpatch , "r");
         ^
ParProgmain.c:18:13: error: expected expression before ‘==’ token
   if (pFile == NULL) perror ("Error opening file");
             ^
ParProgmain.c:21:24: error: expected expression before ‘)’ token
   fgets(buf,chnum,pFile);
                        ^

I 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
 

Offline MoondeckTopic starter

  • Regular Contributor
  • *
  • Posts: 142
  • Country: dk
  • i really like compilers
Re: Can't get "fgets()" to compile
« Reply #4 on: July 31, 2015, 04:14:26 pm »
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.
I'm selling 100ml bottles of free energy, PM me for pricing.
 

Online IanB

  • Super Contributor
  • ***
  • Posts: 12267
  • Country: us
Re: Can't get "fgets()" to compile
« Reply #5 on: July 31, 2015, 04:47:53 pm »
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.

Yes, 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.
 

Offline MoondeckTopic starter

  • Regular Contributor
  • *
  • Posts: 142
  • Country: dk
  • i really like compilers
Re: Can't get "fgets()" to compile
« Reply #6 on: July 31, 2015, 05:27:43 pm »
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.
I'm selling 100ml bottles of free energy, PM me for pricing.
 

Offline MoondeckTopic starter

  • Regular Contributor
  • *
  • Posts: 142
  • Country: dk
  • i really like compilers
Re: Can't get "fgets()" to compile
« Reply #7 on: July 31, 2015, 05:34:32 pm »
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: [Select]
#include <stdio.h>
#include <sys/io.h>

void main()
{
  FILE * pFile;
const char fmode[2] = "r";
char strpatch[40];
char buf[3];
char character[3];
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);
}
}
}
I'm selling 100ml bottles of free energy, PM me for pricing.
 

Offline WaRc3L

  • Contributor
  • Posts: 11
  • Country: es
Re: Can't get "fgets()" to compile
« Reply #8 on: July 31, 2015, 05:56:56 pm »
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: [Select]
#include <stdio.h>
#include <sys/io.h>

void main()
{
  FILE * pFile;
const char fmode[2] = "r";
char strpatch[40];
char buf[3];
char character[3];
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);
}
}
}

pFile 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 returns TRUE 1 (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
« Last Edit: July 31, 2015, 05:59:06 pm by WaRc3L »
 

Offline MoondeckTopic starter

  • Regular Contributor
  • *
  • Posts: 142
  • Country: dk
  • i really like compilers
Re: Can't get "fgets()" to compile
« Reply #9 on: July 31, 2015, 06:01:50 pm »
So, if i have a known number of characters in each line, say 3, i just increment the pointer by 3 each time?
I'm selling 100ml bottles of free energy, PM me for pricing.
 

Offline WaRc3L

  • Contributor
  • Posts: 11
  • Country: es
Re: Can't get "fgets()" to compile
« Reply #10 on: July 31, 2015, 06:14:34 pm »
So, if i have a known number of characters in each line, say 3, i just increment the pointer by 3 each time?

No. 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: [Select]
#include <stdio.h>

int main(void){
FILE* pFile;
char hello[100];
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
« Last Edit: July 31, 2015, 06:29:48 pm by WaRc3L »
 

Offline WaRc3L

  • Contributor
  • Posts: 11
  • Country: es
Re: Can't get "fgets()" to compile
« Reply #11 on: July 31, 2015, 06:21:58 pm »
Another case,

If your file has got a structure, like 3 numbers each lines.

3 2 1
5 100 5
55 9900 100

You can take number by number, with fscanf :

Code: [Select]
#include <stdio.h>

int main(void){
FILE* pFile;
char hello[100];
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);
}


 

Offline MoondeckTopic starter

  • Regular Contributor
  • *
  • Posts: 142
  • Country: dk
  • i really like compilers
Re: Can't get "fgets()" to compile
« Reply #12 on: July 31, 2015, 07:09:49 pm »
Now, its working BUT it does NOT take the next line, how do i do this? Tried incrementing pFile, does not work.

Code: [Select]
#include <stdio.h>
#include <sys/io.h>

///////////////////////////////////
//Novamoon ParProg Utility       //
//Written by Moondeck            //
///////////////////////////////////
//WTFPL License                  //
///////////////////////////////////

void main()
{
  FILE * pFile;
const char fmode[2] = "r";
char strpatch[40];
  char buf;
char character[3];
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);
}
}
I'm selling 100ml bottles of free energy, PM me for pricing.
 

Offline WaRc3L

  • Contributor
  • Posts: 11
  • Country: es
Re: Can't get "fgets()" to compile
« Reply #13 on: July 31, 2015, 07:40:49 pm »
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: [Select]
#include <stdio.h>
#include <sys/io.h>

///////////////////////////////////
//Novamoon ParProg Utility       //
//Written by Moondeck            //
///////////////////////////////////
//WTFPL License                  //
///////////////////////////////////

void main()
{
  FILE * pFile;
const char fmode[2] = "r";
char strpatch[40];
  char buf;
char character[3];
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
« Last Edit: July 31, 2015, 07:46:44 pm by WaRc3L »
 

Offline MoondeckTopic starter

  • Regular Contributor
  • *
  • Posts: 142
  • Country: dk
  • i really like compilers
Re: Can't get "fgets()" to compile
« Reply #14 on: July 31, 2015, 07:46:14 pm »
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: [Select]
023
042
000
013
255
etc.
I'm selling 100ml bottles of free energy, PM me for pricing.
 

Offline WaRc3L

  • Contributor
  • Posts: 11
  • Country: es
Re: Can't get "fgets()" to compile
« Reply #15 on: July 31, 2015, 07:50:42 pm »
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: [Select]
023
042
000
013
255
etc.

I 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
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf