LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-10-2010, 05:44 PM   #1
josip76
LQ Newbie
 
Registered: May 2010
Posts: 12

Rep: Reputation: 0
Reading text file and outputting what’s read in C language


Hi everyone I’m new to programming and this Forum. I searched the previous posts but couldn’t find an answer to my specific problem. Thanks for any help.

What I’m trying to do is read in a text file with an integer in it. I’ve read the other posts about this and could not see anything different in their programs from mine.

The file is being read as binary I used fseek and ftell to get the length of the file and I dynamically allocated a piece of memory to hold what is read using malloc. Then the file is read into memory using fread. The pointer which points to the memory block is called “cfile”.

My problem is that when I display the number read from the file I get a random number output, not the number in the file. For example when the text file has the number 7 in it, the program reads the file and for printf(“%d”, cfile[0]) outputs 2615. When I put the number 7 and a space after it in the text file, the program outputs 663607 for printf(“%d”, cfile[0]).

Here is a copy of my program:

and thanks for any help you can offer


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

int main(void)
{

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);
rewind(fp);

int *cfile; /* Dynamically allocated buffer (entire file) */

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

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

printf("%d", cfile[0]);

free(cfile);
cfile = NULL;

return 0;
}
 
Old 05-10-2010, 05:57 PM   #2
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
If you run this in a symbolic debugger or otherwise display/examine the contents of cfile after the input file is read, the problem will make itself apparent.
 
Old 05-10-2010, 06:05 PM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by josip76 View Post
...
I’m trying to do is read in a text file with an integer in it.
...
Code:
int *cfile;	 /* Dynamically allocated buffer (entire file) */
...
Why "int *" ?
 
Old 05-10-2010, 06:28 PM   #4
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by Sergei Steshenko View Post
Why "int *" ?
Based on how the OP is printing it, i would assume the file is binary data and full of ints.
 
Old 05-10-2010, 06:30 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by smeezekitty View Post
Based on how the OP is printing it, i would assume the file is binary data and full of ints.
Did you see the underlined word in my post ?
 
Old 05-10-2010, 06:32 PM   #6
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Well in that case, use fscanf instead.
You are reading the integer as if it was in binary form yet you say it is in text form.

Last edited by smeezekitty; 05-10-2010 at 06:33 PM. Reason: typgraphical error
 
Old 05-10-2010, 06:36 PM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by smeezekitty View Post
Well in that case, use fscanf instead.
You are reading the integer as if it was in binary form yet you say it is in text form.
Though your suggestion quite possibly is technically correct, it masks (by the fact of its existence) the root cause of the problem IMO the OP has.

I.e. you are suggesting a fix, and I want the OP to understand the fundamental problem in his/her approach.
 
Old 05-10-2010, 07:10 PM   #8
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
That answer is very Sergei Steshenkoish.
Ok i am out of this thread.
 
Old 05-10-2010, 07:30 PM   #9
josip76
LQ Newbie
 
Registered: May 2010
Posts: 12

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Sergei Steshenko View Post
Though your suggestion quite possibly is technically correct, it masks (by the fact of its existence) the root cause of the problem IMO the OP has.

I.e. you are suggesting a fix, and I want the OP to understand the fundamental problem in his/her approach.

Hi, thanks for getting back so quickly to me. First, yes the file being read is a text file with an integer in it but I’m opening it as a binary file via “rb” in the line: (fp=fopen("toberead", "rb"). I did this to avoid any character translations performed on the text file so that fseek( ) would return the correct size of the file.

I used the data type of integer for the pointer “cfile” since the file contained an integer value. I thought that even though the file was being read as a binary file that the int data type was valid since the binary value read corresponded to an integer. Please correct me if I am wrong here.

I don’t understand why it was recommended to use fscanf instead of fread?

Thanks again for your help
 
Old 05-10-2010, 07:34 PM   #10
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by josip76 View Post
Hi, thanks for getting back so quickly to me. First, yes the file being read is a text file with an integer in it but I’m opening it as a binary file via “rb” in the line: (fp=fopen("toberead", "rb"). I did this to avoid any character translations performed on the text file so that fseek( ) would return the correct size of the file.

I used the data type of integer for the pointer “cfile” since the file contained an integer value. I thought that even though the file was being read as a binary file that the int data type was valid since the binary value read corresponded to an integer. Please correct me if I am wrong here.

I don’t understand why it was recommended to use fscanf instead of fread?

Thanks again for your help
Back to the roots. A text file is a sequence of characters, and the latter are represented in "C" by char (and not int) type. This is your fundamental problem - you have type mismatch.

Last edited by Sergei Steshenko; 05-10-2010 at 07:51 PM.
 
1 members found this post helpful.
Old 05-10-2010, 07:40 PM   #11
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Use a char* instead of an int* to store the file, as the file is a string of chars, not ints.

fread treats the pointer as an array of chars, but because you read them as ints you get an int composed of 4 consecutive bytes (char's) in the file.
 
1 members found this post helpful.
Old 05-10-2010, 07:54 PM   #12
josip76
LQ Newbie
 
Registered: May 2010
Posts: 12

Original Poster
Rep: Reputation: 0
ah ok I understand now. Thanks to everyone for your help.
 
Old 05-10-2010, 07:55 PM   #13
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
So does it work now?

If so, mark the thread as Solved (in Thread Tools).
 
Old 05-10-2010, 07:57 PM   #14
josip76
LQ Newbie
 
Registered: May 2010
Posts: 12

Original Poster
Rep: Reputation: 0
yup it works out, sorry bout the delay in marking the thread as solved wasnt sure how to do it.
thanks agian
 
  


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
Cannot read text from text file and store it in a variable using shell script anurupr Linux - Newbie 2 03-03-2010 01:38 PM
read string after specific string from a text file using C++ programing language badwl24 Programming 5 10-08-2009 05:41 AM
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
outputting data from a directory to a text file? minm Linux - Newbie 2 12-19-2004 06:46 PM
reading a text file and outputting to another. Hardw1re Programming 28 11-03-2003 08:51 AM

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

All times are GMT -5. The time now is 10:38 AM.

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