finding "\n" in files

A

Andreas Lassmann

is there any way to find an endline in a file?
i.e:

xxxxxxxxxxxxxxxxx
x x x x x x x x x
xxxxxxxxxxxxxxxxx

is there some method in "ifstream"?
or can i check it with something like "if(fileInput == '\n')?

thanks in advance
 
D

Dietmar Kuehl

Andreas said:
is there any way to find an endline in a file?

/**/ std::ifstream in("some file");
/**/ typedef std::istreambuf_iterator<char> iterator;
/**/ iterator it = std::find(iterator(beg), iterator(), '\n');
/**/ if (it != iterator())
/**/ std::cout << "found a newline\n";
 
V

Victor Bazarov

Dietmar said:
/**/ std::ifstream in("some file");
/**/ typedef std::istreambuf_iterator<char> iterator;
/**/ iterator it = std::find(iterator(beg), iterator(), '\n');
.. ^^^^^
What's "beg"? Did you mean

iterator it = std::find(iterator(in), iterator(), '\n');

?
/**/ if (it != iterator())
/**/ std::cout << "found a newline\n";

Thanks.

V
 
D

Dietmar Kuehl

Victor said:
. ^^^^^
What's "beg"? Did you mean

iterator it = std::find(iterator(in), iterator(), '\n');

?

Yes... I had a somewhat longer version which used a variable
named 'beg' first. Sorry, for the broken code.
 
M

Mike Wahler

Andreas Lassmann said:
is there any way to find an endline in a file?
i.e:

xxxxxxxxxxxxxxxxx
x x x x x x x x x
xxxxxxxxxxxxxxxxx

is there some method in "ifstream"?
or can i check it with something like "if(fileInput == '\n')?

thanks in advance

#include <ios>
#include <iostream>
#include <fstream>
#include <string>

int main()
{
std::ifstream input("filename");

if(input)
{
std::streamsize count(0);
std::string line;

while(std::getline(input, line) && !input.eof())
{
std::cout << "Last character read was a newline character\n";
++count;
}

if(!input.eof())
std::cerr << "Error occurred reading input\n";

std::cout << count << " newline characters found in input\n";
}
else
std::cerr << "Could not open input\n";

return 0;
}
 
T

Thomas Matthews

Andreas said:
is there any way to find an endline in a file?
i.e:

xxxxxxxxxxxxxxxxx
x x x x x x x x x
xxxxxxxxxxxxxxxxx

is there some method in "ifstream"?
or can i check it with something like "if(fileInput == '\n')?

thanks in advance

Brute force:
ifstream my_file("my_file.txt")
char c;
while ((my_file >> c) && (c != '\n'))
{
; /* Illustrative null block */
}
/* At this point, the variable 'c' will
* contain a '\n'.
*/

There are better methods to read in text files.
I thought I'd show a brute force method.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,199
Messages
2,571,045
Members
47,643
Latest member
ashutoshjha_1101

Latest Threads

Top