Reading string by char till end of line C/C++ [duplicate]
Asked Answered
D

4

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))

Depredate answered 18/5, 2014 at 20:50 Comment(1)
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.Goldagoldarina
L
24

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.

Lionellionello answered 18/5, 2014 at 20:52 Comment(6)
Thanks it doesn't give me an error in that place but whats the difference between " and ' ?Depredate
@user3102621: I've added more to my answer to help explain. Let me know if it still isn't clear.Lionellionello
Thanks man, main problems that I find with programming is not knowing something simple but yet really annoying :D Thanks again!Depredate
@user3102621: You're not alone. It takes time. Keep at it!Lionellionello
Will '\n' char as an end of line will make my program portable - work on windows machines too ?Staunch
@accountant generally yes.Lionellionello
G
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.

Goldagoldarina answered 18/5, 2014 at 21:5 Comment(2)
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!Depredate
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.Beaujolais
C
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.

Cushman answered 18/5, 2014 at 21:3 Comment(0)
S
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')

Surcingle answered 18/5, 2014 at 20:56 Comment(1)
It can be even more complicated: en.wikipedia.org/wiki/NewlineBreast

© 2022 - 2024 — McMap. All rights reserved.