File reading in C language

I am trying to read a file in C language

while(1){ if(feof(file)){ break; } printf("%c",fgetc(file)); }

At the end I get special character like � I dont have anything like that in file

-------------Problems Reply------------

You can read a file step by step using the following code:

int ch;
while ((ch = fgetc(file)) != EOF) {
putchar(ch);
}

This special character might be the EOF.

This question/answers might be interesting for you as well.

From the man page for fgetc:

RETURN VALUE
fgetc(), getc() and getchar() return the character read as an
unsigned char cast to an int or EOF on end of file or error.

So you should check for feof before you use the return value of fgetc or you will print out the EOF character:

while(1){
int c = fgetc(file);
if(feof(file)){
break;
}
printf("%c",c);
}

Category:c# Views:3 Time:2017-09-11
Tags: file

Related post

  • Classes and file reading 2009-08-14

    Can an object of ifstream type used for file reading be a static member of a class? I want to read a file and store each line in an array of objects of a class i have created. I want the file reading object to belong to the entire array of objects in

  • SIMPLE file reading in Perl 2010-05-11

    The other answered questions were a bit complicated for me, as I'm extremely new to using Perl. I'm curious how Perl reads in the files, how to tell it to advance to the next line in the text file, and how to make it read all lines in the .txt file u

  • Fastest file reading in C 2010-06-08

    Right now I am using fread() to read a file, but in other language fread() is inefficient i'v been told. Is this the same in C? If so, how would faster file reading be done? --------------Solutions------------- If you are willing to go beyond the C s

  • how can I import a file in java programming language 2010-07-05

    how can I import a file in java programming language --------------Solutions------------- You don't import files in Java. You import classes. The syntax is as follows: import com.my.Class; There is nothing like the #include available in C/C++. If you

  • Haskell file reading 2011-10-23

    I have just recently started learning Haskell and I am having a lot of trouble trying to figure out how the file reading works. For example, I have a text file "test.txt" And it contains lines of numbers eg: 32 4 2 30 300 5 I want to read each line a

  • Deleted some native language fonts from control panel font folders, now unable to read my own language 2014-09-13

    hi.. actually i deleted some fonts mistekly from control pannel fonts folders. now the problem is i cant read my nepali language and hindi language in website.The web site display -- _ _ _ _ _ _ _ only i can see every thing and read english language

  • Open file, read it, process, and write back - shortest method in Python 2008-10-22

    I want to do some basic filtering on a file. Read it, do processing, write it back. I'm not looking for "golfing", but want the simplest and most elegant method to achieve this. I came up with: from __future__ import with_statement filename = "..." #

  • How do you test a file.read() error in Python? 2008-12-17

    I have the following code (adapted from an example given in Dive Into Python) that reads the entire contents of a file into a buffer. buffer = "" try: file = open(postFileName, 'rU') try: # Read the entire POST log file into a buffer buffer += file.r

  • Linux & C: How to set file reading priority in multi-process program? 2009-02-06

    This is for an assignment I'm working on, and NO I'm not looking for you to just GIVE me the answer. I just need someone to point me in the right direction, maybe with a line or two of sample code. I need to figure out how to set the priority of a fi

  • How to Perform Asynchronous File Reads in C# 2.0? 2009-04-29

    I have an application which needs to loop through all the lines in text files, over gigabytes in size. Some of these files have 10's or 100's of millions of lines. An example of my current (and synchronous) reading, looks something like... using (Fil

  • Python File Read + Write 2009-05-30

    I am working on porting over a database from a custom MSSQL CMS to MYSQL - Wordpress. I am using Python to read a txt file with \t delineated columns and one row per line. I am trying to write a Python script that will read this file (fread) and [eve

  • How to improve performance of file reading by multiple threads? 2009-09-24

    I need to read a single file using multiple threads under Linux. There are reading operations only and no need of writing. The file reading don't need read the whole file every time. It need read one or more portions of a file every time. I store the

  • How do you make a shared network file read-only using Python? 2009-10-13

    Using Python, what's the correct way to set a file to be read-only when the file is located on a network share (being served from a Windows 2003 Server)? I'm running Python 2.6.2 in OS X (10.6.1). The following code throws an exception (as expected)

  • Where can I find a Vim syntax file for the go language? 2009-11-18

    Has anyone created a vim syntax file for the go language? --------------Solutions------------- go.vim There is also a syntax file on vim.org. A vim syntax file is included in the Go distribution under misc/vim/ For syntax files for other editors see

  • Memory leak in VB file reader 2009-11-19

    I'm currently coding a file reader for fixed-width tables in VB, and the compiled application seems to be sucking down memory like there's no tomorrow. I'm working with a series of ~50 megabyte files, but after running through several, the process st

  • Efficient file reading techniques in C# 2009-11-20

    I am looking for the best way to read a CSV file line by line. I want to know what are the most efficient ways of doing the same. I am particulary concerned when the size of the file is big. Are the file reading utilities avilable in the .NET classes

  • Which API in Java to use for file reading to have best performance? 2009-11-28

    In my place where I work, used to have files with more than million rows per file. Even though the server memory are more than 10GB with 8GB for JVM, sometimes the server get hanged for few moments and chokes the other tasks. I profiled the code and

  • File read by interrupt in java 2009-12-01

    I am using a text file to store the serial port output. And now I want to put the contents of the file to an textArea in java. I have created a dedicated thread for file read operation. I need the thread to sleep when there is no data to read and thr

  • File reading and writing inside asp.net web application 2009-12-17

    I have developed an ASP.NET web application in Visual Studio 2008. I have an HTML document and one text file in the application, but both are not inside my application. Both are outside, so when I try to run the same application in another system, I

  • How to parallelize file reading and writing 2010-01-03

    I have a program which reads data from 2 text files and then save the result to another file. Since there are many data to be read and written which cause a performance hit, I want to parallize the reading and writing operations. My initial thought i

  • C based conf file reader 2010-01-22

    My code uses a text based conf file with key=value type entries. I want to add sections feature for file, i.e. search for number of sections, search for particular key in a section. I was wondering if there are any libraries/functions which already d

  • When you make a file read-only via Properties, can your program write directly to that file? 2010-01-27

    I'm wondering whether making files read-only so the user can't mess with them will disallow my program from writing information to them via an fstream. --------------Solutions------------- Yes. If a file is read-only, it's read-only. Why not unset th

  • Wix - set file read access 2010-02-11

    I am looking into a way of setting read access on a specific file for a web application (where all files read option is set to be false--unchecked in IIS) deployed with Wix. Is it a possible option at all or I am asking the question in a wrong way? T

  • Need file read in from form load 2010-04-19

    I am using a listview, and trying to populate using a file. I need the file to be read in as soon as the form starts. private void mainForm_Load(object sender, EventArgs e) { //get file read in if (File.Exists("../../MealDeliveries.txt")) { StreamRea

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.121 (s). 11 q(s)