Using cin.getline() with files

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.

Thanks in advance for any response given!

closed account (48T7M4Gy)
I'll give you two tips:

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/
> use cin.getline() using the data from text file that was given.

The simplest solution is to use redirection from the command line.
To read the contents of myfile.txt with std::cin use the command line:
./myprogram < myfile.txt
It works the same way in all command line processors.
unix sh and csh: http://sc.tamu.edu/help/general/unix/redirection.html
windows cmd: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx?mfr=true

Here is an example:

File main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#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" ;
    }
}


From the command line:
clang++ -std=c++14 -stdlib=libc++ -O2 -Wall -Wextra -pedantic-errors -pthread main.cpp -lsupc++ -omyprogram 
./myprogram < main.cpp

http://coliru.stacked-crooked.com/a/6dd1b0619e32f710
To do this from within the program (convenient if you are dead without an IDE to baby-sit over you):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <fstream>

struct redirect_file_to_cin
{
    redirect_file_to_cin( const char* 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 ;
    void operator= ( 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" ;
    }
}

http://coliru.stacked-crooked.com/a/52733b64f62f576f
Topic archived. No new replies allowed.