Reading line of chars in C

Back to Programming Languages forum

james_cranston     2014-08-28 03:26:58

How can I use a while loop to create an array of the charcters for each line?

nicolas_patrois     2014-08-28 06:18:02
User avatar

Why should you use a while loop?

Rodion (admin)     2014-08-28 06:54:28
User avatar

On the other hand you really need not array ;-)

Here are two choices:

  • either you use fgetc to read input character-by-character (really with while) and updating counters at the same time - you then need reset counters and print current result each time you encounter '\n' which is the sign of line end (this approach is widely covered in Kernighan & Ritchie book);
  • or you should prepare empty array and read the whole line into it with gets or fgets:

    char line[1000];
    gets(line); // now it contains next line, zero-terminated
    

P.S. I'm afraid that I may forgot many C-related tricks so if my suggestions are not good enough you may want to ask questions
at C-gurus forum.

nicolas_patrois     2014-08-28 12:49:09
User avatar

This thread should move in the languages subforum (and be renamed)?

Rodion (admin)     2014-08-29 05:06:46
User avatar

> This thread should move in the languages subforum (and be renamed)?

Done! At last I've implemented functionality for moving/renaming posts for myself :)

Please login and solve 5 problems to be able to post at forum