-1

I've seen this question has been asked before, but none of the answers seemed to work for my problem.

I am attempting to write a function that will read the contents of a file, and print them. Here is my code;

int main()
{
    int c;
    fseek(stdin, 0, SEEK_SET);
    c = getc(stdin);

    while ((c = getchar()) != EOF)
    {
        putchar(c);
        fseek(stdin, 1, SEEK_CUR);
        c = getc(stdin);
    }
}

When running the code, I pipe in a file using;

./[Program] < [File.txt]

eg.

./FileRead < Hello.txt

However, when I run it, I get a jumble of random letters. Here is an example:

The contents of the file I am piping in:

Hello World!

This is a test file.

I hope this works!

And here is the output:

eood

Tss sfe

Io iwk

Can anyone help me work out what is wrong?

Community
  • 1
  • 1
SexRobomb
  • 5
  • 1
  • 2
  • Are you using the fseek for what? just `while ((c = getchar()) != EOF) { putchar(c); }` – BLUEPIXY Oct 28 '14 at 23:42
  • This seems to have worked to fix my problem. However, when it prints the text, it seems to miss out the first character in the file. Do you know how to fix this? – SexRobomb Oct 28 '14 at 23:52
  • Or the `getc` : since the loop reads a character, skips a character, and ignores a character, you get what you'd expect: every third character. – rici Oct 28 '14 at 23:52
  • @SexRobomb Delete `fseek(stdin, 0, SEEK_SET); c = getc(stdin);` before while-loop. – BLUEPIXY Oct 28 '14 at 23:58
  • Didn't you notice `eood` is just `Hello World!` beginning with the 2nd char and then skipping every 2? – David C. Rankin Oct 29 '14 at 01:17

1 Answers1

1

Just to note that the getc() function moves the active file pointer to the next position automatically after reading a character, so the

//fseek(stdin, 0, SEEK_SET);  

opens the file and sets the pointer at the first char

//c = getc(stdin);           

The //getc() gets the next character in line the 'H' and moves the pointer forward one character c now == 'H'

 while ((c = getchar()) != EOF)

The

//getchar() 

seams to be working (not recommended) when reading from a file try using

//getc(<filepointer>)

c now == 'e' and the filepointer is moved to the first 'l'.

Then you have

//putchar(c) 

which prints the 'e' character

 //fseek(stdin, 1, SEEK_CUR);    

Moves the *fp ahead one character to the second 'l'

Then you have // c = getc(stdin);
Read the next character in line which is the second 'l', move the *fp to the ' ' space, and repeat.

Basically change the code to this:

 while ((ch = getch(stdin)) != EOF)
{
 putchar(c);

}

The code should work fine.

Note: For streams opened in text mode, fseek and _fseeki64have limited use, because carriage return–linefeed translations can cause fseek and _fseeki64to produce unexpected results. The only fseek and _fseeki64operations guaranteed to work on streams opened in text mode are:

•Seeking with an offset of 0 relative to any of the origin values.

•Seeking from the beginning of the file with an offset value returned from a call to ftell when using fseekor _ftelli64when using_fseeki64.

Mike_V
  • 11
  • 4
  • +1 Nice complete answer for a new guy. Minor: May want to review the editing options to improve formatting. – chux - Reinstate Monica Oct 29 '14 at 01:38
  • Thanks Chux, finally found I could answer. I do need some guidelines on answer format. I now see if you don't follow it word for word, it will not make any since. I guess that the nature of the business. I do agreed its harder then it should be to follow. I will try and improve my formatting. :) – Mike_V Oct 29 '14 at 05:21