file.txt的内容是:
5 3
6 4
7 1
10 5
11 6
12 3
12 4
Run Code Online (Sandbox Code Playgroud)
5 3坐标对在哪里.如何在C++中逐行处理此数据?
我能够得到第一行,但是如何获得文件的下一行?
ifstream myfile;
myfile.open ("text.txt");
Run Code Online (Sandbox Code Playgroud) 我看到人们最近在很多帖子中试图读取这样的文件.
码
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char **argv)
{
char *path = argc > 1 ? argv[1] : "input.txt";
FILE *fp = fopen(path, "r");
if( fp == NULL ) {
perror(path);
return EXIT_FAILURE;
}
while( !feof(fp) ) { /* THIS IS WRONG */
/* Read and process data from file… */
}
if( fclose(fp) == 0 ) {
return EXIT_SUCCESS;
} else {
perror(path);
return EXIT_FAILURE;
}
}
Run Code Online (Sandbox Code Playgroud)
这个__CODE__循环有什么问题?
以下C++代码使用ifstream对象从文本文件(每行有一个数字)读取整数,直到它达到EOF.为什么它读取最后一行的整数两次?如何解决这个问题?
码:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream iFile("input.txt"); // input.txt has integers, one per line
while (!iFile.eof())
{
int x;
iFile >> x;
cerr << x << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
input.txt:
10
20
30
Run Code Online (Sandbox Code Playgroud)
输出:
10
20
30
30
Run Code Online (Sandbox Code Playgroud)
注意:我已跳过所有错误检查代码,以使代码段保持较小.在Windows(Visual C++),cygwin(gcc)和Linux(gcc)上可以看到上述行为.
我正在编写一个程序,直接从用户输入读取数据,并想知道我怎么能(没有循环)读取所有数据,直到标准输入EOF.我正在考虑使用,cin.get( input, '\0' )但'\0'不是真正的EOF角色,只读到EOF或者'\0'以先到者为准.
或者使用循环是唯一的方法吗?如果是这样,最好的方法是什么?
来自Stroustrup的TC++ PL,第3版,第21.3.3节:
如果我们尝试读入变量v并且操作失败,则v的值应该保持不变(如果v是istream或ostream成员函数处理的类型之一,则它不会改变).
以下示例似乎与上述引用相矛盾.基于上面的引用,我期待v的值保持不变 - 但它会变为零.对这种明显的矛盾行为有什么解释?
#include <iostream>
#include <sstream>
int main( )
{
std::stringstream ss;
ss << "The quick brown fox.";
int v = 123;
std::cout << "Before: " << v << "\n";
if( ss >> v )
{
std::cout << "Strange -- was successful at reading a word into an int!\n";
}
std::cout << "After: " << v << "\n";
if( ss.rdstate() & std::stringstream::eofbit ) std::cout << "state: eofbit\n";
if( ss.rdstate() & std::stringstream::failbit ) std::cout << "state: failbit\n"; …Run Code Online (Sandbox Code Playgroud) 我正在读一本关于C++的书,它说如果我使用>>运算符,它会返回运算符左侧的对象,所以在这个例子中
Run Code Online (Sandbox Code Playgroud)std::cin >> value1;
代码返回std::cin.
但是,如果我这样做
while(std::cin >> value1)
Run Code Online (Sandbox Code Playgroud)
我的代码将处于循环中,直到出现std::cin错误,因此必须表示运算符bool在std::cin未失败时返回true,在失败时返回false std::cin.
哪一个是它?
我有兴趣讨论用于stringstream解析具有多种类型的行的方法.我将从查看以下行开始:
"2.832 1.3067 nana 1.678"
Run Code Online (Sandbox Code Playgroud)
现在让我们假设我有一个有多个strings和的长行doubles.解决这个问题的显而易见的方法是将字符串标记化,然后检查转换每个字符串.我有兴趣跳过第二步,stringstream直接使用只找到数字.
我认为解决这个问题的好方法是读取字符串并检查是否failbit已设置,如果我尝试将字符串解析为double,它将会是这样.
说我有以下代码:
string a("2.832 1.3067 nana 1.678");
stringstream parser;
parser.str(a);
for (int i = 0; i < 4; ++i)
{
double b;
parser >> b;
if (parser.fail())
{
std::cout << "Failed!" << std::endl;
parser.clear();
}
std::cout << b << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
它将打印出以下内容:
2.832
1.3067
Failed!
0
Failed!
0
Run Code Online (Sandbox Code Playgroud)
我没有惊讶它没有解析一个字符串,但内部发生了什么,以至于它无法清除它failbit并解析下一个数字?
我想打开一个文件进行阅读,即C++方式.我需要能够做到:
文本文件,涉及某种读取线功能.
二进制文件,它提供了一种将原始数据读入char*缓冲区的方法.
所以我使用过eof()的功能在我的很多程序,需要文件输入的,我的教授说,这是蛮好用的,但有几个人在等纷纷表示,我不应该没有真正指定的原因使用它.所以我想知道,有充分的理由吗?
我有以下代码:
ifstream f("x.txt");
string line;
while (f.good()) {
getline(f, line);
// Use line here.
}
Run Code Online (Sandbox Code Playgroud)
但这会两次读到最后一行.为什么会发生这种情况,我该如何解决?
与以下情况非常相似:
ifstream f("x.txt");
string line;
while (!f.eof()) {
getline(f, line);
// Use line here.
}
Run Code Online (Sandbox Code Playgroud)