Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

\n\r

Posted on 2004-11-28
3
Medium Priority
?
265 Views
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 :
Hello

- 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 ?

Thanks.
0
Comment
Question by:matthew016
3 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12691005
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");
0
 
LVL 15

Accepted Solution

by:
efn earned 404 total points
ID: 12691155
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.

--efn
0
 
LVL 9

Author Comment

by:matthew016
ID: 12691221
Thanks, very good
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Suggested Courses
Course of the Month21 days, 2 hours left to enroll

810 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