[SOLVED] Reading text file and outputting what’s read in C language
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 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.
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.