Print line by character in C


 
Thread Tools Search this Thread
Top Forums Programming Print line by character in C
# 1  
Old 04-20-2013
Print line by character in C

Hello, I am reading the K & R C book, and trying to understand more about putchar()/getchar() by practice. Similar to the exercise 1-12, I want try to parse each line by character. Although there are many site about the exercise, but I could find similar case of mine, which is similar to fold command. Not sure it's my code problem, or from my display setting.
Code:
#include <stdio.h>
/*count the characters in input; */

main() {
  char c;

  while (c = (getchar() != EOF) )
  {
//    putchar(c);
    printf("%c\n", putchar(c));
  }

}

Input:
Code:
A test

And I am expecting output like:
Code:
A

t
e
s
t

But I got strange char on the screen, can't be pasted here. What did I miss? Thanks!
# 2  
Old 04-20-2013
Code:
printf("%c\n", putchar(c));
// should be
printf("%c\n", c);

# 3  
Old 04-20-2013
I think your while condition is also slightly wrong.

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

# 4  
Old 04-20-2013
No, still not working. There is no error at compiling, and running of the compiled program seems working too, except the char are not corrected printed out. E.g:
Code:
$ ./print_by_char
A test
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
$

Those [ ] on my screen are tiny squares with 0001 inside like.
Code:
|00|
|01|

I thought it may be related to display or language setting that I have no idea about, but testing of some other code from the book were fine. I am using Ubuntu 12.10 with bash. $LANG=en_CA.UTF-8.
Scott, it seem the syntax is correct.
Any other clue? Thanks!
# 5  
Old 04-20-2013
It's probably some typo you are making. Try again with code below. If it does not work, post what you are seeing.
Code:
$ cat test.c
#include <stdio.h>

main() {
  int c;

  while ((c = getchar()) != EOF) {
    printf("%c\n", c);
    }
  }

Code:
$ cat input
A test

Code:
$ gcc test.c
$ a.out < input
A

t
e
s
t
[ blank line ]
[ blank line ]

This User Gave Thanks to hanson44 For This Post:
# 6  
Old 04-20-2013
The O's and 1's are the false's and true's that getchar() != EOF is returning into c because your condition is wrong.
Code:
$ cat main.c
#include <stdio.h>
/*count the characters in input; */

int main() {
  char c;
  while ((c = getchar()) != EOF)
  {
    printf("%c\n", c);
  }

  return 0;
}
$ cc -o main main.c
$ ./main           
Hello World
H
e
l
l
o
 
W
o
r
l
d

This User Gave Thanks to Scott For This Post:
# 7  
Old 04-20-2013
Scott is right!
The only difference is the order of the condition. Mine is
Code:
while ((c = (getchar() != EOF))

and yours, both of you, are
Code:
while ((c = getchar()) != EOF)

Now I got it. But, with my condition, it should print out seven "1", but why it printed out 7 unprintable chars (I use x here!) like this?
Code:
A Test
x
x
x
x
x
x
x

That's the very subtle but most important part I have missed.
Thanks again, to both of you!

Last edited by yifangt; 04-20-2013 at 08:40 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk or sed to print the character from the previous line after the regexp match

Hi All, I need to print the characters in the previous line just before the regular expression match Please have a look at the input file as attached I need to match the regular expression ^ with the character of the previous like and also the pin numbers and the output file should be like... (6 Replies)
Discussion started by: kshitij
6 Replies

2. Shell Programming and Scripting

Read character by character in line in which space is also included

Hi friend, I have one file , and i want to read that file character by character. I need this script in ksh. while using read option with -n1 am getting error. while read -n1 c read has bad option And if i am using below script, then if in a line has space like this ( Pallvi mahajan)... (10 Replies)
Discussion started by: pallvi_mahajan
10 Replies

3. Shell Programming and Scripting

Print entire line only if certain fixed character matches the string

Hi All, I have a file testarun.txt contains the below lines and i want to print the lines if the character positions 7-8 matches 01. 201401011111 201401022222 201402013333 201402024444 201403015555 201403026666 201404017777 201404028888 201405019999 201405020000 I am trying the... (4 Replies)
Discussion started by: Arunprasad
4 Replies

4. Shell Programming and Scripting

If first character doesn't equal '#' and same line contains 'temp', print everything else

(2 Replies)
Discussion started by: snoman1
2 Replies

5. Programming

Read text from file and print each character in separate line

performing this code to read from file and print each character in separate line works well with ASCII encoded text void preprocess_file (FILE *fp) { int cc; for (;;) { cc = getc (fp); if (cc == EOF) break; printf ("%c\n", cc); } } int main(int... (1 Reply)
Discussion started by: khaled79
1 Replies

6. Shell Programming and Scripting

How to print a particular character n number of times in a line??

hi, Is it possible to print a particular character n number of times in a line? for example. i am print the following line using echo command.. echo "files successfully moved" i want to count the number of characters that are been displayed. i am doin it using echo "files... (8 Replies)
Discussion started by: Little
8 Replies

7. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies

8. Shell Programming and Scripting

AWK Print Line If Specific Character Is Matched

Hello, I have a file as such: FFFFFFF6C000000 225280 225240 - - rwxs- FFFFFFFF79C00000 3240 3240 - - rwxs- FFFFFFFF7A000000 4096 4096 - - rwxs- FFFFFFFF7A400000 64 64 ... (3 Replies)
Discussion started by: PointyWombat
3 Replies

9. HP-UX

How to remove new line character and append new line character in a file?

Hi Experts, I have data coming in 4 columns and there are new line characters \n in between the data. I need to remove the new line characters in the middle of the row and keep the \n character at the end of the line. File is comma (,) seperated. Eg: ID,Client ,SNo,Rank 37,Airtel \n... (8 Replies)
Discussion started by: sasikari
8 Replies

10. Shell Programming and Scripting

read the text file and print the content character by character..

hello all i request you to give the solution for the following problem.. I want read the text file.and print the contents character by character..like if the text file contains google means..i want to print g go goo goog googl google like this Using unix Shell scripting... without using... (1 Reply)
Discussion started by: samupnl
1 Replies
Login or Register to Ask a Question