ifstream :: getline

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream test;
test.open("test",ios::trunc);
char temp_str[3];
test.getline(temp_str,sizeof(temp_str));
test.seekg(0,test.beg);// *this was try resolve
cout << test.tellg(); // output : -1
// rest
test.close();
}

why can't i continue working with the file
There are several possibilities as to why your program is failing.

First you should always test that your file opened successfully. Since you tried to open your file for input with the ios::trunc flag, the file will not open. The ios::trunc is only useful for files opened for output mode. And note since the ios::trunc erases the file contents if your file did open then the read would fail because there is nothing in the file to read. Lastly since you're trying to read such a small string it is possible that you are trying to retrieve more characters than the string can accommodate before the end of line character is encountered, which will also cause a stream error. And remember anytime a stream is in an error state no further processing of that stream will occur until the error is cleared.


Thank you very much for your answer, but if I get rid of the logical errors, then anyway I will get -1. And I would want to escape it.
I'd start by using the std::string version of getline(), it is less likely to fail due to buffer too small problems, which can easily arise from using a char array.

1
2
    char temp_str[3];
    test.getline(temp_str,sizeof(temp_str));

would become
1
2
    string temp_str;
    getline(test, temp_str);


http://www.cplusplus.com/reference/string/string/getline/
Thanks again, I want to ask you about what might be my problem, that I always typed the text myself.
PS
Sorry for my English
Which problem are you asking about? I listed several in my first post.

Perhaps you should try something like the following for a start:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    ifstream inFile("test.txt");  // Note the file test.txt must exit in the current working directory.
    
    string temp_str;

    // Get a complete line of text from the file.
    getline(inFile, temp_str);

    // Print the string retrieved.
    cout << temp_str << std::endl;
    
}


What might be the problem? In this case, the file did not open, because ios::trunc only applies to output files.

To find where the error occurred, add some tests of the file status. After every operation, possibly something could have gone wrong. So you need to check.

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
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ifstream test;
    test.open("test",ios::trunc);
    
    if (test.fail())
    {
        cout << "Error 1 - could not open input file\n";
        return 1;    
    }    
    
    
    char temp_str[3];
    test.getline(temp_str,sizeof(temp_str));
    if (test.fail())
    {
        cout << "Error 2 - getline failed\n";
    }
    
    
    test.seekg(0,test.beg);
    if (test.fail())
    {
        cout << "Error 3 - seekg failed\n";
    }
    
    
    cout << "4 - tellg position: " << test.tellg() << '\n';

}

The code as it stands outputs the message:
Error 1 - could not open input file

I put return 1 after the error message, if the file is not open, there's no point in continuing with the rest of the program.

So fix that code error now:
 
    test.open("test",ios::trunc);
should be:
 
    test.open("test");


Recompile the code and run the program again.

If the file is still not open now, you need to check the name and location of the input file is correct.


When you get the file to open there may still be errors.

When I run it, this is the output:
Error 2 - getline failed
Error 3 - seekg failed
4 - tellg position: -1


Why did the getline fail? Well, the first line of my file is "apple". That needs a buffer big enough to contain the 5 letters plus the null terminator '\0'.

So, let's make the buffer bigger:
 
    char temp_str[3];
becomes
 
    char temp_str[100];


I made the buffer reasonably large, because the line length is often likely to be more than five characters.

Recompile and try again. If all goes well, you should see:
4 - tellg position: 0

Last edited on
ok, I got it: a buffer must be bigger than a line size of the file.txt.
But if I want to take a string which smaller than the file string, what the command I must use?
But if I want to take a string which smaller than the file string, what the command I must use?

If you don't want to read all of the line, but just a single word, then use
 
    test >> temp_str;


Reading from an input file stream is very similar to reading user input via cin. If you know how to use cin, then you have a pretty good idea how to read from a file.
wow, it was so easy thx
Topic archived. No new replies allowed.