9

How to read a string one char at the time, and stop when you reach end of line? I'am using fgetc function to read from file and put chars to array (latter will change array to malloc), but can't figure out how to stop when the end of line is reached

Tried this (c is the variable with char from file):

if(c=="\0")

But it gives error that I cant compare pointer to integer

File looks like (the length of the words are unknown):

one
two
three

So here comes the questions: 1) Can I compare c with \0 as \0 is two symbols (\ and 0) or is it counted as one (same question with \n) 2) Maybe I should use \n ? 3) If suggestions above are wrong what would you suggest (note I must read string one char at the time)

(Note I am pretty new to C++(and programming it self))

user3102621
  • 425
  • 2
  • 4
  • 9
  • Your question says C/C++, but to reading a line of text from a file is massively easier in C++ than it is in C. Would you accept an answer that is C++ only? Because it's trivial in C++ and many lines in C, given that you have to dynamically allocate memory, etc. – Ray Toal May 18 '14 at 20:58

4 Answers4

23

You want to use single quotes:

if(c=='\0')

Double quotes (") are for strings, which are sequences of characters. Single quotes (') are for individual characters.

However, the end-of-line is represented by the newline character, which is '\n'.

Note that in both cases, the backslash is not part of the character, but just a way you represent special characters. Using backslashes you can represent various unprintable characters and also characters which would otherwise confuse the compiler.

Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132
4

The answer to your original question

How to read a string one char at the time, and stop when you reach end of line?

is, in C++, very simply, namely: use getline. The link shows a simple example:

#include <iostream>
#include <string>
int main () {
  std::string name;
  std::cout << "Please, enter your full name: ";
  std::getline (std::cin,name);
  std::cout << "Hello, " << name << "!\n";
  return 0;
}

Do you really want to do this in C? I wouldn't! The thing is, in C, you have to allocate the memory in which to place the characters you read in? How many characters? You don't know ahead of time. If you allocate too few characters, you will have to allocate a new buffer every time to realize you reading more characters than you made room for. If you over-allocate, you are wasting space.

C is a language for low-level programming. If you are new to programming and writing simple applications for reading files line-by-line, just use C++. It does all that memory allocation for you.

Your later questions regarding "\0" and end-of-lines in general were answered by others and do apply to C as well as C++. But if you are using C, please remember that it's not just the end-of-line that matters, but memory allocation as well. And you will have to be careful not to overrun your buffer.

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
  • I will be using c++ (wrote malloc cause I don't know better thing in c++ than him(but I heard that it is advised to use something else (will look for it latter in the progress :) ))) Thanks for the help! – user3102621 May 18 '14 at 21:18
  • You can, in C, use `int getc(FILE*)` and the function stack to get an exact number of bytes to allocate in one allocation. The limitation is the maximum stack size, so it won't work for huge files, but it's certainly doable. – Marcus Harrison Sep 05 '17 at 09:54
3

If you are using C function fgetc then you should check a next character whether it is equal to the new line character or to EOF. For example

unsigned int count = 0;
while ( 1 )
{
   int c = fgetc( FileStream );

   if ( c == EOF || c == '\n' )
   {
      printF( "The length of the line is %u\n", count );
      count = 0;
      if ( c == EOF ) break;
   }
   else
   {
      ++count;
   }
}    

or maybe it would be better to rewrite the code using do-while loop. For example

unsigned int count = 0;
do
{
   int c = fgetc( FileStream );

   if ( c == EOF || c == '\n' )
   {
      printF( "The length of the line is %u\n", count );
      count = 0;
   }
   else
   {
      ++count;
   }
} while ( c != EOF );

Of course you need to insert your own processing of read xgaracters. It is only an example how you could use function fgetc to read lines of a file.

But if the program is written in C++ then it would be much better if you would use std::ifstream and std::string classes and function std::getline to read a whole line.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

A text file does not have \0 at the end of lines. It has \n. \n is a character, not a string, so it must be enclosed in single quotes

if (c == '\n')

ScottMcP-MVP
  • 10,337
  • 2
  • 15
  • 15