Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

fgets

Posted on 2011-10-12
5
Medium Priority
?
701 Views
Last Modified: 2012-05-12
I'm coming from the java world and c is leaving me confused
This should hopefully return the first line of a file but I can't get it to compile
#include <stdio.h>
#include <stdlib.h>
#define MAX_LEN 100

int main(int argc,char *argv[]) {
	FILE input;
	FILE output;
	char *in_line;
	input = fopen(argv[1], "r");
	fgets(in_line, MAX_LEN, input);
	printf(in_line);
	return EXIT_SUCCESS;
}

Open in new window

Chapter 1.c: In function ‘main’:
Chapter 1.c:19:2: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘input’
Chapter 1.c:19:8: error: incompatible types when assigning to type ‘FILE’ from type ‘struct FILE *’
Chapter 1.c:20:8: error: ‘in_line’ undeclared (first use in this function)
Chapter 1.c:20:8: note: each undeclared identifier is reported only once for each function it appears in
Chapter 1.c:20:2: error: incompatible type for argument 3 of ‘fgets’
/usr/include/stdio.h:606:14: note: expected ‘struct FILE * __restrict__’ but argument is of type ‘FILE’
mike@linux-1x7z:~/C projects/Data Structures Chapter 1/src> gcc Chapter\ 1.c -o test
Chapter 1.c: In function ‘main’:
Chapter 1.c:19:8: error: incompatible types when assigning to type ‘FILE’ from type ‘struct FILE *’
Chapter 1.c:20:2: error: incompatible type for argument 3 of ‘fgets’
/usr/include/stdio.h:606:14: note: expected ‘struct FILE * __restrict__’ but argument is of type ‘FILE’

Open in new window

0
Comment
Question by:GPicasso
  • 4
5 Comments
 
LVL 16

Expert Comment

by:imladris
ID: 36959240
Looks, for starters, like "input" is considered a keyword. Change it to something else (like "inp") and see whether it removes any of the errors.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36959242
this works for me:

#include <stdio.h>
#include <stdlib.h>
#define MAX_LEN 100

int main(int argc,char *argv[]) {
	FILE *input;
	//FILE *output;
	char in_line[100];
	input = fopen(argv[1], "r");
	fgets(in_line, MAX_LEN, input);
	printf(in_line);
	return EXIT_SUCCESS;
}

Open in new window

0
 
LVL 47

Accepted Solution

by:
for_yan earned 200 total points
ID: 36959247
input should be the poiinter:

FILE *input;

and  also I changed to static allocation of char array

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36959248
so I compiled it with gcc, it produced a.out
and even read and printed the first line of the file
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36959260
this is how it works with dynamic allocation of memory for array:
#include <stdio.h>
#include <stdlib.h>
#define MAX_LEN 100

int main(int argc,char *argv[]) {
	FILE *input;
	//FILE *output;
	char *in_line;
	  in_line = (char*)calloc(100, sizeof(char));
	input = fopen(argv[1], "r");
	fgets(in_line, MAX_LEN, input);
	printf(in_line);
	return EXIT_SUCCESS;
}

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses
Course of the Month21 days, 3 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question