I have the following code:

#include <iostream>
#include <conio.h>

using namespace std;

long filesize(FILE *stream);

int main(void)
    FILE *stream;
    char c;
    int i=0,n;
    long lSize;

    stream = fopen("tanc.TXT", "r");

    fseek (stream , 0 , SEEK_END);
    lSize = ftell (stream);
    rewind (stream);

    cout<<"Dati numarul de caractere:";

    while(!feof(stream) && i<lSize)
        for (int j=i; j<i+n; j++)


It works well for the following example:

File content(single line):
bread horse meat salt

Output for k=2:
br#ea#d #ho#rs#e #me#at# s#al#t #

but if i have
File content(multiple lines):

Output for k=2:

Why are "s" and "m" letters printed 2 times? I guess "\n" is responsible, but i don't get how and because of that, don't know how to fix it. Any ideas? Thanks.

Recommended Answers

All 4 Replies

Your code is a mixture of C and C++ conventions, you should choose one or the other. As you posted this in the C section I assume you want C code. What do you want to do with newlines? Do you want to skip them entirely? You also don't have to seek all the time. Something like this should work:

#include <stdio.h>
#include <errno.h>

int main(void)
    FILE *stream       = NULL;
    char         c     = 0;
    unsigned int i     = 0,
                 n     = 0;

    stream = fopen("tanc.TXT", "r");

    if (stream != NULL)
        printf("Dati numarul de caractere: ");

        if (scanf(" %u", &n) == 1)
            while((c = fgetc(stream)) != EOF)
                if (i == n) {
                    i = 0;

                if (c != '\n')
                    printf("%c", c);
        else {
            fprintf(stderr, "Invalid input.\n");

        if (fclose(stream) == EOF) {
            fprintf(stderr, "Error on closing the input file: %s\n", strerror(errno));
    } else {
        fprintf(stderr, "Error on opening the input file: %s\n", strerror(errno));

    return 0;

And as to why your program does what it does: I think it is because you are opening the file in text mode instead of binary mode:

For streams open in text mode, offset shall either be zero or a value returned by a previous call to ftell, and origin shall necessarily be SEEK_SET.

So replace fopen("tanc.TXT", "r"); with fopen("tanc.TXT", "rb"); and it should work. (Although output might be wrong because you're mixing cout/cin with printf) (But it doesn't skip over newlines yet, but the code above shows how to do that, if that is what you want) You could remove the seek part altogether though as you're seeking to where you ended the previous iteration anyway. You could also rewrite your loop because going from i to i+n-1 takes the same amount of iterations as going from 0 to n-1. Which is (in my opinion) more readable.

I want my output to contain the "\n" character.. Like this:


Try otputing to another file instead of windows console and read, as i see you use dos include.
Please note that windows new line is not '\n' because it's one byte in linux LF and 2 byte in windows CR+LF please see http://en.wikipedia.org/wiki/Newline

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.