Error: conflicting types for <filepointer>
Hej jag vill printa alla linjer som är över 80 karaktärer långa. Jag vet inte om koden fungerar för jag kan inte testa den på en fil.
Felmeddelande:
program.c:8:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
8 | main() {
| ^~~~
program.c: In function ‘main’:
program.c:15:5: error: conflicting types for ‘fptr’
15 | fptr = fopen("textfile.md", "r");
| ^~~~
program.c:11:11: note: previous declaration of ‘fptr’ was here
11 | FILE *fptr;
| ^~~~
program.c:15:12: warning: initialization of ‘char’ from ‘FILE *’ makes integer from pointer without a cast [-Wint-conversion]
15 | fptr = fopen("textfile.md", "r");
| ^~~~~
program.c:25:25: warning: passing argument 1 of ‘fgetc’ makes pointer from integer without a cast [-Wint-conversion]
25 | if ((c == fgetc(fptr)) != EOF) {
| ^~~~
| |
| char
In file included from program.c:1:
/usr/include/stdio.h:485:25: note: expected ‘FILE *’ but argument is of type ‘char’
485 | extern int fgetc (FILE *__stream);
| ~~~~~~^~~~~~~~
program.c:43:12: warning: passing argument 1 of ‘fclose’ makes pointer from integer without a cast [-Wint-conversion]
43 | fclose(fptr);
| ^~~~
| |
| char
In file included from program.c:1:
/usr/include/stdio.h:213:26: note: expected ‘FILE *’ but argument is of type ‘char’
213 | extern int fclose (FILE *__stream);
| ~~~~~~^~~~~~~~
Kod:
#include <stdio.h>
#include <stdlib.h>
#define FALSE 0
#define TRUE 1
#define MAXLINE 1000
main() {
FILE *fptr;
int i, c, conditional;
char line[MAXLINE],
fptr = fopen("textfile.md", "r");
conditional = TRUE;
i = 0;
while (conditional) {
// Get all acharacters and put them into an array.
// Incrament a variable and check the length with it
// When arriving at /n or EOF, print it if it's long enough.
if ((c == fgetc(fptr)) != EOF) {
line[i] = c;
++i;
if (c == '\n') {
if (i >= 80) {
for (int j = 0; j <= i; ++j ) {
putchar( line[j] );
}
}
i = 0;
}
}
else {
conditional = FALSE;
}
}
fclose(fptr);
}
Löst: problemet var komma istället för semi-kolon vid char :---)