简体   繁体   中英

Read line from file: Unable to use getline()

I have the task to write a function that reads a line from a text file (renamed to .dat , however it contains only text), but I am out of options for a solution because of the following points:

  1. I am using Borland C++ Version 5.02 , and no, I CAN´T download another compiler because I dont have admin rights on my laptop and the guy who has the needed password isnt there until next week.

  2. The compiler does not accept using namespace std , and also it doesnt accept getline() , no matter if string and iostream are included or not.

I am trying to find a solution or at least the tiniest approach, but I am unable to find one.

So my question is: How do I read a line from a simple textfile without using getline() ( cin.getline works, the ones from string or fstream doesnt)? The textfile contains several lines like these:

1234;12.05.03;08:44:23; XY12-AB;A1-12;Timeout

2345;12.05.03;09:04:34;XY1-CD;A22-9;Connection refused

And the numbers/letters between the ;need to be stored in variables so they can be worked with.

Im not asking for you to write my code, but I am reallyreaylly frustrated and my instructor is no help.

Live long and prosper, Me.

http://www.cplusplus.com/reference/string/string/getline/

If you cant use (which you really shouldnt)

using namespace std

Then refer to your namespace with :: operator
For example:

std::string

Now try to write your own code using std:: and comment if you still cant do it.

Also, there is a lot of other options than std::getline() to read line of text.

Ref: Read file line by line

Option 1:

Try using the C's fgets() function.

Option 2: You mention that cin.getline() works. You can freopen stdin with the input file and then cin will point to mentioned file. After that cin.getline() will read from the file:

Downside: After the freopen you will not be able to accept any input from the user.

Example: Note this has not been tried using g++ but I guess it should work with Borland too.

#include <iostream>
#include <stdio.h>

int main() {
        char buf[1000];
        freopen("somefile.dat", "r", stdin);
        while (cin.getline(buf, sizeof(buf)).good()) {
                // Now buf contains a line
                // Do something with it
        }
        return 0;
}

Try using

getline(cin >> ws, variableName);

But first, you have to use

using namespace std;

I'm having the same problem while i using multi dimensional array on structs into a file, i have try different ways. But then i tried this one and it's work for me.

So in my case it gonna be like this

#include <iostream>
#include <fstream>

using namespace std;

int main () {

ifstream myFile;
myFile.open("test.txt");
int count = 0;
while (!myFile.eof())
{
    getline(myFile >> ws, data[count].string1);
    getline(myFile >> ws, data[count].string2);
    myFile >> data[count].int1;
    for (int i = 0; i < data[count].int1; i++) {
        getline(myFile >> ws, data[count].data2[i].string3);
    }
    count++;
}

return 0;

}

For more: https://www.geeksforgeeks.org/problem-with-getline-after-cin/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM