LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 05-13-2010, 11:50 PM   #1
josip76
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


Hi

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'");
exit(1);
}


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

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

char *cfile;

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

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


int i=0;
while (*cfile) { //Here is the problem
i++;
printf("\n");
printf("%d",i);
}

free(cfile);
cfile = NULL;

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

Rep: Reputation: 16
Code:
int i=0;
while (*cfile)	{	//Here is the problem
i++;
printf("\n");
printf("%d",i);
}
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
josip76
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
 
  


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:39 PM.

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