how do you read in a text file char by char a store in a string?
coolmel55
0
Light Poster
Recommended Answers
Jump to Post>How do I find for example ; in the file now?
…#include <iostream> #include <string> int main() { std::string line; while ( getline(std::cin, line) ) { std::string::size_type pos = line.find(";"); if ( pos != std::string::npos ) { std::cout << line << std::endl; } } return 0; }
Jump to PostGreetings,
Since you are using the C++ language, I will stick with the syntax. A good way to read a stream character by character would be a function called sgetc():
What is sgetc()?
int sgetc( );sgetc() is a function that returns the character …
Jump to PostYou can always do it the default way. Dave presented a pretty good example how to do it with iostream, though I just used streambuf for simplicity. If you'd like, I can explain what his code did and how it works.
- Stack Overflow
Jump to PostAlright, lets disect this one line at a time.
We already know about iostream, so lets skip the first line.
» #include <string>
…
This is the standard header <string> to define the container template class basic_string and various supporting templates.
Jump to Post#include <iostream> #include <string> int main() { int lines = 0, lines_with_semicolon = 0; std::string line; while ( getline(std::cin, line) ) { ++lines; std::string::size_type pos = line.find(";"); if ( pos != std::string::npos ) { ++lines_with_semicolon; std::cout << line << std::endl; } } std::cout << "lines = " …
All 20 Replies
coolmel55
0
Light Poster
Dave Sinkula
2,398
long time no c
Team Colleague
coolmel55
0
Light Poster
Stack Overflow
8
Junior Poster
coolmel55
0
Light Poster
Stack Overflow
8
Junior Poster
coolmel55
0
Light Poster
Stack Overflow
8
Junior Poster
coolmel55
0
Light Poster
Dave Sinkula
2,398
long time no c
Team Colleague
coolmel55
0
Light Poster
Dave Sinkula
2,398
long time no c
Team Colleague
coolmel55
0
Light Poster
coolmel55
0
Light Poster
coolmel55
0
Light Poster
coolmel55
0
Light Poster
Stack Overflow
8
Junior Poster
coolmel55
0
Light Poster
coolmel55
0
Light Poster
coolmel55
0
Light Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.