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

  • Thread starter sozener1
  • Start date
  • Tags
    files
In summary, the code that poster is using only reads keyboard inputs, and when he tries to read from a file, he gets strange symbols printed out. Can anyone help him figure out what is wrong?
  • #1
sozener1
19
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
  • #2
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.
 
  • #3
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
 
  • #4
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!
 
  • #5
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.
 
  • #6
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.
 
  • #7
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:
  • #8
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.
 
  • #9
it would be something like cout >> 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.
 

What is the purpose of using getchar() and putchar() to read in files?

The getchar() and putchar() functions are commonly used in C programming to read and write characters from the standard input and output streams. They can also be used to read and write files by redirecting the standard input and output streams to a file. This allows for efficient reading and writing of characters from a file in a C program.

How do I use getchar() and putchar() to read in a file in C?

To use getchar() and putchar() to read in a file, you must first open the file using the fopen() function. Then, you can use a while loop to read each character from the file using getchar() until the end of the file is reached. Within the loop, you can perform any necessary operations on the characters and then use putchar() to print them to the output stream.

Can getchar() and putchar() be used to read and write binary files?

Yes, getchar() and putchar() can be used to read and write binary files in C. However, they are designed to work with characters, so they may not be the most efficient or practical option for working with binary data. It is recommended to use the fread() and fwrite() functions for reading and writing binary data in C.

Are there any potential limitations or drawbacks to using getchar() and putchar() to read in files?

One potential limitation of using getchar() and putchar() to read in files is that they can only read and write one character at a time. This may not be ideal for large files or when efficiency is a concern. Additionally, these functions may not be suitable for handling certain special characters or non-ASCII characters, which may require additional processing.

Can I use getchar() and putchar() to read and write files in other programming languages?

Getchar() and putchar() are specific to the C programming language and may not be available in other languages. However, other programming languages may have similar functions or methods for reading and writing characters from files. It is important to consult the documentation for the specific language you are using to determine the best way to read and write files.

Similar threads

  • Programming and Computer Science
Replies
6
Views
889
  • Programming and Computer Science
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
934
  • Programming and Computer Science
Replies
15
Views
5K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
8
Views
5K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
Back
Top