Hello, i'm taking a fundamental c++ course and a part of my newest assignment is asking me to read 100 characters from a file at a time without having any cut off words.
The professor wants me to use cin.getline() in my solution.
Unfortunately, I haven't been able to successfully figure out how to use cin.getline() using the data from text file that was given. I can't even get cin.getline() to work in general.
I don't know how opposed this forum is to giving working examples, but I think it would help me the most to understand. This is just a small, but crucial, part of this assignment.
1. The tutorials on this site are very useful and self-help is often the quickest way to solving a problem even though these forums are available to everyone.
2. It is unlikely people will respond with working examples to your problem because the problem is not defined, you haven't shown your attempts along with the sample data, and regrettably this is not a homework site.
But,
cin.getline is not consistent with your requirement that the information comes from a file instead of standard input (cin) This is a link to cin.getline with a sample. http://www.cplusplus.com/reference/istream/istream/getline/?kw=cin.getline
You might like to address what appears to be your problem by reading http://www.cplusplus.com/doc/tutorial/files/
#include <iostream>
int main()
{
const std::size_t MAX_LINE_SZ = 8191 ;
char line[MAX_LINE_SZ+1] ; // +1 for the terminating null character
while( std::cin.getline( line, MAX_LINE_SZ ) ) // for each line read from stdin
{
// do something with the line: for example, print it normally and then print it in reverse
std::cout << line << '\n' ;
// gcount(): number of characters that were read (including the null character at the end)
auto pos = std::cin.gcount() - 1 ; // -1 because we don't want to print the null character at the end
while( pos > 0 ) std::cout << line[ --pos ] ;
std::cout << "\n\n" ;
}
}
#include <iostream>
#include <fstream>
struct redirect_file_to_cin
{
redirect_file_to_cin( constchar* file_name )
{
fbuf.open( file_name, std::ios::in ) ; // open file for input
oldbuf = std::cin.rdbuf( std::addressof(fbuf) ) ; // redirect file to std::cin
}
~redirect_file_to_cin() { std::cin.rdbuf(oldbuf) ; }
// not copyable or assignable
redirect_file_to_cin( const redirect_file_to_cin& ) = delete ;
voidoperator= ( const redirect_file_to_cin& ) = delete ;
std::filebuf fbuf ;
std::streambuf* oldbuf ;
};
int main()
{
redirect_file_to_cin redirector( __FILE__ ) ; // redirect this file to std::cin
const std::size_t MAX_LINE_SZ = 8191 ;
char line[MAX_LINE_SZ+1] ; // +1 for the terminating null character
while( std::cin.getline( line, MAX_LINE_SZ ) )
{
// do something with the line: for example, print it normally and then print it in reverse
std::cout << line << '\n' ;
// gcount(): number of characters that were read (including the null character at the end)
auto pos = std::cin.gcount() - 1 ; // -1 because we dobn't want to print the null character at the end
while( pos > 0 ) std::cout << line[ --pos ] ;
std::cout << "\n\n" ;
}
}