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

  • Thread starter Thread starter sozener1
  • Start date Start date
  • Tags Tags
    files
Click For Summary

Discussion Overview

The discussion revolves around the use of the C programming functions getchar() and putchar() for reading sentences from a text file. Participants explore issues related to file input, character encoding, and the behavior of the code provided by the original poster.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • The original poster describes a problem where the character "P" from the word "Project" is printed as a weird symbol when reading from a file.
  • Some participants assert that the code is not reading from a file but rather from keyboard input, questioning the use of stdin.
  • Others clarify that the original poster is using file redirection to read from a file into stdin.
  • One participant suggests using the 'od' command to inspect the first few bytes of the input file for potential encoding issues.
  • Another participant raises the possibility that the file may be a UTF-8 file with a Byte Order Mark (BOM), which could explain the unexpected character output.
  • There are suggestions for looking up tutorials on file I/O in C to better understand the process.

Areas of Agreement / Disagreement

Participants express disagreement regarding whether the code is correctly reading from a file or from keyboard input. Some assert it reads from stdin, while others support the original poster's claim of using file redirection. The discussion remains unresolved regarding the specific cause of the character issue.

Contextual Notes

There are mentions of potential encoding issues, such as the presence of a BOM in the input file, which may affect how characters are read and displayed. The discussion does not reach a consensus on the exact nature of the problem.

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
6K
  • · 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