Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c++;读取txt到字符串错误 这个项目是C++的。我试图沿着一个文本文件移动,每次在屏幕上打印一个80个字符长的substr。下一步是更新x的起始位置,然后再次打印。我的目标是在txt上“滑动”。当我启动第二个流操作时,我没有收到错误,但屏幕上不再有文本输出。 谁知道我做错了什么?在执行新操作之前是否必须关闭文件?塔克斯 ifstream ifs("data.txt"); string line; string subline; int x=5; while(getline(ifs,line)) { subline=line.substr(x,80); cout << subline; } system("pause"); system("cls"); x++; //my issue! while(getline(ifs,line)) { subline=line.substr(x,80); cout << subline; } ifstream-ifs(“data.txt”); 弦线; 弦子线; int x=5; while(getline(ifs,line)){ 子线=子线(x,80); cout_C++_String_Text Files - Fatal编程技术网

c++;读取txt到字符串错误 这个项目是C++的。我试图沿着一个文本文件移动,每次在屏幕上打印一个80个字符长的substr。下一步是更新x的起始位置,然后再次打印。我的目标是在txt上“滑动”。当我启动第二个流操作时,我没有收到错误,但屏幕上不再有文本输出。 谁知道我做错了什么?在执行新操作之前是否必须关闭文件?塔克斯 ifstream ifs("data.txt"); string line; string subline; int x=5; while(getline(ifs,line)) { subline=line.substr(x,80); cout << subline; } system("pause"); system("cls"); x++; //my issue! while(getline(ifs,line)) { subline=line.substr(x,80); cout << subline; } ifstream-ifs(“data.txt”); 弦线; 弦子线; int x=5; while(getline(ifs,line)){ 子线=子线(x,80); cout

c++;读取txt到字符串错误 这个项目是C++的。我试图沿着一个文本文件移动,每次在屏幕上打印一个80个字符长的substr。下一步是更新x的起始位置,然后再次打印。我的目标是在txt上“滑动”。当我启动第二个流操作时,我没有收到错误,但屏幕上不再有文本输出。 谁知道我做错了什么?在执行新操作之前是否必须关闭文件?塔克斯 ifstream ifs("data.txt"); string line; string subline; int x=5; while(getline(ifs,line)) { subline=line.substr(x,80); cout << subline; } system("pause"); system("cls"); x++; //my issue! while(getline(ifs,line)) { subline=line.substr(x,80); cout << subline; } ifstream-ifs(“data.txt”); 弦线; 弦子线; int x=5; while(getline(ifs,line)){ 子线=子线(x,80); cout,c++,string,text-files,C++,String,Text Files,是的,您确实需要在使用文件后关闭它。始终清理您在编程中所做的每件事,尤其是文件操作 在实际对文件执行任何操作之前,应该检查文件是否确实成功打开。 当您以现有方式(使用构造函数)打开文件时,如果文件打开操作失败,则不会返回任何内容 您可以检查ifstream方法“good()”以查看流是否已成功打开,或“fail()”以查看是否未成功打开 e、 g ifstream-ifs(“data.txt”); if(ifs.fail()) { 是的,你确实需要在使用文件后关闭它。总是清理你在编程中做的每件事

是的,您确实需要在使用文件后关闭它。始终清理您在编程中所做的每件事,尤其是文件操作

在实际对文件执行任何操作之前,应该检查文件是否确实成功打开。 当您以现有方式(使用构造函数)打开文件时,如果文件打开操作失败,则不会返回任何内容

您可以检查ifstream方法“good()”以查看流是否已成功打开,或“fail()”以查看是否未成功打开

e、 g

ifstream-ifs(“data.txt”);
if(ifs.fail())
{

是的,你确实需要在使用文件后关闭它。总是清理你在编程中做的每件事,尤其是文件操作

在实际对文件执行任何操作之前,应该检查文件是否确实成功打开。 当您以现有方式(使用构造函数)打开文件时,如果文件打开操作失败,则不会返回任何内容

您可以检查ifstream方法“good()”以查看流是否已成功打开,或“fail()”以查看是否未成功打开

e、 g

ifstream-ifs(“data.txt”);
if(ifs.fail())
{

std::cerr在第二次打开文件之前,应尝试关闭该文件。 问题是在执行第二次读取时,文件指针指向最后一个。
尝试将文件指针重置为起始位置或重新打开文件。

在第二次打开文件之前,应尝试关闭文件。 问题是在执行第二次读取时,文件指针指向最后一个。
尝试重置指向文件开头的文件指针或重新打开文件。

您将到达文件结尾,它将设置“”状态位(并删除“”状态位)。这将意味着基本上所有后续读取操作都会失败

简单的修复方法是在x++之后添加:

ifs.clear(); // Get rid of the 'eof' state bit
ifs.seekg(0, ios_base::beg); // Go back to the beginning of the file

您将到达文件的末尾,它将设置“”状态位(并删除“”状态位)。这意味着基本上所有后续读取操作都会失败

简单的修复方法是在x++之后添加:

ifs.clear(); // Get rid of the 'eof' state bit
ifs.seekg(0, ios_base::beg); // Go back to the beginning of the file

你真的应该学习用IDE/调试器调试你的应用程序。你用的是什么IDE?代码块。而且它没有提到任何错误。作为记录:我用了#include#include#include#include#include#使用名称空间std;我不是说编译错误。有了调试器,你可以一步一步地检查你的应用程序,检查它的路径,或者yze变量等。看看这个URL:你真的应该试着学习用IDE/调试器调试你的应用程序。你在用什么IDE?代码块。而且它没有提到任何错误。作为记录:我使用了#include#include#include#include#include使用名称空间std;我不是指编译错误。有了调试器,你可以一步一步通过ap应用程序,检查它的路径,分析变量,等等。看看这个URL:另外,我习惯使用++x而不是x++,因为它可能会稍微快一点;在增量之前,它不需要复制对象来返回旧值。另外@Fuzz就在上面,您可能应该检查ifs.fail()一直以来,我都习惯使用++x而不是x++,因为它可能会稍微快一点;在增量之前,不需要复制对象来返回旧值。另外@Fuzz就在上面,您可能应该一直检查ifs.fail()。