从数据文件C 读取时,将删除第一个字母
First letters being erased when reading from data file c++
有人可以解释为什么从数据文件读取时只有第一个字母被删除,而仅在数组的1/2/3部分而不是0部分?(对不起,真的不知道如何解释)(我只包括我所获得的部分以及数据文件)
我得到的
googleylesmith01@gmail.comymerman27Ecurity问题:与手表的白兔子deviantartragonmaster27andalfthegreynull
应该是
googlekylesmith01@gmail.comkyleman27securityquestion:whiterabbitwithawatchDeviantArtdraganmaster27gandalfthegreynull
和原始数据文件
google; kylesmith01@gmail.com; kyleman27;安全问题:带有手表的白兔子;DeviantArt; Dragonmaster27; Gandalfthegrey;null;
我不会包含所有代码,因为它应该与此问题无关
#include<iostream>
#include <fstream>
#include <string>
#include <vector>
#include<sstream>
using namespace std;
const int NCOLS = 4;
const int NROWS = 10;
void description_and_options(string data[][NCOLS], int count[NCOLS]);
void available_options();
void view_line_data(int choice,string data[][NCOLS]);
int main()
{
ifstream file_name;//create the new file
string user_input_file;//the files name inputed by the user
int stringlength;
string read_in_array[NROWS][NCOLS];
string line;
int counter[10] = { 1,2,3,4,5,6,7,8,9,10 };
string user_option_choice;
string small_exit = "x";
string large_exit = "X";
int view_choice;
cout << "Enter the name of the input file: ";
cin >> user_input_file;
if (user_input_file.length() > 4)// check to see if its more than 4 in length
{
stringlength = user_input_file.length(); //saves length
if (user_input_file.substr(stringlength - 4, 4) == ".txt")//checks to see if its .dat
{
file_name.open(user_input_file.c_str());
if (file_name.fail())
{
cerr << "The file " << user_input_file << " failed to open.n";//tells user if it fails
exit(1);
}
}
}
else
{
user_input_file += ".txt";//adds .dat to non .dat
file_name.open(user_input_file.c_str());
}
if (file_name.fail())
{
cout << "File failed to open" << endl;
system("PAUSE");
exit(1);
}
for (int row = 0; row <= 9; row++)
{
for (int col = 0; col < 4; col++)
{
if (getline(file_name, line, ';'))
{
file_name.ignore(1, 'n');
read_in_array[row][col] = line;
cout << read_in_array[row][col];
}
}
cout << endl;
}
//[updown][leftright]
file_name.close();
无论如何是否可以解决此问题,而无需完全更改代码?
它忽略了第一个字符,因为您将其告诉
file_name.ignore(1, 'n');
每次调用getline
之后,将忽略流中的第一个字符。看起来您正在这样做,因为您认为文件中仍然存在的;
。关于getline
,您需要记住的是,它丢弃了您使用的定界符。这意味着它将读取直到找到;
,然后将;
扔掉。这意味着您不需要忽略它,因为它不再存在。
仅删除对ignore
的呼叫不足以解决该问题。由于您试图解析整个行,我们需要做的就是将行读取到stringstream
中,然后在流上调用getline
以获取各个部分。这是因为仅阅读到;
将捕获Newline。
快速重构代码会为您提供看起来像
的东西for (int row = 0; row <= 9; row++)
{
std::string temp;
std::getline(file_name, temp)
std::stringstream ss(temp)
for (int col = 0; col < 4; col++)
{
if (getline(ss, line, ';'))
{
read_in_array[row][col] = line;
cout << read_in_array[row][col];
}
}
cout << endl;
}
您正在错误地使用ifstream :: ignore()。
从输入序列中提取字符并丢弃它们,直到 提取了n个字符,或者一个字符的比较等于 特尔姆。
file_name.ignore(1, 'n');
始终驳回首字母。在您的情况下,"之后的首字母排队。
file_name.ignore(1, 'n');
将使流从输入中忽略一个字符。
通过阅读您的代码:
- 对于您所谓的" 0部分",在循环中的第一个
getline
之前尚未调用ignore
。 - 对于"零件1/2/3",
ignore
语句使流程跳过下一个字符 - 对于其余部分,有一个空间或一个" n",以使可读字母没有被跳过。
- 为什么它只打印双链接列表的第一个值,而我的程序却崩溃了
- std::find,返回所有找到的值的替代方法,而不仅仅是存在重复的向量的第一个值
- G++ 发出警告,要求删除一个代码的数组,但不删除另一个代码的数组
- 如何仅读取文本文件中的第一个值
- 在C++中,如何在第一个"system()"结束后执行第二个"system()"?
- 删除第 I 个元素
- 查找不在标准中的第一个值::设置<int>最小-最大值
- 避免在使用链接列表从 deque 中删除最后一个节点时出现内存泄漏
- std::字符串擦除以删除最后一个字母
- C++:忽略第一个 cin.ignore 之后的输入
- 在C++中打印多个矢量的第一个值
- C++去除前x个元素的有效方法,在不改变向量大小的情况下将第x+1个元素推到第一个
- C++第一个cout将不会打印
- 我们可以在第一个else-if条件结束后使用另一个else-if条件吗
- OpenGL:第二个VBO破坏了第一个VBO
- 为什么第一个Dynamic_cast没有投射到基类?
- 一个对象的两个指针.删除了一个指针,对象仍然存在
- OpenGL 2D游戏只绘制第二个精灵纹理而不是第一个
- 删除指针作为 std::map 的第一个键
- 删除二进制字符串中的第一个0