Posted on 2004-11-28
Last Modified: 2010-04-15
Hello, I'm learning how to use files in C language ,
and there's something I don't understand
- In my file :

- In my pgm :
printf("%d\n", ftell(filein));   // 0
printf("%c\n", fgetc(filein)); // H
printf("%d\n", ftell(filein));   // 1

then with a fseek i go at 5th position, just before the \n\r

printf("%d\n", ftell(filein));   // 5
printf("%c\n", fgetc(filein)); // <new line>
printf("%d\n", ftell(filein));   // 7

But I tought fgetc would only take the '\n' cause fgetc takes one char ...
Why it takes both in one time ?

Question by:matthew016
    LVL 55

    Expert Comment

    by:Jaime Olivares
    I guess it is due you have opened your file as 'text', something like:

    filein = fopen("yourfile.txt", "rt");

    Try to open your file as binary:

    filein = fopen("yourfile.txt", "rb");
    LVL 15

    Accepted Solution

    Different platforms use different characters for ending lines of text, which can be a problem for writing portable software.  The C library's solution is to abstract the format, so that from a C program's point of view, all text files have lines terminated with '\n'.  The library takes care of translating between a newline character in its application programming interface and whatever the target platform actually uses, so one can write portable text-processing programs.  This translation is probably what happened in your case.

    Opening a file in text mode turns this abstraction on, and opening a file in binary mode turns it off.  That's why jaime_olivares suggested opening it in binary mode, assuming your goal is to be able to read the characters that are actually in the file.

    LVL 9

    Author Comment

    Thanks, very good

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now