Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-13-2010, 11:50 PM   #1
LQ Newbie
Registered: May 2010
Posts: 12

Rep: Reputation: 0
Reading in a file in C, after file read null terminator at strings end gone


In reading a file into my program using C, i used calloc to allocate and initialize the needed memory. The size of the memory allocated was the size of the file-length + 1, so that the end of the string would house the NULL-terminator. However, after i read in the file using fread and I use while(*cfile) to read the string until reaching the null char the loop becomes an infinite loop (cfile is the name of the pointer to the memory block). Its as if after using fread to read in the file contents the null terminator is gone. I attached my code below. Thanks for any help.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char** argv)

FILE *fp; //file pointer
long int lfilelng; //file length

//open file
if ((fp=fopen("toberead", "rb"))==NULL) {
printf("cannot open file - 'toberead'");

if (fseek(fp, 0L, SEEK_END) !=0 ) {
printf("seek error");

lfilelng = ftell(fp);
printf("%ld\n", lfilelng);

char *cfile;

if ((cfile=calloc(lfilelng+1, sizeof(char)))==NULL) {
printf("insuficient memory to read file");

if (fread(cfile, lfilelng, 1, fp) != 1) {
printf("\nread error occured");

int i=0;
while (*cfile) { //Here is the problem

cfile = NULL;

return 0;
Old 05-14-2010, 12:20 AM   #2
Registered: Aug 2009
Distribution: Suse, RedHat, CentOS, Solaris, Windows
Posts: 38

Rep: Reputation: 16
int i=0;
while (*cfile)	{	//Here is the problem
Until your *cfile is not null, it will continue.You are not doing any processing on "cfile" in the loop, definitely your *cfile is valid, and will be valid every time, since you are not doing any ++ or -- or cfile = null.
Old 05-14-2010, 12:44 AM   #3
LQ Newbie
Registered: May 2010
Posts: 12

Original Poster
Rep: Reputation: 0
wow that was pretty simple, im so ashamed right now lol. thanks for your help


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to store text(strings) in a 2D character array reading from a text file(C++) bewidankit Programming 3 02-14-2008 07:08 AM
How can read from file.txt C++ where can save this file(file.txt) to start reading sam_22 Programming 1 01-11-2007 05:11 PM
Pop3 Command stream end of file while reading line sesimonsen Linux - Networking 0 05-13-2005 12:29 PM
read the input file from the specified line no till end suchi_s Programming 5 09-09-2004 04:36 AM
Script, Reading a file, When end of file? elibm Programming 2 07-16-2001 11:01 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:07 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration