[英]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:我的任务是编写一个 function 从文本文件中读取一行(重命名为
.dat
,但它只包含文本),但由于以下几点,我无法选择解决方案:
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.我正在使用
Borland C++ Version 5.02
,不,我不能下载另一个编译器,因为我的笔记本电脑没有管理员权限,而且知道所需密码的人要到下周才会出现。
The compiler does not accept using namespace std
, and also it doesnt accept getline()
, no matter if string
and iostream
are included or not.编译器不接受
using namespace std
,也不接受getline()
,无论是否包含string
和iostream
。
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)?所以我的问题是:如何在不使用
getline()
的情况下从简单的文本文件中读取一行( cin.getline
有效,而来自string
或fstream
的无效)? The textfile contains several lines like these:文本文件包含如下几行:
1234;12.05.03;08:44:23;
1234;12.05.03;08:44:23; XY12-AB;A1-12;Timeout
XY12-AB;A1-12;超时
2345;12.05.03;09:04:34;XY1-CD;A22-9;Connection refused
2345;12.05.03;09:04:34;XY1-CD;A22-9;连接被拒绝
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/ 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. 现在尝试使用std ::编写自己的代码,如果仍然无法注释,请添加注释。
Also, there is a lot of other options than std::getline() to read line of text. 另外,除了std :: getline()以外,还有很多其他选项可以读取文本行。
Ref: Read file line by line 参考: 逐行读取文件
Option 1: 选项1:
Try using the C's fgets() function. 尝试使用C的fgets()函数。
Option 2: You mention that cin.getline() works. 选项2:您提到cin.getline()有效。 You can freopen stdin with the input file and then cin will point to mentioned file.
您可以使用输入文件freopen stdin,然后cin指向所提到的文件。 After that cin.getline() will read from the file:
之后,cin.getline()将从文件中读取:
Downside: After the freopen you will not be able to accept any input from the user. 缺点:在freopen之后,您将无法接受用户的任何输入。
Example: Note this has not been tried using g++ but I guess it should work with Borland too. 示例:请注意,尚未使用g ++尝试过此方法,但我想它也应与Borland一起使用。
#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/更多信息: https://www.geeksforgeeks.org/problem-with-getline-after-cin/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.