Problem reading the contents of the text file and displaying it as a string?

There is documentation that you can use and look up easily on your favorite search engine which will tell you the answer. For example as described on this page for fgets().

Up vote 1 down vote favorite share g+ share fb share tw.

I have a text file named MySQL. Txt having the following contents : MySQL_IP:172.19.1.173 MySQL_USER:kok MySQL_PASS:kok MySQL_DATABASE:kok I am trying to read the contents of the file using the following code #include main() { FILE *fp; char str50000; fp = fopen("MySQL. Txt","r"); fgets(str,sizeof(str),fp); printf("%s\n",str); } No matter how much I increase the size of the string buffer,i am provided with teh output which is : MySQL_IP:172.19.1.173 which is just the first line...I fail to fathom the reason!

C link|improve this question edited Aug 1 '11 at 4:31Mahesh13.5k1823 asked Aug 1 '11 at 4:29phoenix736 33% accept rate.

There is documentation that you can use and look up easily on your favorite search engine which will tell you the answer. For example, as described on this page for fgets(): The fgets() function shall read bytes from stream into the array pointed to by s, until n-1 bytes are read, or a is read and transferred to s, or an end-of-file condition is encountered. The string is then terminated with a null byte.

If you need to read in the entire file at once, use fread() instead of fgets(). Unlike fgets(), fread() will not stop reading on occurrence of a newline. If you want to process the file line-by-line, then call fgets() repeatedly until feof() returns a non-zero value (indicating EOF) or ferror() returns a non-zero value (indicating an error condition).

It's better to check the value returned by fgets(); it returns the value EOF if there's nothing more to be read. This applies to all the standard input functions: check the documentation for the function you're using to see what it returns to indicate the end of the input. After fgets() tells you it's finished, then you can use feof() or ferror() to tell you why.

– Keith Thompson Aug 1 '11 at 5:05.

Fgets reads a line, i.e. : until the next \n (newline) in the text or end of file. To read the whole file, you need to repeat the sequence until feof returns true.

More precisely fgets() reads a line (and stores it in a string). (Or it reads a partial line if there isn't enough room to read it all. ) – Keith Thompson Aug 1 '11 at 5:06 @Keith - thanks, typo – littleadv Aug 1 '11 at 5:46.

Fgets documention says : syntex : char * fgets ( char * str, int num, FILE * stream ); Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or a the End-of-File is reached, whichever comes first. A newline character makes fgets stop reading, but it is considered a valid character and therefore it is included in the string copied to str. A null character is automatically appended in str after the characters read to signal the end of the C string.

So here fgets only read upto 1st line so I suggest you to use fread().

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions