0

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.

Community
  • 1
  • 1
  • "I CAN´T download another compiler because I dont have adminrights" - you don't need admin rights. – Karoly Horvath Nov 28 '14 at 09:54
  • Well I do when I want to install that compiler. All our (me and the other pupils) laptops have been set up so we cant install something without the admin. (To make sure nobodys going to mess around and accidently install malware) – Kampfkeksgeschwader Nov 28 '14 at 09:55
  • Seek, and ye shall find: http://cygwin.com/faq.html#faq.setup.noroot – marc Nov 28 '14 at 09:59
  • "that compiler"? note: you should quit.... – Karoly Horvath Nov 28 '14 at 09:59
  • Your choices are not limited to "download another compiler"; there are *lots* and *lots* of ways to read text without using `cin` and `getline` (which are after all only 'convenience wrappers'). Use `fgets` -- and if everything else fails, use `fread`. – Jongware Nov 28 '14 at 10:00
  • @Karoly Horvath: Sorry for not speaking perfect english as a 18-year old german pupil. – Kampfkeksgeschwader Nov 28 '14 at 10:07
  • @Jongware: My problem is that I should (should means I have to) use C++ code for that. Dont know if I have a little blackout or if I just dont understand that read/write file thing...but thats why im asking. – Kampfkeksgeschwader Nov 28 '14 at 10:10
  • @Jongware: not limited, but it's by *far* the best option. – Karoly Horvath Nov 28 '14 at 10:13

3 Answers3

0

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

Community
  • 1
  • 1
Patryk Krawczyk
  • 1,342
  • 1
  • 14
  • 25
0

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;
}
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/