Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using fgets() with char* type

Tags:

c

string

char

fgets

I have a simple question about using fgets() with char* string.

....
char *temp;
FILE fp=fopen("test.txt", "r");

fgets(temp, 500, fp);
printf("%s", temp);
....

This code didn't work well.

But after I modified char *temp to char temp[100];, the code worked well as I intended.

What is the difference between those two?

When I googled it, some said that memory must be allocated to char * using malloc()...

But I couldn't understand it.

like image 763
user3033077 Avatar asked Nov 25 '13 16:11

user3033077


People also ask

Does fgets work with pointers?

The fgets() function returns the pointer to the string buffer if anything was written to it, or a null pointer if an error occurred or if the file position indicator was at the end of the file.

What does fgets () do in C?

fgets() function in C The function reads a text line or a string from the specified file or console. And then stores it to the respective string variable. Similar to the gets() function, fgets also terminates reading whenever it encounters a newline character.

Does fgets work with stdin?

Since fgets() reads input from user, we need to provide input during runtime. Reads characters from the standard input (stdin) and stores them as a C string into str until a newline character or the end-of-file is reached.

How use fgets function?

C library function - fgets() The C library function char *fgets(char *str, int n, FILE *stream) reads a line from the specified stream and stores it into the string pointed to by str. It stops when either (n-1) characters are read, the newline character is read, or the end-of-file is reached, whichever comes first.


2 Answers

char *temp is only a pointer. At begin it doesn't points to anything, possibly it has a random value.

fgets() reads 500 bytes from fp to the memory addresse, where this temp pointer points! So, it can overwrite things, it can make segmentation faults, and only with a very low chance will be work relativale normally.

But char temp[500] is a 500 bytes long array. That means, that the compiler does the allocation on the beginning of your process (or at the calling of your function). Thus this 500 bytes will be a useable 500 bytes, but it has a price: you can't reallocate, resize, free, etc. this.

What the google wants from you, is this:

char *temp = (char*)malloc(500);

And a

free(temp);

after you don't need this any more.

like image 198
peterh Avatar answered Oct 05 '22 08:10

peterh


When we write

char *temp ; 

it means temp is an uninitialized pointer to char i.e. currently it does not contain any address in it .

While using fgets you have to pass a string in which the bytes read from file pointer is to be copied . link since the temp is uninitialized , the fgets looks like this

fgets(<no string> , 500 , fp ) ;

which is invalid .

Hence , we should give initialized string which can be formed as :

1) char *temp = malloc(sizeof(500)) ;
or
2) char temp[500] ;

Hence if we pass initialized string to fgets , it would look like

fgets( < some string > , 500 , fp) ;
like image 21
Subbu Avatar answered Oct 05 '22 08:10

Subbu