[C] using getchar() and putchar() to read in files

  • Thread starter Thread starter sozener1
  • Start date Start date
  • Tags Tags
    files
sozener1
Messages
19
Reaction score
0
Im working on some programme

and I've used getchar() putchar() to read sentences in a text file

I open the file usin stdin < filename.txt format

and here is my code

#include <stdio.h>
#include <string.h>


int main(int argc, char *argv[]) {

int i;
printf(" ");
while((i=getchar())!='l'){
putchar(i);
}



return 0;

}

and here is the first few sentences in the file

"Project Gutenberg's Alice's Adventures in Wonderland, by Lewis Carroll

This eBook is for the use of anyone anywhere at no cost and with"

the code works fine for most part and prints out every single characters correctly including the space EXCEPT for "P" from Project in the first sentence

it gets printed out in a very weird symbols

can anyone help me figure out what is wrong
 
Technology news on Phys.org
You're not reading from a file. You're reading keyboard inputs and showing them on the screen. You even didn't open a file!
Take a look at here.
 
Shyan said:
You're not reading from a file. You're reading keyboard inputs and showing them on the screen. You even didn't open a file!
Take a look at here.


no I am not meant to open files in here
 
sozener1 said:
no I am not meant to open files in here

But you said you want to read sentences from a file!
Anyway, the code you posted, only reads keyboard inputs!
 
Shyan said:
You're not reading from a file. You're reading keyboard inputs and showing them on the screen.

He is reading from stdin, and if you look at how he calls his program, he reads from the file.
 
sozener1 said:
the code works fine for most part and prints out every single characters correctly including the space EXCEPT for "P" from Project in the first sentence

it gets printed out in a very weird symbols

can anyone help me figure out what is wrong

Use 'od' or some similar program to look at the first few bytes in your input file.
 
sozener1 said:
the code works fine for most part and prints out every single characters correctly including the space EXCEPT for "P" from Project in the first sentence

it gets printed out in a very weird symbols

Are you sure it is a simple ASCII file and not UTF-8 file with BOM (http://en.wikipedia.org/wiki/Byte_order_mark )?

Edit: that's actually the same hint Nugatory gave, he was just faster. I hate being interrupted when posting
grumpy_borek.png
 
Last edited by a moderator:
Shyan said:
But you said you want to read sentences from a file!
Anyway, the code you posted, only reads keyboard inputs!
Poster indicated in his opening post that he is using the operating system to perform redirection of file to stdin.
 
it would be something like count >> txt.txt , (but that's prob not right lol) look for some tuts for like this http://www.cs.bu.edu/teaching/c/file-io/intro/
 
  • #10
NascentOxygen said:
Poster indicated in his opening post that he is using the operating system to perform redirection of file to stdin.

then use a pipe
 
  • #11
forget about what I said it,s late and I'm tired.
 

Similar threads

Replies
6
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 15 ·
Replies
15
Views
7K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 8 ·
Replies
8
Views
6K
Replies
1
Views
2K
  • · Replies 11 ·
Replies
11
Views
35K